Use of profile in Maven2

Source: Internet
Author: User

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:

  1. <project>

  2. <modelversion>4.0.0</modelversion>

  3. <groupid>cc.mzone</groupid>

  4. <artifactid>myjar</artifactid>

  5. <version>0.1</version>

  6. <packaging>jar</packaging>

  7. <build>

  8. <resources>

  9. <resource>

  10. <directory>src/main/resources</directory>

  11. <includes>

  12. <include>* *</include>

  13. </includes>

  14. <filtering>true</filtering>

  15. </resource>

  16. </resources>

  17. </build>

  18. <properties>

  19. <jdbc.url>jdbc:mysql://localhost:3306/abc</jdbc.url>

  20. <jdbc.username>root</jdbc.username>

  21. <jdbc.password>root</jdbc.password>

  22. </properties>

  23. <profiles>

  24. <profile>

  25. <id>product</id>

  26. <properties>

  27. <jdbc.url>jdbc:mysql://localhost:3306/abc123</jdbc.url>

  28. <jdbc.username>rootuser</jdbc.username>

  29. <jdbc.password>rootpwd</jdbc.password>

  30. </properties>

  31. </profile>

  32. </profiles>

  33. </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:

  1. <resources>

  2. <resource>

  3. <directory>src/main/resources</directory>

  4. <includes>

  5. <include>* *</include>

  6. </includes>

  7. <filtering>true</filtering>

  8. </resource>

  9. </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:

    1. Jdbc.driver=com.mysql.jdbc.driver

    2. Jdbc.url=${jdbc.url}

    3. Jdbc.username=${jdbc.username}

    4. jdbc.password=${jdbc.password}

The following effects are performed:

    1. # # Use the default configuration information

    2. MVN Clean Package

    3. # # configuration information using the product environment

    4. 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

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.