4

これは、Maven のビルド プロファイルを使用する最初の日です。次のファイルにプロファイルがあります。

  1. pom.xml
  2. Maven 設定 (%USER_HOME%/.m2/settings.xml)

好奇心のために、同じ id(local_deploy) を持つ両方のファイルに 1 つのプロファイルを作成しました。違いは 1 つのプロパティ (例: tomcat.pwd) だけです。

POM でのプロファイルは次のようになります。

<profile>
        <id>local_deploy</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <tomcat.host>localhost</tomcat.host>
            <tomcat.port>8080</tomcat.port>
            <tomcat.url>http://${tomcat.host}:${tomcat.port}/manager/text</tomcat.url>
            <tomcat.user>admin</tomcat.user>
            <tomcat.pwd>admin</tomcat.pwd>
        </properties>
    </profile>

Maven 設定のプロファイルは次のようになります。

<profile>
    <id>local_deploy</id>
    <properties>
      <tomcat.host>localhost</tomcat.host>
      <tomcat.port>8080</tomcat.port>
      <tomcat.url>http://${tomcat.host}:${tomcat.port}/manager/text</tomcat.url>
      <tomcat.user>admin</tomcat.user>
      <tomcat.pwd>wrongpwd</tomcat.pwd>
    </properties>
  </profile>

Maven 設定のプロファイルは に記載されていないことに注意してください<activeProfiles>

次のコマンドを使用してアプリケーションをインストールしようとすると

mvn clean install -P local_deploy help:active-profiles

アプリケーションがデプロイされ、コンソールに次の出力が表示されます。

The following profiles are active:
local_deploy (source: external)
local_deploy (source: <my groupId>:<my artifactId><version>)

私はこのドキュメントを読んでいますが、それは言っています

Take note that profiles in the settings.xml takes higher priority than profiles in the POM

したがって、Maven 設定のパスワードが正しくないために、デプロイが失敗したはずだと思います。ここで何が欠けていますか?

4

1 に答える 1

4

これが私が使用したサンプルポンです:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>profiles-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>print-hello</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <property name="msg" value="${hello}" />
                                <property name="msg2" value="${hello2}" />
                                <echo message="hello from build: ${msg}, ${msg2}" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>p2</id>
            <properties>
                <hello>from-pom</hello>
                <hello2>from-pom-again</hello2>
            </properties>
        </profile>
    </profiles>
</project>

私の設定で定義する:

<profile>
    <id>p2</id>
    <properties>
        <hello>from-settings</hello>
    </properties>
</profile>

したがって、注: POM と設定で同じ名前の 2 つのプロファイルが同じhelloプロパティを定義しています。ただし、POM のものは 1 つの追加プロパティを定義しますhello2

次に、次を実行します。

mvn test -Pp2 help:active-profiles

私はビルド出力の一部として得ました:

[INFO] --- maven-antrun-plugin:1.5:run (print-hello) @ profiles-sample ---
[INFO] Executing tasks

main:
     [echo] hello from build: from-settings, from-pom-again
[INFO] Executed tasks
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building profiles-sample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.2:active-profiles (default-cli) @ profiles-sample ---
[INFO] 
Active Profiles for Project 'com.sample:profiles-sample:jar:0.0.1-SNAPSHOT': 

The following profiles are active:

 - p2 (source: external)
 - p2 (source: com.sample:profiles-sample:0.0.1-SNAPSHOT)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

そのため、Maven Help Plugin から、両方のプロファイルがアクティブであることが実際にわかります。これは、Antrun の一部として両方のプロパティを取得したためです (hello設定hello2のプロファイルと pom のプロファイルから)。
したがって、2 つのプロファイルが同時にアクティブになり、それらのプロパティがマージされ (hello同じ名前を共有しているため)、設定のプロパティが POM のプロパティよりも優先され、POM の追加のプロパティが次のように正しく取得されました。良い。

そのため、あなたが言及したシナリオを再現できませんでした。設定と pom を再確認し、操作するプロパティを追加することをお勧めします。

于 2015-12-16T16:33:49.687 に答える