1. 概述
Maven Profiles 提供了一种机制,可以根据不同的构建环境或需求定制构建流程。例如,你可以为不同环境(开发、测试、生产)配置不同的依赖或插件,或者根据需要启用不同的测试策略。
本文将介绍如何声明和使用 Maven Profiles,并通过多个示例说明其典型用法。
2. 简单示例
通常情况下,执行 mvn package
会运行所有的单元测试。但在某些场景下,我们可能希望快速打包而不运行测试,比如验证部署包是否能正常运行。
我们可以创建一个名为 no-tests
的 profile,将 maven.test.skip
设置为 true
:
<profile>
<id>no-tests</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
然后通过如下命令启用该 profile:
mvn package -Pno-tests
这样构建时就会跳过测试阶段。当然,你也可以直接使用 -Dmaven.test.skip
参数,但这是为了演示 profile 的基本使用方式。
3. 声明 Profiles
你可以在 pom.xml
中声明多个 profiles,每个 profile 必须有唯一的 id
。
比如,我们定义两个 profile,一个用于集成测试,另一个用于突变测试:
<profiles>
<profile>
<id>integration-tests</id>
</profile>
<profile>
<id>mutation-tests</id>
</profile>
</profiles>
每个 profile 中可以配置:
- dependencies
- plugins
- resources
- finalName
这样,你就可以根据需要在不同的 profile 中启用不同的插件组合或资源过滤策略。
3.1 Profile 的作用范围
Profiles 可以定义在以下三个位置:
- 项目级别:写在
pom.xml
中,适用于当前项目 ✅ - 用户级别:写在用户目录下的
~/.m2/settings.xml
中 - 全局级别:写在 Maven 安装目录下的
conf/settings.xml
中
通常推荐将 profile 定义在 pom.xml
中,这样可以确保本地开发环境和 CI 构建环境一致,避免配置不一致带来的问题 ❌
4. 激活 Profiles
定义好 profile 后,下一步就是激活它们。
4.1 查看当前激活的 profile
可以使用如下命令查看当前激活的 profile:
mvn help:active-profiles
如果你没有激活任何 profile,输出将是:
The following profiles are active:
也可以将 maven-help-plugin
绑定到 compile
阶段,自动输出激活的 profile:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.2 使用 -P
参数激活
这是最常见的方式:
mvn package -P integration-tests
也可以激活多个 profile:
mvn package -P integration-tests,mutation-tests
4.3 默认激活
如果希望某个 profile 默认激活,可以设置 <activeByDefault>true</activeByDefault>
:
<profile>
<id>integration-tests</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
⚠️ 注意:如果你手动激活了其他 profile,这个默认 profile 将不会生效。
4.4 基于属性激活
你可以根据系统属性激活 profile:
<profile>
<id>active-on-property-environment</id>
<activation>
<property>
<name>environment</name>
</property>
</activation>
</profile>
激活命令如下:
mvn package -Denvironment
还可以根据属性值激活:
<property>
<name>environment</name>
<value>test</value>
</property>
此时激活命令为:
mvn package -Denvironment=test
也可以反向匹配:
<property>
<name>environment</name>
<value>!test</value>
</property>
4.5 基于 JDK 版本激活
你可以根据当前运行的 JDK 版本来激活 profile:
<profile>
<id>active-on-jdk-11</id>
<activation>
<jdk>11</jdk>
</activation>
</profile>
也支持版本范围,具体可参考 Maven Version Range Syntax
4.6 基于操作系统激活
Maven 支持根据操作系统信息激活 profile。你可以通过如下命令查看当前环境信息:
mvn enforcer:display-info
输出示例:
Maven Version: 3.5.4
JDK Version: 11.0.2 normalized as: 11.0.2
OS Info: Arch: amd64 Family: windows Name: windows 10 Version: 10.0
然后可以配置一个仅在 Windows 10 上激活的 profile:
<profile>
<id>active-on-windows-10</id>
<activation>
<os>
<name>windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
</profile>
4.7 基于文件是否存在激活
还可以根据文件是否存在来决定是否激活 profile:
<activation>
<file>
<missing>target/testreport.html</missing>
</file>
</activation>
上面这个配置表示当 target/testreport.html
文件不存在时才激活该 profile。
5. 禁用 Profile
有时候我们想禁用某个已经激活的 profile,可以使用 !
或 -
前缀:
mvn compile -P -active-on-jdk-11
这样就会禁用 active-on-jdk-11
这个 profile。
6. 总结
Maven Profiles 是一个非常强大的功能,允许我们根据不同的构建需求定制构建流程。
✅ 优点包括:
- 支持多种激活方式(命令行、属性、JDK、OS、文件)
- 可以定义在
pom.xml
、用户配置或全局配置中 - 支持默认激活和禁用操作
通过合理使用 profiles,我们可以优化构建流程,提升开发效率,避免重复配置。
如果你在 CI/CD 流程中使用 Maven,profiles 几乎是必备工具,强烈建议掌握其用法。