33

我有一些配置的 Maven POM 文件,在插件部分,我有一些配置如下的 maven tomcat 插件:

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

我想将 url 设置导出到某个属性文件,例如带有该键的 tomcat.properties:

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 回答
12

完整的工作示例在:http ://hg.defun.work/exp/file/tip/maven/properties

这是pom.xml的重要部分:

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
      <execution>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <files>
        <file>dev.properties</file>
      </files>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target>
            <echo>project.build.sourceEncoding is "${project.build.sourceEncoding}"</echo>
            <echo>foo is "${foo}"</echo>
            <echo>with-spaces is "${with-spaces}"</echo>
            <echo>existent.property is "${existent.property}"</echo>
            <echo>nonexistent.property is "${nonexistent.property}"</echo>
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

如您所见,properties-maven-plugin仍处于alpha阶段,这就是为什么我讨厌将 Maven 作为构建工具...

于 2013-04-18T22:25:36.420 回答
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 回答