Maven dynamically modifies the value or properties of the properties file according to the P parameter value package

Source: Internet
Author: User

Because I recently busy company affairs, yesterday did not write a blog, continue to write today

Requirements: Due to the recent development of the Clover Project, no use of spring, no use of any framework, and the use of the Java EE Web project, so connect ZK and MongoDB, Redis and other servers need to specify the properties file,

At present, the company also sub-set environment (Dev, test, product), so want to package the time according to the profile ID to dynamically use different environment properties file or dynamically modify the properties parameter values

Method One: Use a set of properties configuration files, but the specific parameters are packaged and replaced using the ${key} placeholder method





Specify parameter values for each set of environments in Maven's POM



We also want to activate the Resources filter filter:


Following the above configuration, after we perform the MAVEN packaging operation:


We can see that mongodbconfig.properties, Redisconfig.properties, zkconfig.properties and so on are all replaced with the parameters of the specified environment.

Since some older systems specify each set of environment profiles under resources in the project, such as slices:


However, we only want to move the *.properties files under the specified environment to classes after packing, so we can consider using method two

Method Two: Using Maven-antrun-plugin plug-in method to implement

We are only packaging the dev environment:

We want to specify the Resources.dir directory as Src/main/resources/dev

We add a plugin for maven-antrun-plugin, as I just move these files under the sub-project Clover-core, and eventually play into a jar package, So to put this plugin code under the Clover-core pom.xml file, if you put this plugin in the parent project Pom.xml then move all sub-projects under the resources file


Let's stick to the code:

<build><finalName>Clover-core-${clover.version}</finalName><plugins><plugin><groupId>Org.apache.maven.plugins</groupId><artifactId>Maven-antrun-plugin</artifactId><executions><execution><id>Compile</id><phase>Compile</phase><configuration><tasks><echomessage= "********************** copy resources to classpath*************************"/><echomessage= "********************** copy ${resources.dir} to classpath*************************"/><copyTodir= "Target/classes"Overwrite= "true"><filesetdir= "${resources.dir}"><includename="*.*"/><includename="*/*.*"/></fileset></copy></tasks></configuration><goals><goal>Run</goal></goals></execution></executions></plugin></plugins><resources><resource><directory>Src/main/resources</directory><filtering>True</filtering></resource></resources></build>

After we execute the package:


We can see that these *.properties have been moved to classes under the Clover-core.

Personally, it's the first way to be more recommended.

PS: Sometimes we want to use dynamic packaging, the name of the generated sub-project is with each set of environment, such as: Clover-core-dev-1.0.jar

We set the clover.version under the profile of the parent project.


Sub-engineering Clover-core want final package is clover.version specified environment + version number:


<version>${clover.version}</version>  ---This will be packaged after the final version of the Central private library
<finalName>clover-core-${clover.version}</finalName>-This is the sub-project Clover-core packaging final name

After we execute the package:

Private Library


When we play the war package, we can also specify the version number of the dependent module:


We use the profile configuration of the environment version, but let you want to change to another version, you can see a package of dependent jar is the version you specified

We changed the Clover-core version to dev-1.0.


And put Clover-core's pom.xml


After packing, we look at the war package:


But I look at Target under the Clover-core project:


It turns out that's the case

Maven takes a long time, but most of the time is once configured, then no modifications are made and then the project is developed, but some of the details are eventually not studied so clearly

The article writes a little water, hoped to be useful to you, thanks


Maven dynamically modifies the value or properties of the properties file according to the P parameter value package

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.