One of the benefits of using MAVEN to manage your project is that you can use different compilation packaging settings for different environments to facilitate a multi-environment package deployment, generally we have at least two development environment and formal environment for the development project, the configuration information for both environments will be different, such as database configuration. We can use MAVEN's profile definition to differentiate, for example, we define the following fragment in the project's Pom file:
<project>
<modelversion>4.0.0</modelversion>
<groupid>cc.mzone</groupid>
<artifactid>myjar</artifactid>
<version>0.1</version>
<packaging>jar</packaging>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>* *</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/abc</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>root</jdbc.password>
</properties>
<profiles>
<profile>
<id>product</id>
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/abc123</jdbc.url>
<jdbc.username>rootuser</jdbc.username>
<jdbc.password>rootpwd</jdbc.password>
</properties>
</profile>
</profiles>
</project>
Here we define the relevant configuration of the database in the Pom file, define a profile with the ID of product, and also define the relevant configuration of the database in this profile. This allows us to use the default JDBC setting when we use the mvn Package Command When we use mvn package-p product When Maven automatically uses the database configuration in profile with ID product, this is the overwrite of Maven read property profiles.
Then look at the configuration of the Resources section in the Pom file:
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>* *</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
One of the most important is the <filtering>true</filtering> section, this configuration means filtering the placeholders in the specified properties file above, the placeholder is the form of the ${variable name} , Maven automatically reads the configuration file and then resolves the placeholder, replacing it with the attributes defined in the Pom file above. We can define a jdbc.properties configuration file under Src/main/resources, with the following content:
Jdbc.driver=com.mysql.jdbc.driver
Jdbc.url=${jdbc.url}
Jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
The following effects are performed:
# # Use the default configuration information
MVN Clean Package
# # configuration information using the product environment
MVN Clean Package -P product
After executing the above command in two times, and then looking at the packaged results under the target directory of the project, you can see that the contents of the Jdbc.properties file vary with the parameters of the package, thus enabling our multi-environment configuration to be packaged automatically.
Use of profile in Maven2