135

有谁知道如何在 Maven 中读取 x.properties 文件。我知道有一些方法可以使用资源过滤来读取属性文件并从中设置值,但我想要在我的 pom.xml 中使用一种方法,例如:

<properties file="x.properties"> 

</properties>

对此进行了一些讨论: Maven外部属性

4

3 回答 3

100

试试 Properties Maven 插件

于 2009-05-11T18:40:58.867 回答
60

使用建议的 Maven 属性插件,我可以读取 buildNumber.properties 文件,该文件用于对构建进行版本控制。

  <build>    
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${basedir}/../project-parent/buildNumber.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
   </plugins>
于 2009-05-18T21:52:09.350 回答
6

This answer to a similar question描述了如何扩展属性插件,以便它可以使用属性文件的远程描述符。描述符基本上是一个包含属性文件的 jar 工件(属性文件包含在 src/main/resources 下)。

描述符被添加为扩展属性插件的依赖项,因此它位于插件的类路径中。该插件将在类路径中搜索属性文件,将文件的内容读入 Properties 实例,并将这些属性应用于项目的配置,以便它们可以在其他地方使用。

于 2009-09-20T13:04:58.060 回答