maven中配置多个profile会优先使用哪个

如题所述

profile简介

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


4.2     profile的定义位置

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

(1)    针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。

(2)    针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。

(3)    全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。



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

我们可以在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定义的先后顺序来进行覆盖取值的,然后后面定义的会覆盖前面定义的。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-29
推荐一个叫答案之家的网站,上面有推荐的查题系统,复制对应考试题的题目,然后粘贴到里面,系统会自动返回答案

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网