ChatGPT解决这个技术问题 Extra ChatGPT

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

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

<properties file="x.properties"> 

</properties>

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

如果您只有几个值,并且不同的用户需要不同的值,请考虑改为 putting the value in your settings.xml

k
kapex

试试 Properties Maven Plugin


我想这就是我正在寻找的我在 maven 存储库中找不到 1.0-SNAPSHOT 但有一个版本:mvnrepository.com/artifact/org.codehaus.mojo/… <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <版本>1.0-alpha-1</版本> </依赖>
当前版本:<groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <版本>1.0-alpha-2-SNAPSHOT</版本>来自snapshots.repository.codehaus.org
答案中的链接已更新为来自@JesseGlick 的新链接
我在 Windows 上遇到了这个插件的问题。如果有人也有问题,请改用 kuali
T
The Alchemist

使用建议的 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) 的错误
当我进入这个<plugin> <插件>下的部分</build> 之后在常规 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”。
C
Community

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

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