乐闻世界logo
搜索文章和话题

What is Maven Profile? How to use Profile to manage multi-environment configurations?

2月18日 21:35

Maven Profile is a mechanism provided by Maven to use different build configurations in different environments or conditions. Profile allows developers to define multiple sets of configurations and activate the corresponding configuration during the build based on conditions.

Profile Definition Locations:

  1. pom.xml: Project-level Profile, only valid for the current project
  2. settings.xml: User-level Profile, valid for all projects
  3. ~/.m2/settings.xml: Global-level Profile, valid for all users and projects

Profile Activation Methods:

  1. Command Line Activation: Use -P parameter to specify the activated Profile
bash
mvn clean install -Pdev mvn clean install -Pdev,test
  1. Environment Variable Activation: Judge through environment variables
xml
<profiles> <profile> <id>dev</id> <activation> <property> <name>env</name> <value>dev</value> </property> </activation> </profile> </profiles>
  1. JDK Version Activation: Automatically activate based on JDK version
xml
<activation> <jdk>11</jdk> </activation>
  1. Operating System Activation: Automatically activate based on operating system
xml
<activation> <os> <family>Windows</family> </os> </activation>
  1. File Existence Activation: Automatically activate based on whether a file exists
xml
<activation> <file> <exists>src/main/resources/dev.properties</exists> </file> </activation>

Common Use Cases for Profile:

  1. Multi-environment Configuration: Use different configurations for development, testing, and production environments
xml
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> <db.url>jdbc:mysql://localhost:3306/dev</db.url> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> <db.url>jdbc:mysql://prod-db:3306/prod</db.url> </properties> </profile> </profiles>
  1. Dependency Management: Use different dependency versions in different environments
  2. Plugin Configuration: Use different plugin configurations in different environments
  3. Resource Filtering: Filter different resource files based on Profile
xml
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>

Best Practices:

  • Define clear Profile names for different environments (dev, test, prod)
  • Use properties to uniformly manage environment-related configurations
  • Define common Profiles in parent POM, which can be inherited by submodules
  • Avoid defining too many configurations in Profile, keep it concise
  • When activating multiple Profiles with -P parameter, pay attention to configuration priority
  • Use Profile to automate builds for different environments in CI/CD processes

Common Commands:

  • mvn help:active-profiles: View currently activated Profiles
  • mvn help:all-profiles: View all available Profiles
  • mvn clean install -Pprofile1,profile2: Activate multiple Profiles
标签:Maven