MAVEN Auto-config Multi-environment automatic packaging

Source: Internet
Author: User
Tags maven central

Maven has a lot of good plug-ins, recently in the research package to replace the configuration of multiple environments, colleagues introduced the use of Ali Auto-config, used a bit really good.

Auto-config is actually a lot more than the ant age character substitution, but Ant needs to write its own script to do these things, but Auto-config can be configured.

Auto-config's core technology I personally think that is volicty template technology, including the inside of the placeholder oplacehold are defined in ${}volicty syntax.

Auto-config mainly involved in the following parts of the document: Auto-config.xml,template.vm,antx.property.

Auto-config.xml primarily defines those characters that need to be replaced, what their default values are, those files that need to be replaced with placeholders, and the output files.

TEMPLATE.VM The main definition template file, is actually the configuration file that we need, but the symbol that needs to be replaced here is output in the form of placeholder.

Antx.property defines a placeholder for a value that is used in place of a placeholder for a specific value.

If you use Auto-config:

1. Install Maven

2. Using the Auto-config plug-in, Need to insert in Pom.xml plugin, follow-up will be detailed, here it is necessary to say that the Great Wall Firewall in the mainland, too much, maven central warehouse download a jar package takes half an hour, here can again maven setting file mirrors add the following configuration:,

<mirror>
<id>nexus-osc</id>
<mirrorOf>external:*</mirrorOf>
<name>nexus osc</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
</mirrors>

It means downloading all maven files from the Oschina station.

Pom.xml increase the configuration of Auto-config

<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 also download down on the network, I manually added a configuration

<configuration>
<userProperties>${user.dir}/${autoconfig.properties}</userProperties>
</configuration>
What's this for? For the file location of the custom Antx.property, why do you want to customize the file location? The reason is that it is convenient to specify parameters in a multi-environment to modify the configuration.

Here User.dir is the path to the current project, autoconfig the default file location is in the User.home directory.


In this case, you need to modify the configuration in a multi-environment, so you need to configure the profile 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

Only Mavn package-p Dev or Mavn package-p Pro can be used to package production or development packages during the actual compilation process.

After the pom.xml is defined, you need to add a Meta-inf folder under the Web-inf directory and a new autoconf folder under the folder.

Under the Autoconf folder to place the Auto-config.xml and all the template files, note that the template file must be placed here, I currently do not find the configuration put to another location.


Specific configuration of the Auto-config:

<?xml version= "1.0" encoding= "UTF-8"?>
<config>
<group>
<property name= "Petstore.work" description= "Application working directory"/>
<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 uploaded files"/>
<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>

The name of the property definition here is the placeholder name, DefaultValue is the default value

Script below defines the specific template, as well as the output file.


WEB.VM is a template file definition, which is our configuration file, where variables are split through 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 is the specific configuration of the landing, can have multiple, through the mvn-p to develop specific configuration files

Petstore.work=1
petstore.loggingroot=2
Petstore.upload=3
Petstore.logginglevel=4

If not in Pom. XML is configured by configuring the default antx.properties in the root directory of the project, or under User.home, if no autoconfig is required for you to create.


By modifying four files you can basically use Auto-config, the packaged file has changed the placeholder to the value we need, this is a multi-environment packaging artifact.


MAVEN Auto-config Multi-environment automatic 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.