Maven:从属性文件中设置pom.xml中的属性

Dra*_*gon 9 maven

我有多模块项目,对不同的模块版本有很多依赖.目前版本是硬编码的,需要手动更改它们.因此,我决定将所有这些文件放在属性文件中,并在项目构建期间从中获取属性值.

以下是我尝试这样做的方法:

root pom.xml

<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>./version.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

文件版本.properties

module1.version=1.1
module2.version=1.8
module3.version=5.4
Run Code Online (Sandbox Code Playgroud)

模块pom.xml的示例

<properties>
    <module1.project.version>${module1.version}</module1.project.version>
</properties>

<parent>
    <groupId>com.mymodule</groupId>
    <artifactId>test</artifactId>
    <version>${module1.version}</version>
    <relativePath>../pom.xml</relativePath>
</parent>
Run Code Online (Sandbox Code Playgroud)

构建失败:

无法在项目ccm-agent上执行目标org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version(parse-versions):目标org.codehaus.mojo的执行解析版本:build-helper- maven-plugin:1.7:parse-version失败.NullPointerException - > [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException:无法在项目ccm-agent上执行目标org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version(parse-versions) :目标org.codehaus.mojo的执行解析版本:build-helper-maven-plugin:1.7:解析版本失败.

如何从文件中读取某些属性并以正确的方式配置pom.xml?

Dra*_*gon 6

最后它似乎很简单.我用过initialize相.改变它来validate解决问题:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <phase>validate</phase>
Run Code Online (Sandbox Code Playgroud)