如果未激活其他配置文件,则激活Maven配置文件

ROM*_*eer 19 java build maven-3 maven maven-profiles

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

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

mvn clean install -PspecificProfile
mvn clean install -Dsmth -PspecificProfile
mvn clean install -Dsmth -PspecificProfile,anotherProfile
Run Code Online (Sandbox Code Playgroud)

然后我想激活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
Run Code Online (Sandbox Code Playgroud)

然后我想激活defaultProfile(+其他指定的配置文件).

理念:

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

例子:

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!)
Run Code Online (Sandbox Code Playgroud)

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

<profile>
    <id>defaultProfile</id>
    <activation>
        <property>!x</property>
    </activation>
    ...
</profile>
<profile>
    <id>specificProfile</id>
    <properties>
        <x>true</x>
    </properties>
    ...
</profile>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

use*_*405 23

个人资料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.
Run Code Online (Sandbox Code Playgroud)

这是一个解决方法:

<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>
Run Code Online (Sandbox Code Playgroud)

命令:

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!)
Run Code Online (Sandbox Code Playgroud)

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