MAVEN profile for multi-environment packaging

Source: Internet
Author: User

Quick FIX:

Project directory

Add profile to the 1.pom file

<profiles>
<profile>
<!--Local development environment--
<id>Dev</id>
<properties>
<profiles.active>Dev</profiles.active>
</properties>
<activation>
<activeByDefault>True</activeByDefault>
</activation>
</profile>
<profile>
<!--test environment--
<id>Test</id>
<properties>
<profiles.active>Test</profiles.active>
</properties>

</profile>
<profile>
<!--production environment--
<id>Prod</id>
<properties>
<profiles.active>Prod</profiles.active>
</properties>
</profile>
</profiles> 

2. Specify the filter folder and Maven-war-plugin in the Pom to specify the replacement folder

 <build>
<resources>
<resource>
<directory>Src/main/resources</directory>
<includes>
<include>Spring-content.xml</include>
</includes>
<filtering>True</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>Org.apache.maven.plugins</groupId>
<artifactId>Maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archiveClasses>True</archiveClasses>
<warName>${project.artifactid}</warName>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
<webXml>${basedir}/src/main/webapp/web-inf/web.xml</webXml>
<webappDirectory>${project.build.directory}/${project.artifactid}
</webappDirectory>
<webResources>
<resource>
<!--since I am putting the configuration files in/web-inf/config/folder-
<!--so putsrc/main/resources Replace DAO web-inf/config/with file replaced by filter-
<directory>Src/main/resources</directory>
<targetPath>Web-inf/config</targetPath>
<filtering>True</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>Org.eclipse.jetty</groupId>
<artifactId>Jetty-maven-plugin</artifactId>
<version>9.3.0.m2</version>
<configuration>
<scanIntervalSeconds>6</scanIntervalSeconds>
<port>5004</port>
<webAppConfig>
<contextPath>/xxxx</contextPath>
<!--<defaultsdescriptor>${basedir}/src/main/resources/webdefault.xml</defaultsdescriptor>-- >
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>

3. Compare Web-inf/config under Aplicationcontent.xml (will be replaced later) and src/main/resources under Aplicationcontent.xml

<beanID= "Configproperties"
class= "Org.springframework.beans.factory.config.PropertiesFactoryBean">
<propertyname= "Locations">
<list>
<value>/web-inf/config/application_dev.properties</value>
</list>
</property>
</bean>

Vs

<beanID= "Configproperties"
class= "Org.springframework.beans.factory.config.PropertiesFactoryBean">
<propertyname= "Locations">
<list>
<!--${profiles.active} here placeholders are replaced by MVN with profile.active environment variables from the POM
in the third step, the Maven-war-plugin configuration replaces the file to complete the multi-environment switch-
<value>/web-inf/config/application_${profiles.active}.properties</value>
</list>
</property>
</bean>

4.maven compiled package mvn clean package-dmaven.test.skip=true -ptest

Specify-dmaven.test.skip=true to skip test-ptest Activate profile id=test environment parameters

Achieve results

Config to replace the original Application_dev.properties compiler with application_test.properties

This enables the loading of multi-environment configurations.

Concept Introduction

When you build a project, you may encounter different configurations in different environments such as testing (unit testing), development, simulation, production, and so on.
If you need to modify many and complex items, you should use Apache Maven's profile and Filtering features to resolve them.

Filtering function

Filtering is a feature of Maven resources Plugin, which replaces the value of the ${...} symbol in the resource file (*.properties,*.xml) with the values of the System properties or project properties. For example, if your system attribute has a "user.name=foobar", then the ${user.name} symbol in the resource file is automatically replaced with "Foobar" when Maven compiles.

Profile function

The role of profile is to allow you to define several profiles in a project file (pom.xml) and then select one of them at compile time to overwrite the original definition of the project file. Then the previous example, if we need to define different User.Name attribute values for the development environment and production environment, we create two properties files in the project directory:

Profile-development.properties, Content

User.name=foobar

Profile-production.properties, Content

User.name=tom

Then add the profile section in the project file (Pom.xml) as follows:

<build>
<filters>
<filter>Src/main/filters/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>Src/main/resources</directory>
<filtering>True</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>Develop</id>
<properties>
<env>Develop</env>
</properties>
</profile>
<profile>
<id>Test</id>
<properties>
<env>Test</env>
</properties>
</profile>
<profile>
<id>Product</id>
<properties>
<env>Product</env>
</properties>
</profile>
</profiles>
Pasting

When compiling a project, you can use the-p parameter

number specifies the ID of the profile that needs to be used, such as the following command will use development profile:

$MVN Clean compile-pdevelopment

If you want to use production profile, execute the following command:

$MVN Clean compile-pproduction

If you do not specify the-p parameter, an entry of activebydefault=true (that is, development) is used.

At this point, the filtering and profile capabilities enable you to use different configuration values for your development and production environments. Of course, profile can also allow you to add more definitions, such as adding different resource files for a particular profiles. In some large and medium-sized projects, different environments may simply modify the configuration value is not enough, may also need a configuration file for the entire replacement, then it should be specified in the Profiles/profile/build/resources section. Details can be found in the Appendix link.

http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/

Http://www.kafeitu.me/solution/2013/06/29/a-successful-cofiguration-file-management-solution.html

MAVEN profile for 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.