是的,这是可能的。但是您似乎首先对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
使用此解决方案,您可以使用多个配置文件 ID 调用 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。