简介
profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等。
位置
对于使用Maven3,我们可以有多个地方定义profile。定义的地方不同,它的作用范围也不同。
(1) 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。
(2) 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。
(3) 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。
激活方式
(1)在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该profile就默认会被激活。所以当我们调用mvn package的时候上面的profileTest1将会被激活,但是当我们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。
<profiles>
<profile>
<id>profileTest1</id>
<properties>
<hello>world</hello>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>profileTest2</id>
<properties>
<hello>andy</hello>
</properties>
</profile>
</profiles>
(2)使用activeProfiles指定处于激活状态的profile
在settings.xml或者pom.xml中设置:
<profiles>
<profile>
<id>profileTest1</id>
<properties>
<hello>world</hello>
</properties>
</profile>
<profile>
<id>profileTest2</id>
<properties>
<hello>andy</hello>
</properties>
</profile>
</profiles>
在settings.xml中定义activeProfiles
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles>
(3)使用-P参数显示的激活一个profile
(4)根据jdk来激活profile
<profiles>
<profile>
<id>profileTest1</id>
<jdk>1.5</jdk>
</profile>
<profiles>
除此外,还有操作系统、文件是否存在、系统属性。若是系统属性,激活样例如mvn package –Dhello=world
查看当前处于激活状态的profile
mvn help:active-profiles
参考: