从Maven POM文件中读取属性文件

36

我有一个包含一些配置的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 文件中读取这个键?

3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
37

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插件并在Maven生命周期的初始化阶段运行read-project-properties目标。该插件页面上的示例如下:

<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>

1
两个链接都失效了。 - user1531971
在运行时注入或提取Java类的属性值是否可能?我正在尝试各种方法来获取属性文件中键的值。 - MKod

13

完整的工作示例可在此处找到: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 作为构建工具的原因...


11

按照最佳答案中的说明,实际上无法从文件中加载属性,因为这些属性在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

这是因为在执行 initialize 之前,validate 已经被执行了。 - gavenkoa
@gavenkoa,你能解释一下我在那个字符串中失败的原因吗? - Daniel Thomas - drt24
请查看我的回答并阅读有关Maven生命周期顺序的内容:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 如果您在目标中更改phaseinitialize之后的任何阶段,您的示例将开始工作! - gavenkoa
@gavenkoa 已修复。为什么在您报告问题时没有立即修复呢? - JeanValjean
@JeanValjean 没有时间。我是在雇主支付的工作时间内发表这个评论的。 - gavenkoa

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