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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25666473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:25:54  来源:igfitidea点击:

Activate Maven profile if another profile is not activated

javamavenbuildmaven-3maven-profiles

提问by ROMANIA_engineer

The question is related to Maven: Only activate profile A if profile B is not activated?, but it's more specific.

问题与Maven有关:如果未激活配置文件B,则仅激活配置文件A?,但它更具体。

If I type one of the following:

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

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

then I want to activate the specificProfileprofile. (+the additional specified profiles)

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

If I type anything else like:

如果我输入其他内容,例如:

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

then I want to activate the defaultProfile(+the additional specified profiles).

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

Idea:

主意:

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

Examples:

例子:

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!)


I tried to do something like this (in pom.xml):

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

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

but it doesn't work.

但它不起作用。

回答by user2432405

Profile x will be the the only active profile when you call mvn ... -P x. Reason from the maven documentation:

当您调用 时,配置文件 x 将是唯一的活动配置文件mvn ... -P x。来自 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.

Here's a workaround:

这是一个解决方法:

<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>

The commands:

命令:

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!)

Execute mvn ... help:active-profilesto get the list of the ids of active profiles.

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