Maven Profile精讲
一个项目通常都会有多个不同的运行环境,例如开发环境,测试环境、生产环境等。而不同环境的构建过程很可能是不同的,例如数据源配置、插件、以及依赖的版本等。每次将项目部署到不同的环境时,都需要修改相应的配置,这样重复的工作,不仅浪费劳动力,还容易出错。为了解决这一问题,Maven 引入了 Profile 的概念,通过它可以为不同的环境定制不同的构建过程。
Profile 的类型
Profile 可以分为 3 个类型,它们的作用范围也各不相同。
类型 | 位置 | 有效范围 |
---|---|---|
Per Project | Maven 项目的 pom.xml 中 | 只对当前项目有效 |
Per User | 用户主目录(%USER_HOME%)/.m2/settings.xml 中 | 对本机上该用户所有 Maven 项目有效 |
Global | Maven 安装目录(%MAVEN_HOME%)/conf/settings.xml 中 | 对本机上所有 Maven 项目有效 |
声明 Profile
Maven 通过 profiles 元素来声明一组 Profile 配置,该元素下可以包含多个 profile 子元素,每个 profile 元素表示一个 Profile 配置。每个 profile 元素中通常都要包含一个 id 子元素,该元素是调用当前 Profile 的标识。定义 Profile 的一般形式如下:
<profiles>
<profile>
<id>profile id</id>
....
</profile>
<profile>
<id>profile id</id>
....
</profile>
</profiles>
除此之外,Profile 中还可以声明一些其他的 POM 元素,但不同位置的 Profile 所能声明的 POM 元素也是不同的。
1. 在 pom.xml 中声明的 Profile,由于其能够随着 pom.xml 一起存在,它被提交到代码仓库中,被 Maven 安装到本地仓库或远程仓库中,所以它能够修改或增加很多 POM 元素,其中常用的元素如下表。
一级 | 二级 | |
---|---|---|
project | repositories |
|
pluginRepositories |
| |
dependencies |
| |
plugins |
| |
dependencyManagement |
| |
distributionManagement |
| |
modules |
| |
properties |
| |
reporting |
| |
build | plugins | |
defaultGoal | ||
resources | ||
testResources | ||
directory | ||
filters | ||
finalName | ||
pluginManagement | ||
filters |
2. 在 setting.xml 中声明的 Profile 是无法保证能够随着 pom.xml 一起被分发的,因此 Maven 不允许用户在该类型的 Profile 修改或增加依赖或插件等配置信息,它只能声明以下范围较为宽泛的元素。
repositories:仓库配置。
pluginRepositories:插件仓库配置。
properties:键值对,该键值对可以在 pom.xml 中使用。
激活 Profile
Profile 能够在项目构建时,修改 POM 中配置或者添加一些额外的配置元素。用户可以通过多种方式激活 Profile,以实现不同环境使用不同的配置,执行不同的构建过程。Profile 可以通过以下 6 种方式激活:
令行激活
settings.xml 文件显示激活
系统属性激活
操作系统环境激活
文件存在与否激活
默认激活
下面我们以一个 Maven 项目为例,分别对以上 6 种激活方式进行介绍。
准备 Maven 项目
1. 创建一个名为 maven 的项目,并在其 src/main/resources 目录下有 3 个环境 properties 配置文件。
env.properties:默认配置文件
env.test.properties:测试环境配置文件
env.prod.properties:生产环境配置文件
2. 该项目目录结构如图 1 所示。
图1:示例项目的目录结构
3. 在 pom.xml 中定义三个不同的 Profile,将 maven-antrun-plugin:run 目标绑定到 default 生周期的 test 阶段上,以实现在不同的 Profile 中进行不同的操作。
<project xmlns="http://maven.apache网站站点" rel="nofollow" />
mvn clean install -Ptest1,test2
打开令行窗口,跳转到 pom.xml 所在的目录,执行以下 mvn 令,激活 id 为 test 的 Profile。
mvn clean test -Ptest
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO] Deleting D:\eclipse workSpace 3\maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.test.properties,将其配置信息到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.968 s
[INFO] Finished at: 2021-03-02T09:28:49+08:00
[INFO] ------------------------------------------------------------------------
settings.xml 文件显示激活
在本地仓库的 settings.xml 文件中添加如下配置,激活指定的 Profile。
<activeProfiles>
<activeProfile>test</activeProfile>
</activeProfiles>
Maven 本地仓库的 settings.xml 文件,默认位于 %USER_HOME%/.m2 目录下。
打开令行窗口,跳转到 pom.xml 所在的目录下,执行以下 mvn 令。
mvn clean test
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO] Deleting D:\eclipse workSpace 3\maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.test.properties,将其配置信息到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.012 s
[INFO] Finished at: 2021-03-02T09:56:59+08:00
[INFO] ------------------------------------------------------------------------
系统属性激活
用户可以配置当某个系统属性存在时,激活指定的 Profile。例如,我们在 id 为 prod 的 profile 元素中添加以下配置,表示当系统属性 user 存在,且值等于 prod 时,自动激活该 Profile。<activation>
<property>
<name>user</name>
<value>prod</value>
</property>
</activation>
打开令行窗口,跳转到 pom.xml 所在的目录。执行令,其中参数 -D 选项用来指定系统临时属性。
mvn clean test -Duser=prod
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO] Deleting D:\eclipse workSpace 3\maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.prod.properties,将其配置信息到 D:\eclipse workSpace
[echo] 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.036 s
[INFO] Finished at: 2021-03-02T10:23:19+08:00
[INFO] ------------------------------------------------------------------------
操作系统环境激活
Maven 还可以根据操作系统环境自动激活指定的 Profile。例如,将以下配置(本地计算机操作系统环境信息)添加到 pom.xml 文件中的 id 为 normal 的 Profile 中。
<activation>
<os>
<name>Windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
打开令行窗口,跳转到 pom.xml 所在的目录下,执行以下 mvn 令。
mvn clean test
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO] Deleting D:\eclipse workSpace 3\maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.properties,将其配置信息到 D:\eclipse workSpace
[echo] 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.025 s
[INFO] Finished at: 2021-03-02T10:46:12+08:00
[INFO] ------------------------------------------------------------------------
文件存在与否激活
Maven 可以根据某些文件存在与否,来决定是否激活 Profile。例如,在 id 为 prod 的 Profile 中添加以下配置,该配置表示当 env.prod.properties 文件存在,且 env.test.properties 文件不存在时,激活该 Profile。
<activation>
<file>
<exists>./src/main/resources/env.prod.properties</exists>
<missing>./src/main/resources/env.test.properties</missing>
</file>
</activation>
将 env.test.properties 从项目中删除,打开令行窗口,跳转到 pom.xml 所在目录,执行以下令。
mvn clean test
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\eclipse workSpace4\maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace4\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.prod.properties,将其配置信息到 D:\eclipse workSpace
[echo] 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace4\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.197 s
[INFO] Finished at: 2021-04-21T15:12:59+08:00
[INFO] ------------------------------------------------------------------------
默认激活
我们可以在声明 Profile 时,指定其默认激活。例如,在 id 为 normal 的 Profile 中添加以下配置,指定该 Profile 默认激活。<activation>
<activeByDefault>true</activeByDefault>
</activation>
打开令行窗口,跳转到 pom.xml 所在目录,执行以下令。
mvn clean test
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< net.biancheng.www:maven >-----------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
[INFO] Deleting D:\eclipse workSpace4\maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\eclipse workSpace4\maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
[INFO] Surefire report directory: D:\eclipse workSpace4\maven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
[INFO] Executing tasks
[echo] 使用 env.prod.properties,将其配置信息到 D:\eclipse workSpace
[echo] 3\maven\target\classes\user.properties 中
[copy] Copying 1 file to D:\eclipse workSpace4\maven\target\classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.986 s
[INFO] Finished at: 2021-04-21T15:24:59+08:00
[INFO] ------------------------------------------------------------------------