java - 如果未激活另一个配置文件,则激活 Maven 配置文件

问题与 Maven: Only activate profile A if profile B is not activated? 有关,但更具体。

如果我输入以下内容之一:

mvn clean install -PspecificProfile
mvn clean install -Dsmth -PspecificProfile
mvn clean install -Dsmth -PspecificProfile,anotherProfile

然后我想激活 specificProfile 配置文件。 (+额外指定的配置文件)

如果我输入其他类似的内容:

mvn install
mvn clean install
mvn clean install -Dsmth
mvn clean install -Dsmth -PanotherProfile
mvn clean install -Dsmth -PdefaultProfile
mvn clean install -Dsmth -PdefaultProfile,anotherProfile

然后我想激活defaultProfile(+额外指定的配置文件)。

想法:

if ( specific profile P is used via command line ) {
    activate P;
} else {
    activate the default profile;
}
activate other specified profiles;

示例:

mvn ...                          // default
mvn ... -PspecificProfile        // specificProfile           (no default!)
mvn ... -Px                      // default + x
mvn ... -Px,y                    // default + x + y
mvn ... -Px,specificProfile      // x + specificProfile       (no default!)
mvn ... -Px,specificProfile,y    // x + specificProfile + y   (no default!)

我试图做这样的事情(在 pom.xml 中):

<profile>
    <id>defaultProfile</id>
    <activation>
        <property>!x</property>
    </activation>
    ...
</profile>
<profile>
    <id>specificProfile</id>
    <properties>
        <x>true</x>
    </properties>
    ...
</profile>

但它不起作用。

最佳答案

当您调用 mvn ... -P x 时,配置文件 x 将是唯一 Activity 的配置文件。来自 maven 文档的原因:

  Profiles can be explicitly specified using the -P CLI option.
  This option takes an argument that is a comma-delimited list of profile-ids to
use. When this option is specified, no profiles other than those specified in
the option argument will be activated.

这里有一个解决方法:

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>!specific</name>
            </property>
        </activation>
    </profile>
    <profile>
        <id>specific</id>
        <activation>
            <property>
                <name>specific</name>
            </property>
        </activation>
    </profile>
    <profile>
        <id>x</id>
        <activation>
            <property>
                <name>x</name>
            </property>
        </activation>
    </profile>
    <profile>
        <id>y</id>
        <activation>
            <property>
                <name>y</name>
            </property>
        </activation>
    </profile>
</profiles>

命令:

mvn ...                        // default
mvn ... -Dspecific             // specific Profile         (no default!)
mvn ... -Dx                    // default + x
mvn ... -Dx -Dy                // default + x + y
mvn ... -Dx -Dspecific         // x + specific Profile     (no default!)
mvn ... -Dx -Dspecific -Dy     // x + specific Profile + y (no default!)

执行 mvn ... help:active-profiles 获取 Activity 配置文件的 id 列表。

https://stackoverflow.com/questions/25666473/

相关文章:

scala - 如何为 SBT 设置默认项目

build - 在构建服务器上使用 ramdisk 是否明智?

scala - SBT - 姓名和身份证有什么区别?

c++ - 在头文件与 .cpp 文件中编码 C++ (moSTLy)

build - Gitlab-CI:gitlab ci trigger build 仅用于合并请求

parameters - TeamCity 条件参数值

android - 如何加快 android ndk 构建

qt - Qt的配置有哪些不同的平台

Maven - 从外部属性文件中读取属性

ios - 带有 "Release: Fastest, Smallest [-Os]"的 Xcode