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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/849389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:09:01  来源:igfitidea点击:

How to read an external properties file in Maven

javabuildmaven-2properties-file

提问by Dougnukem

Does anyone know how to read a x.properties file in Maven. I know there are ways to use resource filtering to read a properties file and set values from that, but I want a way in my pom.xml like:

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

<properties file="x.properties"> 

</properties>

There was some discussion about this: Maven External Properties

对此有一些讨论: Maven External Properties

采纳答案by Mike Pone

回答by Dougnukem

Using the suggested Maven properties plugin I was able to read in a buildNumber.properties file that I use to version my builds.

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

回答by Rich Seller

This answerto a similar question describes how to extend the properties plugin so it can use a remote descriptor for the properties file. The descriptor is basically a jar artifact containing a properties file (the properties file is included under src/main/resources).

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

The descriptor is added as a dependency to the extended properties plugin so it is on the plugin's classpath. The plugin will search the classpath for the properties file, read the file''s contents into a Properties instance, and apply those properties to the project's configuration so they can be used elsewhere.

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