问题
有谁知道如何在Maven中读取x.properties文件。我知道有一些方法可以使用资源过滤来读取属性文件并从中设置值,但我想在我的pom.xml中使用以下方法:
<properties file="x.properties">
</properties>
有一些讨论:Maven External Properties
#1 热门回答(84 赞)
#2 热门回答(47 赞)
使用建议的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>
#3 热门回答(4 赞)
Thisanswer对类似的问题描述了如何扩展属性插件,以便它可以使用属性文件的远程描述符。描述符基本上是一个包含属性文件的jar工件(属性文件包含在src / main / resources下)。
描述符作为依赖项添加到扩展属性插件中,因此它位于插件的类路径中。该插件将在类路径中搜索属性文件,将文件的内容读入Properties实例,并将这些属性应用于项目的配置,以便在其他地方使用它们。