Maven: Profile(二)

profile简介

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,
然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。
比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;
或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,
linux下又是另外一套信息,等等。具体的激活条件有哪些我在后文会讲到。

profile的定义位置

我们可以有多个地方定义profile。定义的地方不同,它的作用范围也不同。

  1. 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。
  2. 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。
    该文件在用户家目录下的“.m2”目录下。
  3. 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的.

profile中能定义的信息

当profile定义在settings.xml中时意味着该profile是全局的,它会对所有项目或者某一用户的所有项目都产生作用。
因为它是全局的,所以在settings.xml中只能定义一些相对而言范围宽泛一点的配置信息,比如远程仓库等。
而一些比较细致一点的需要根据项目的不同来定义的就需要定义在项目的pom.xml中。
具体而言,能够定义在settings.xml中的信息有<repositories>、<pluginRepositories>和<properties>。
定义在<properties>里面的键值对可以在pom.xml中使用。

profile定义在pom.xml中

定义在pom.xml中的profile可以定义更多的信息。主要有以下这些:

<repositories>
<puginRepositories>
<dependencies>
<pugins>
<properties>
<dependencyManagement>
<distributionManagement>

还有buid元素下面的子元素,主要包括:

<defautGoa>
<resources>
<testResources>
<finaName>

profile的激活方式

使用activeByDefault设置激活

<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元素中指定激活条件,当没有显式指定profile,而设定activeByDefault为true的时,
意味着默认情况下该profile就会被激活。
例如,当我们调用mvn package的时候上面的profileTest1将会被激活,但是当我们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。

在settings.xml中使用activeProfiles指定处于激活状态的profile

<settings>
  ...
  <activeProfiles>
    <activeProfile>profile-1</activeProfile>
  </activeProfiles>
  ...
</settings>

使用-P参数显示的激活一个profile

TODO

根据环境来激活profile

link
Profiles can be automatically triggered based on the detected state of the build environment.
These triggers are specified via an <activation> section in the profile itself.
Currently, this detection is limited to prefix-matching of the JDK version,
the presence of a system property or the value of a system property.

查看当前处于激活状态的profile

command

mvn help:active-profiles
    原文作者:shiyang6017
    原文地址: https://segmentfault.com/a/1190000010363417
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞