profile可让咱们定义一系列的配置信息,而后指定其激活条件。这样咱们就能够定义多个profile,而后每一个profile对应不一样的激活条件和配置信息,从而达到不一样环境使用不一样配置信息的效果。好比说,咱们能够经过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5如下使用另一套配置信息;或者有时候咱们能够经过操做系统的不一样来使用不一样的配置信息,好比windows下是一套信息,linux下又是另一套信息,等等。具体的激活条件有哪些我在后文会讲到。linux
对于使用Maven3,咱们能够有多个地方定义profile。定义的地方不一样,它的做用范围也不一样。windows
(1) 针对于特定项目的profile配置咱们能够定义在该项目的pom.xml中。测试
(2) 针对于特定用户的profile配置,咱们能够在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。ui
(3) 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。spa
profile中可以定义的配置信息跟profile所处的位置是相关的。如下就分两种状况来讨论,一种是定义在settings.xml中,另外一种是定义在pom.xml中。操作系统
当profile定义在settings.xml中时意味着该profile是全局的,它会对全部项目或者某一用户的全部项目都产生做用。由于它是全局的,因此在settings.xml中只能定义一些相对而言范围宽泛一点的配置信息,好比远程仓库等。而一些比较细致一点的须要根据项目的不一样来定义的就须要定义在项目的pom.xml中。具体而言,可以定义在settings.xml中的信息有<repositories>、<pluginRepositories>和<properties>。定义在<properties>里面的键值对能够在pom.xml中使用。orm
定义在pom.xml中的profile能够定义更多的信息。主要有如下这些:xml
<repositories>ci
<pluginRepositories>get
<dependencies>
<plugins>
<properties>
<dependencyManagement>
<distributionManagement>
还有build元素下面的子元素,主要包括:
<defaultGoal>
<resources>
<testResources>
<finalName>
Maven给咱们提供了多种不一样的profile激活方式。好比咱们可使用-P参数显示的激活一个profile,也能够根据环境条件的设置让它自动激活等。下面将对它们一一进行介绍:
先看下面一个配置
<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>
咱们能够在profile中的activation元素中指定激活条件,当没有指定条件,而后指定activeByDefault为true的时候就表示当没有指定其余profile为激活状态时,该profile就默认会被激活。因此当咱们调用mvn package的时候上面的profileTest1将会被激活,可是当咱们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。
咱们能够在settings.xml中使用activeProfiles来指定须要激活的profile,这种方式激活的profile将全部状况下都处于激活状态。好比如今咱们定义了以下两个profile
<profiles>
<profile>
<id>profileTest1</id>
<properties>
<hello>world</hello>
</properties>
</profile>
<profile>
<id>profileTest2</id>
<properties>
<hello>andy</hello>
</properties>
</profile>
</profiles>
这里的profile能够是定义在settings.xml中的,也能够是定义在pom.xml中的。这个时候若是咱们须要指定profileTest1为激活状态,那么咱们就能够在settings.xml中定义activeProfiles,具体定义以下:
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles>
考虑这样一种状况,咱们在activeProfiles下同时定义了多个须要激活的profile。这里还拿上面的profile定义来举例,咱们定义了同时激活profileTest1和profileTest2。
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
<activeProfile>profileTest2</activeProfile>
</activeProfiles>
从profileTest1和profileTest2咱们能够看出它们共同定义了属性hello。那么这个时候我在pom.xml中使用属性hello的时候,它到底取的哪一个值呢?是根据activeProfile定义的顺序,后面的覆盖前面的吗?根据个人测试,答案是非也,它是根据profile定义的前后顺序来进行覆盖取值的,而后后面定义的会覆盖前面定义的。
假设咱们如今有以下定义的profiles
<profiles>
<profile>
<id>profileTest1</id>
<properties>
<hello>world</hello>
</properties>
</profile>
<profile>
<id>profileTest2</id>
<properties>
<hello>andy</hello>
</properties>
</profile>
<profiles>
那么当咱们在进行Maven操做时就可使用-P参数显示的指定当前激活的是哪个profile了。好比咱们须要在对项目进行打包的时候使用id为profileTest1的profile,咱们就能够这样作:
mvn package –P profileTest1
当咱们使用activeByDefault或settings.xml中定义了处于激活的profile,可是当咱们在进行某些操做的时候又不想它处于激活状态,这个时候咱们能够这样作:
Mvn package –P !profileTest1
这里假设profileTest1是在settings.xml中使用activeProfile标记的处于激活状态的profile,那么当咱们使用“-P !profile”的时候就表示在当前操做中该profile将不处于激活状态。
profile一个很是重要的特性就是它能够根据不一样的环境来激活,好比说根据操做系统的不一样激活不一样的profile,也能够根据jdk版本的不一样激活不一样的profile,等等。
<profiles>
<profile>
<id>profileTest1</id>
<jdk>1.5</jdk>
</profile>
<profiles>
上面状况表示在jdk为1.5版本系列的时候激活profileTest1。
<profiles>
<profile>
<id>profileTest1</id>
<jdk>[1.4,1.7)</jdk>
</profile>
<profiles>
上面的状况表示在jdk为1.4、1.5和1.6的时候激活profileTest1。
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
</profiles>
上面的状况就是根据操做系统的类型来激活profileTest1。
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<property>
<name>hello</name>
<value>world</value>
</property>
</activation>
</profile>
</profiles>
上面的profileTest1将在提供了系统属性hello,而且其值为world的时候激活。下面的作法能够激活profileTest1。
mvn package –Dhello=world
当是下面的这种定义形式时,profileTest1将在指定了系统属性hello,且其值为任意值的时候被激活。
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<property>
<name>hello</name>
</property>
</activation>
</profile>
</profiles>
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<file>
<exists>target</exists>
</file>
</activation>
</profile>
</profiles>
上面的定义表示当存在target文件时激活profileTest1。
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<file>
<missing>target</missing>
</file>
</activation>
</profile>
</profiles>
上面的定义表示当不存在target文件时激活profileTest1。
咱们能够同时定义多个profile,那么在创建项目的过程当中,到底激活的是哪个profile呢?Maven为咱们提供了一个指令能够查看当前处于激活状态的profile都有哪些,这个指定就是mvn help:active-profiles。
如今假设咱们的settings.xml文件中有以下profile的定义:
<profiles>
<profile>
<id>profileTest1</id>
<activation>
<file>
<missing>target</missing>
</file>
</activation>
</profile>
</profiles>
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles>
这个时候咱们能够看到,咱们已经定义了profileTest1始终为激活状态,这个时候咱们使用mvn help:active-profiles查看处于激活状态的profile时,就会打印出以下内容: