【问题标题】:How to read an external properties file in Maven如何在 Maven 中读取外部属性文件
【发布时间】:2010-10-25 08:44:52
【问题描述】:

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

<properties file="x.properties"> 

</properties>

对此进行了一些讨论: Maven External Properties

【问题讨论】:

标签: java build maven-2 properties-file


【解决方案1】:

【讨论】:

【解决方案2】:

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

【讨论】:

  • 你能显示 buildNumber.properties 文件的内部吗?谢谢!
  • 感谢您的工作示例。但是,为什么我收到Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize) 的错误
  • 当我在 之后和常规 maven 插件之前进入 下的这个 部分时,我收到此错误:Plugin 'execution' not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)
  • @BorisBrodski 你能显示 buildNumber.properties 文件的内部吗?您似乎删除了一些细节。
  • @MoustafaMahmoud 为什么是我?这不是我的答案 :) 但我可以推测,它可能看起来像“my.great.product.version=1.0.0”。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2011-08-10
    • 2013-01-20
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多