深入浅析maven profile的激活

2020/2/20 8:09:38

本文主要是介绍深入浅析maven profile的激活,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

说起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的(请参阅 https://www.imooc.com/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)
点击查看更多内容


这篇关于深入浅析maven profile的激活的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程