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

30
这个问题与Maven:仅在未激活配置文件B时激活配置文件A?相关,但更具体。
如果我输入以下内容之一:
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>

但是它不起作用。

1个回答

35

当你使用mvn ... -P x命令时,只有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.

这里有一个解决方法:

<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 命令以获取当前激活的配置文件的id列表。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接