亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

深入淺析maven profile的激活

標簽:
Java

说起maven的profile,相信大家都不会陌生,可以根据不同的情况激活不同的profile,达到灵活配置的目的。这里给大家简单介绍一下maven profile激活的相关知识。

怎么知道哪些profile激活了

运行mvn help:active-profiles命令即可。例如有如下的pom文件

    <profiles>
        <profile>
            <id>bar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>foo</id>
        </profile>
    </profiles>

运行结果为:

The following profiles are active:

 - bar (source: org.example:maven:1.0-SNAPSHOT)

activeByDefault VS activeProfiles

activeProfiles元素出现在settings文件中,它指定的profile一定会激活。
例如在settings文件中有如下内容:

	<profiles>
		<profile>
			<id>foo</id>
		</profile>
		<profile>
			<id>dev</id>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>dev</activeProfile>
	</activeProfiles>

运行mvn help:active-profiles结果为:

The following profiles are active:

 - dev (source: external)

运行mvn -P foo help:active-profiles结果为:

The following profiles are active:

 - foo (source: external)
 - dev (source: external)

而当一个标记了activeByDefault的profile出现在pom中时,如果同一个pom文件中其他profile没有激活,则该profile激活,其他profile有激活的,则该profile不激活。例如在pom文件中有如下内容:

    <profiles>
        <profile>
            <id>bar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>foo</id>
        </profile>
    </profiles>

运行mvn help:active-profiles结果为:

The following profiles are active:

 - bar (source: org.example:maven:1.0-SNAPSHOT)

运行mvn -P foo help:active-profiles结果为:


 - foo (source: org.example:maven:1.0-SNAPSHOT)

当然了,activeByDefault也能出现在settings文件中,但是官方文档也没支持这种情况下,其表现形式是怎样的。

properties激活profile

我们已经知道,pom文件中的properties是无法激活它自己的profile的(请参阅 http://www.xianlaiwan.cn/article/299915 ),然而可以在settings文件中的profile里设置properties,进而激活pom中的profile。
例如在settings文件中这样设置:

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <abc>xyz</abc>
            </properties>
        </profile>
    </profiles>

	<activeProfiles>
		<activeProfile>dev</activeProfile>
	</activeProfiles>

在pom中这样写:

   <profiles>
       <profile>
           <id>foo</id>
           <activation>
               <property>
                   <name>abc</name>
                   <value>xyz</value>
               </property>
           </activation>
       </profile>
   </profiles>

运行mvn help:active-profiles,可以看到,foo这个profile被激活了

The following profiles are active:

 - dev (source: external)
 - foo (source: org.example:maven:1.0-SNAPSHOT)
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消