1. 激活 profile 配置方式
在 Maven 中,可以选用如下的方式激活 profile。
1. 命令行激活
用户可以在 mvn 命令行中添加参数“-P”,指定要激活的 profile 的 id。如果一次要激活多个 profile,可以用逗号分开一起激活。例如:
mvn clean install -Pdev_env,test_evn
这个命令就同时激活了 id 为“dev_evn”和“test_evn”的两个 profile。
2. Settings 文件显示激活
如果希望某个 profile 默认一直处于激活状态,可以在 settings.xml 中配置 activeProfiles 元素,指定某个 profile 为默认激活状态,样例配置代码如下:
<settings>
...
<activeProfiles>
<activeProfile>dev_evn</activeProfile>
</activeProfiles>
...
</settings>
3. 系统属性激活
可以配置当某个系统属性存在时激活 profile,代码如下:
<profiles>
<profile>
...
<activation>
<property>
<name>profileProperty</name>
</property>
</activation>
</profile>
</profiles>
甚至还可以进一步配置某个属性的值是什么时候激活,例如:
<profiles>
<profile>
...
<activation>
<property>
<name>profileProperty</name>
<value>dev</value>
</property>
</activation>
</profile>
</profiles>
这样就可以在 mvn 中用“-D”参数来指定激活,例如:
Mvn clean install -DprofileProperty=dev 表示激活属性名称为 profileProperty,值为 dev 的 profile。
实际上这也是一种命令激活 profile 的方法,只是用的是“-D”参数指定激活的属性和值,而前面的是用的“-P”参数指定激活的 profile 的 id 而已。
4. 操作系统环境激活
用户可以通过配置指定不同操作系统的信息,实现不同操作系统做不同的构建。例如:
<profiles>
<profile>
<activation>
<os>
<name>Window XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
</profiles>
family 的值是 Windows、UNIX 或 Mac。name 为操作系统名称。arch为操作系统的架构。version为操作系统的版本。具体的值可以通过查看环境中的系统属性“os.name”“os.arch”和“os.version”获取。
5. 文件存在与否激活
当然,也可以通过配置判断某个文件存在与否来决定是否激活 profile,样例配置代码如下:
<profiles>
<profile>
<activation>
<file>
<missing>t1.properties</missing>
<exists>t2.properties</exists>
</file>
</activation>
</profile>
</profiles>
6. 默认激活
最后,还可以配置一个默认的激活 profile,例如:
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
需要注意的是,如果 pom 中有任何一个 profile 通过其他方式被激活的话,所有配置成默认激活的 profile 都会自动失效。
可以使用如下命令查看当前激活的 profile。
Mvn help:active-profiles
也可以使用如下命令查看所有的 profile。
Mvn help:all-profiles
7. 通过idea 勾选profiles选项
注意这种勾选,只对 通过idea的maven工具有效,对命令行无效。
通过IDEA官方文档(www.jetbrains.com/help/idea/2… 可知,默认激活的会用灰色的勾,勾选上。当取消灰色勾选时,则代表禁用这个profile. 比如setting.xml中配置了
<activeProfiles>
<activeProfile>nexus</activeProfile>**
</activeProfiles>
默认激活nexus,IDEA加载项目的时候,Profiles的nexus默认灰色的勾,当取消这个勾时,则是禁用uat:!uat,setting.xml中id为nexus的也不会生效。 需要注意的是,命令行输入: -P !uat,则报错。
这个部分见下面的实例进一步说明。
profile种类
1)pom.xml pom.xml 中声明的 profile 只对当前项目有效。
2)用户 settings.xml 在用户目录下的“.m2/settings.xml”中的 profile,对本机上的该用户的所有 Maven 项目有效。
3)全局 settings.xml 在 Maven 安装目录下 conf/settings.xml 中配置的 profile,对本机上所有项目都有效。
上面三种profile会合并在一块,优先合并全局 settings.xml、再合并用户 settings.xml,最后合并项目的pom.xml. 而且相同的标签以最后一个起作用。也就是相同的标签,后面的会覆盖前面的。表现就是 pom.xml优先级高于用户settings.xml高于全局settings.xml(在没有默认激活的情况下,见后面的实例说明)。
activeByDefault和activeProfiles的区别
activeByDefault
<activeByDefault>既能用在settings.xml也能用在pom.xml里
其作用maven官方文档是这么说的:
This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods.
也就是说配置了<activeByDefault>标签的profile只会在没有其他激活的profile的情况下被激活
activeProfiles
<activeProfiles>只能用在settings.xml文件
作用也看一下maven官方文档的说法:
The final piece of the settings.xml puzzle is the activeProfiles element. This contains a set of activeProfile elements, which each have a value of a profile id. Any profile id defined as an activeProfile will be active, regardless of any environment settings. If no matching profile is found nothing will happen. For example, if env-test is an activeProfile, a profile in a pom.xml (or profile.xml with a corresponding id will be active. If no such profile is found then execution will continue as normal.
在<activeProfiles>里配置的profile会无视其激活条件默认激活,如果指定的profile id不存在则忽略
实例说明
全局setting.xml配置(注意:更改全局配置或用户配置后,需要重启IDEA):
pom.xml配置:
取值:
命令行
- mvn clean package dev 结果
myName=abc_setting myAge=18 myHeight=185
- mvn clean package uat 结果
myName=abc_setting myAge=18 myHeight=185
如果 pom.xml中uat在上,dev在下:
- mvn clean package dev 结果
myName=abc_setting
myAge=18
myHeight=183
- mvn clean package uat 结果
myName=abc_setting
myAge=18
myHeight=185
结果分析:因为setting.xml配置了默认激活,所以scc.name和scc.age永远以setting.xml里的为准。同时激活了uat,当dev手动激活后,scc.height的值就以最后一个为准。
idea的maven工具
maven自带工具需要注意的是:uat默认是灰色的勾选
当uat灰色的勾去掉时,是禁用uat.setting.xml里的uat也不起作用了。
结果是:
myName=abc
myAge=30
myHeight=183
注意在命令行中输入 !uat会报错。