Maven auto-config multi-Environment automatic packaging, mavenauto-config

Source: Internet
Author: User
Tags maven central

Maven auto-config multi-Environment automatic packaging, mavenauto-config

Maven has many excellent plug-ins. Recently I have been studying how to replace the configuration of multiple environments in packaging. My colleague introduced how to use Alibaba's auto-config, which is really good.

In the auto-config era, there are almost too many characters to replace, but ant needs to write scripts to complete these tasks, but auto-config can be configured.

I personally think the core technology of auto-config is volicty's template technology, including the placeholder oplacehold in it, which is defined by the syntax of $ {} volicty.

Auto-config involves the following files: auto-config.xml, template. vm, antx. property.

The auto-config.xml mainly defines the characters that need to be replaced, what their default value is, the files that need to replace placeholders, and the output files.

Template. vm mainly defines the template file, which is actually the configuration file we need, but all the symbols to be replaced here are output in the form of placeholders.

Antx. property defines the specific values in the placeholder to replace the placeholder value.

If you use auto-config:

1. Install maven

2. to use the auto-config plug-in, you must go to the pom. insert the plug-in xml, which will be detailed later. It is necessary to talk about the Great Wall firewall in mainland China. It takes about half an hour to download a jar package from maven central repository, here, you can add the following configuration to mirrors in the setting file of maven :,

<Mirror>
<Id> nexus-osc </id>
<MirrorOf> external: * </mirrorOf>
<Name> Nexus osc </name>
<Url> http://maven.oschina.net/content/groups/public/ </url>
</Mirror>
</Mirrors>

Download all maven files from oschina.

Add auto-config configuration in pom. xml

<Build>
<FinalName> json </finalName>
<Plugins>
<Plugin>
<GroupId> com. alibaba. citrus. tool </groupId>
<ArtifactId> autoconfig-maven-plugin </artifactId>
<Version> 1.2 </version>
<Configuration>
<UserProperties >$ {user. dir}/$ {autoconfig. properties} </userProperties>
</Configuration>
<Executions>
<Execution>
<Phase> package </phase>
<Goals>
<Goal> autoconfig </goal>
</Goals>
</Execution>
</Executions>
</Plugin>
</Plugins>
</Build>

The configuration here is downloaded from the network. I manually added a configuration

<Configuration>
<UserProperties >$ {user. dir}/$ {autoconfig. properties} </userProperties>
</Configuration>
What is this? It is used to customize the File Location of antx. property. Why do we need to customize the file location? The reason is that it is convenient to specify parameters in multiple environments to modify the configuration.

Here, user. dir is the path of the current project, and the default file location of autoconfig is in the user. home Directory.


The configuration needs to be modified in multiple environments, so you need to configure the profiles of multiple environments in pom. xml.

<Profiles>
<Profile>
<Id> dev </id>
<Properties>
<Autoconfig. properties> antx-dev.properties </autoconfig. properties>
</Properties>
</Profile>
<Profile>
<Id> deploy </id>
<Properties>
<Autoconfig. properties> antx-pro.properties </autoconfig. properties>
</Properties>
</Profile>
</Profiles>

<Profiles>
<Profile>
<Id> dev </id>
<Properties>
<Autoconfig. properties> antx-dev.properties </autoconfig. properties>
</Properties>
</Profile>
<Profile>
<Id> pro </id>
<Properties>
<Autoconfig. properties> antx-pro.properties </autoconfig. properties>
</Properties>
</Profile>
</Profiles>

In the actual compilation process, you only need mavn package-P dev or mavn package-P pro to package the packages for production or development.

After pom. xml is defined, you need to add a WEB-INF folder under the META-INF directory and add the autoconf folder under the folder

Place the auto-config.xml and all the template files in the autoconf folder, note that the template file must be placed here, and I have not found the configuration to put it elsewhere.


Specific configuration of auto-config:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Config>
<Group>
<Property name = "petstore. work" description = "working directory of the application"/>
<Property name = "petstore. loggingRoot" defaultValue = "$ {petstore. work}/logs"
Description = "log file directory" required = "true"/>
<Property name = "petstore. upload" defaultValue = "$ {petstore. work}/upload"
Description = "directory of the uploaded file"/>
<Property name = "petstore. loggingLevel" defaultValue = "warn"
Description = "Log File level">
</Property>
</Group>
<Script>
<Generate template = "web. vm" destfile = "WEB-INF/classes/spring-web.xml" charset = "UTF-8"/>
</Script>
</Config>

Here, the names defined by property are all placeholder names, and defaultvalue is the default value.

The specific template defined in the script and the output file.


Web. vm is defined in the template file, which is our configuration file. variables are separated by placeholders.

<? Xml version = "1.0" encoding = "UTF-8"?>
<Config>
<Property name =$ {petstore. work}> </property>
<Property name =$ {petstore. loggingRoot}> </property>
<Property name =$ {petstore. upload}> </property>
<Property name =$ {petstore. loggingLevel}> </property>
</Config>

Antx. property indicates the implementation of specific configurations. Multiple configuration files can be created through mvn-p.

Petstore. work = 1
Petstore. loggingRoot = 2
Petstore. upload = 3
Petstore. loggingLevel = 4

If not in pom. Xml uses configuration to configure the default antx. properties in the root directory of the project, or in the user. home Directory. If autoconfig is not available, you need to create it.


You can basically use auto-config by modifying the four files. The packaged file has changed the placeholder to the value we need. This is an artifact of Multi-Environment packaging.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.