25

質問は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>

しかし、うまくいきません。

4

1 に答える 1

30

を呼び出すと、プロファイル 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.

回避策は次のとおりです。

<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 のリストを取得します。

于 2014-09-16T01:43:42.277 に答える