MAVEN publishes different configuration files in different environments

Source: Internet
Author: User

At development time, different environments will always use different configurations. such as local, test, pre-release, publish and other environments, such as the database are used to different configurations. If you change it manually, it will be very troublesome.

Fortunately, MAVEN provides the functionality to help us solve this problem.

We use different database configurations for different environments to illustrate

Directly on the code:

1.db.properties

Jdbc.username=${jdbc.username}jdbc.password=${jdbc.password}jdbc.url=${jdbc.url}name =${myname}

2.dev.properties

Jdbc.url=jdbc:mysql://127.0.0.1:3306/devdb?useunicode=true&characterencoding=utf8& Zerodatetimebehavior=converttonulljdbc.username=devuserjdbc.password=dev123456

3.product.properties

Jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useunicode=true&characterencoding=utf8& Zerodatetimebehavior=converttonulljdbc.username=productuserjdbc.password=product123456

4.test.properties

Jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useunicode=true&characterencoding=utf8& Zerodatetimebehavior=converttonulljdbc.username=testuserjdbc.password=test123456

5.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mavenImparityProfile</groupId> <artifactid>mavenimparityprofile </artifactId> <packaging>war</packaging> <version>1.0-snapshot</version> <name>mavenimparityprofile Maven webapp</name> <url>http://maven.apache.org</url><dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</arti Factid> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <properties> <env>test< /env><!--is equivalent to defining a variable for use below-<myName> Zhang San </myName><!--using a properties file that is undefined, but other places will change the value Volume-</properties> <activation><!--By default--&LT;ACTIVEBYDEFAULT&G Ttrue</activeByDefault> </activation> </profile> <profile> &LT;ID&G t;dev</id> <properties> <env>dev</env> <myName> John Doe &L T;/myname> </properties> <activation><!--By default activation-<activebydef Ault>false</activeByDefault> </activation> </profile> <profile> &LT;ID&G t;product</id> <properties> <env>product</env> </propertie s> </profile> </profiles> <build> &LT;FINALNAME&GT;MAVENIMPARITYPROFILE&LT;/FINALNAME&G      T <filters><!--Get the filtered source files that have actual data--and &LT;FILTER&GT;SRC/MAIN/RESOURCES/PROPERTIES/${ENV}.PROPERTIES&L        T;/filter> </filters> <!--specify all files and folders under Src/main/resources as resource files---<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!--whether to use filters--</resource> <!--the second way set to Dev.properties, and so on, that is, the ${in these files Key} will be replaced with a true value-<!--<resource> <directory>src/main/resources/properties/propert ies</directory>&lt;! &ndash; be sure to point to the upper directory &ndash;&gt; <includes> &lt;! &ndash; files to be traversed must be written &ndash;&gt; <include>product.properties</include> <include>test.properties</include>              <include>dev.properties</include> <include>db.properties</include> </includes> <filtering>true</filtering> </resource>--> </resources> </build></project>

The implementation is mainly implemented by configuring Frofile. 3 environments (TEST,DEV,PRODUCT) are configured above. The test environment is activated by default.

We directly execute the deploy using the test configuration.

If you want to use the product's configuration, use the MAVEN command MVN clean package-p product (note:-P to capitalize the parameter after-P is the ID of the different environment we defined earlier. If you are using the idea tool, you do not have to write mvn this parameter when configuring run

Appendix:

If you want to configure Jenkins, the other parameter configuration is the same, only need to modify the MAVEN command

Here is the release product environment

MAVEN publishes different configuration files in different environments

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.