是的,这是可能的。但似乎你首先对how profiles are activated感到困惑。
命令
mvn package -Denvironment=dev
将不激活而无需进一步配置任何轮廓。在你的情况下,它的工作原理,因为必须有在你的POM配置文件定义,是由系统属性environment
的存在与dev
值激活。你的配置看起来像:
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>dev</value>
</property>
</activation>
</profile>
</profiles>
这是使配置文件时,你通过系统属性-Denvironment
激活魔法。考虑到这一点,您可以使用相同的想法激活多个配置文件:声明多个由存在系统属性激活的<profile>
元素。
<profiles>
<profile>
<activation>
<property>
<name>myAwesomeProperty1</name>
<value>true</value>
</property>
</activation>
</profile>
<profile>
<activation>
<property>
<name>myAwesomeProperty2</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
上面配置将激活两个轮廓如果myAwesomeProperty1
和myAwesomeProperty2
是系统属性具有值true
。
不过在这个特殊情况下,你想要的是根据你的环境激活一个版本,所以基于-P
命令行开关而不是系统属性来激活这些配置文件也许是个好主意。
从Introduction to Build Profiles:
配置文件可以使用-P
CLI选项中明确指定。
这个选项需要一个参数,这是一个逗号分隔的配置文件的ID列表使用。当指定了此选项时,在选项参数中指定的配置文件(一个或多个)将除了其通过它们的活化构型或settings.xml
的<activeProfiles>
部分活化的任何配置文件被激活。
mvn groupId:artifactId:goal -P profile-1,profile-2
有了这个解决方案,您可以调用Maven的多档编号。也就是说,如果你在你的配置
<profiles>
<profile>
<id>profile-1</id>
<!-- rest of config -->
</profile>
<profile>
<id>profile-2</id>
<!-- rest of config -->
</profile>
</profiles>
以上调用将同时激活profile-1
和profile-2
。
您没有通过-D指定配置文件,您可以通过-P指定配置文件,是的,您可以有多个配置文件-Pprofile1,profile2。在你的情况下,配置文件可能是通过属性(-D)激活的,那么你可以在同一属性(仍然通过-D),两个属性(-Dprop1 -Dprop2)或通过-P明确地激活两个配置文件 –
Take看看[这](http://stackoverflow.com/questions/18152831/building-multiple-maven-profiles-for-a-single-jenkins-job),它可能会帮助你 – haihui