通过属性文件指定 pom 属性?

Eri*_* B. 4 java properties maven

由于我的构建系统的设计方式(RTC 构建引擎),我想通过属性文件为 maven 提供属性值,而不是为每个属性指定 -Dkey=value。

我发现了几个关于 SO 的问题(How to set build properties from a file in Maven POM? and How to read an external properties file in Maven)与这个问题完全相关,但它们相对较旧,并且都需要自定义插件工作(处于 alpha 状态)。

我意识到像这样将参数传递给 Maven 可能不是最好的解决方案,但另一个选项是通过 -D 设置在命令行上指定所有内容,这也不理想。

此外,鉴于此属性文件仅由构建引擎(而不是个人开发人员)真正使用,我并不真正相信它属于 pom.xml 文件。但是我找不到任何其他机制可以让我指定要使用的插件 - settings.xml 不允许指定插件。

在这种情况下,我唯一的选择是使用插件并在项目 pom 中指定它吗?

Joe*_*Joe 7

在 pom 中,您可以放置​​...

<properties>
    <core-version>1234</core-version>
    <lib-version>1234</lib-version>
    <build-version>9999</lib-version>
    <build-date>20150101</build-date>
</properties>
Run Code Online (Sandbox Code Playgroud)

具有您需要的所有属性。

或者你可以使用...

<build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>dev.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Run Code Online (Sandbox Code Playgroud)

并且该文件dev.properties将包含属性

core-version=1234
lib-version=1234
build-version=9999
build-date=20150101
...
Run Code Online (Sandbox Code Playgroud)

或者......你可以使用settings.xml文件注入属性,如在这里

您可能还会发现 Maven 内部版本号插件很有用...这里