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--<ACTIVEBYDEFAULT&G Ttrue</activeByDefault> </activation> </profile> <profile> <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> <ID&G t;product</id> <properties> <env>product</env> </propertie s> </profile> </profiles> <build> <FINALNAME>MAVENIMPARITYPROFILE</FINALNAME&G T <filters><!--Get the filtered source files that have actual data--and <FILTER>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><! – be sure to point to the upper directory –> <includes> <! – files to be traversed must be written –> <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