如何在Maven中读取外部属性文件

时间:2009-05-11 18:23:57

标签: java build maven-2 properties-file

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

<properties file="x.properties"> 

</properties>

有一些关于此的讨论: Maven External Properties

3 个答案:

答案 0 :(得分:90)

答案 1 :(得分:50)

使用建议的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>

答案 2 :(得分:4)

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

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