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

Dou*_*kem 122 java maven-2 build properties-file

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

<properties file="x.properties"> 

</properties>
Run Code Online (Sandbox Code Playgroud)

有一些讨论: Maven外部属性

Mik*_*one 92

试试 Maven Properties插件

  • 当前链接:http://mojo.codehaus.org/properties-maven-plugin/read-project-properties-mojo.html (3认同)
  • 答案中的链接已更新为@JesseGlick的新链接 (2认同)

Dou*_*kem 54

使用建议的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>
Run Code Online (Sandbox Code Playgroud)

  • 你能展示buildNumber.properties文件的内部吗?谢谢! (8认同)

Ric*_*ler 6

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

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