说起maven的profile,相信大家都不会陌生,可以根据不同的情况激活不同的profile,达到灵活配置的目的。这里给大家简单介绍一下maven profile激活的相关知识。
怎么知道哪些profile激活了运行mvn help:active-profiles命令即可。例如有如下的pom文件
bar
true
foo
运行结果为:
The following profiles are active: - bar (source: org.example:maven:1.0-SNAPSHOT)activeByDefault VS activeProfiles
activeProfiles元素出现在settings文件中,它指定的profile一定会激活。
例如在settings文件中有如下内容:
foo
dev
dev
运行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文件中有如下内容:
bar
true
foo
运行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文件中这样设置:
dev
xyz
dev
在pom中这样写:
foo
abc
xyz
运行mvn help:active-profiles,可以看到,foo这个profile被激活了
The following profiles are active: - dev (source: external) - foo (source: org.example:maven:1.0-SNAPSHOT)



