33

いくつかの設定を含むMaven POMファイルがあり、プラグインのセクションには、次のような設定のmaven tomcatプラグインがあります。

<configuration>
   <url>http://localhost:8080/manager/html</url>
   <server>tomcat</server>
</configuration>

そのキーを使用して、たとえば tomcat.properties などのプロパティ ファイルに URL 設定をエクスポートしたいと思います。

url=http://localhost:8080/manager/html

また、POM ファイルでこのキーを読み取るにはどうすればよいですか?

4

3 に答える 3

36

Maven では、プロジェクトの POM でプロパティを定義できます。これは、次のような POM ファイルを使用して行うことができます。

<project>
    ...
    <properties>
        <server.url>http://localhost:8080/manager/html</server.url>
    </properties>
    ...
    <build>
        <plugins>
            <plugin>
            ...
                <configuration>
                    <url>${server.url}</url>
                    <server>tomcat</server>
                </configuration>
            ...
            </plugin>
        </plugins>
    </build>
</project>

タグ内でプロパティを指定することを避けproperties、コマンド ラインから次のように値を渡すことができます。

mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal

これらのプロパティをコマンド ラインから指定したくない場合や、これらのプロパティをプロジェクト POM からプロパティ ファイルにさらに分離する必要がある場合は、Properties Maven プラグインread-project-propertiesを使用して、その目的を実行する必要があります。 Maven ライフサイクルの初期化フェーズで。プラグイン ページの例をここに再現します。

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
           <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>etc/config/dev.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
于 2011-08-22T08:20:22.393 に答える
10

これらのプロパティはフィルタリングに使用できますが、pom ファイルでは使用できないため、受け入れられた回答の指示を使用してファイルからプロパティをロードすることは実際には不可能です。最小限のカウンターの例:

pom.xml で:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <executions>
        <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
        <execution>
          <!-- Apart from this test, the phase must be initialize -->
          <phase>validate</phase>
          <goals>
            <goal>read-project-properties</goal>
          </goals>
          <configuration>
            <files>
              <file>dev.properties</file>
            </files>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.6</version>
      <executions>
        <execution>
          <phase>validate</phase>
          <goals>
            <goal>run</goal>
          </goals>
          <configuration>
            <target>
              <echo>Displaying value of properties</echo>
              <echo>[foo] ${foo}</echo>
            </target>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

持っているdev.properties:

foo=bar

次に、コマンドを実行すると、次のmvn validate出力が生成されます。

 [echo] Displaying value of properties
 [echo] [foo] bar
于 2012-06-22T09:24:03.943 に答える