Dynamically package MAVEN projects with Maven-war-plugin

Source: Internet
Author: User

Reproduced in: http://nileader.blog.51cto.com/1381108/449956


In the project release, you may encounter this situation, I hope that the project source code to maintain the same premise, you want to be able to target different operating environment to obtain the corresponding running package. (e.g. War pack)

It is possible to use a configuration file to solve this problem. You can put some parameters in the project and the running environment in the configuration file, each environment, so that before packaging just specify this package I need to specify which configuration file this package needs, You can then make the run package correspond to the environment. But the bad thing about this is that the choice of the configuration file requires human intervention--if you encounter a situation like this: xxx, you give me the package of environment. At this time, the usual practice is to immediately recall I want to change which place, let the project know what I want to use the configuration.

Now there is another way, using Maven-war-plugin, this plugin can specify which environment package I want to play when executing the package command, without needing to pay attention to what configuration file I want to use now. Of course only for MAVEN projects.

For example: Maven package–p youenvname so you can play a youenvname environment of the running package.

The 1th step is to prepare different operating parameters for different environments: Two files are built in the Src/main/resources directory to configure the different parameters of the two environments. Note that the configuration file needs to be a legitimate properties file: for example:

Src/main/resources/iproject-test.properties src/main/resources/iproject-real.properties

corresponding to the test and formal two environments respectively. The content of course is two keys a value not one of the key-value is right.

This is usually the case in src/main/resources/iproject-test.properties (just a sample)

System.envid=real method.version=1.0.0. Release .....

2nd step, create a new directory under Src/main, for example: Src/main/packagefilter

Then find the one or more profile files (usually in the Web-inf directory) that you want to use for this few parameters, and move them to the Src/main/packagefilter directory. (This time you may ask: The configuration file changed places, the project understand?) Df

Once these are configured in the Src/main/packagefilter directory, make changes to the parameters that are configured with the above (just the sample)

<bean id= "MyService" class= "Com.nileader.MyService" init-method= "init" > <property name= "Version" > <value>${system.envid}</value> </property> </bean>

Here you see, the parameter already has a placeholder for a ${key}.

3rd step, add the dependencies of the package plugin in the Pom file:

<filters>       <filter> src/main/resources/ iproject-test.properties </filter>   </filters>   <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-war-plugin</artifactId>       < configuration>           <webResources>               <resource>                    <directory>src /main/packagefilter</directory>                    <filtering>true</filtering>                    <targetpath>web-inf</targetpath>                </resource>            </webResources>       </configuration>    </plugin> 

This will solve the problem just mentioned, the configuration file seems to have moved, but do not need to tell you the project, in fact, when the project load configuration file, we have a complete, does not contain any placeholder configuration in the project should be placed in the location, here is implemented by this configuration:

<resource> <directory>src/main/packageFilter</directory> <filtering>true</filtering >

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.