Maven:从属性文件中设置pom.xml中的属性 [英] Maven: set property in pom.xml from properties file
问题描述
我有一个多模块项目,在不同模块版本上有很多依赖性.目前,版本已进行硬编码,因此需要手动进行更改.因此,我决定将所有这些文件放入属性文件,并在项目构建期间从中获取属性值.
I have multi-module project with a lot of dependencies on different modules versions. At the moment versions are hardcoded and one needs to change them manually. So I decided to put all of them to a properties file and get properties values from it during project build.
这是我尝试执行的操作:
Here is how I try to do it:
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>
文件version.properties
module1.version=1.1
module2.version=1.8
module3.version=5.4
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>
构建失败,原因:
无法执行目标 org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version 项目ccm-agent上的(解析版本):的执行解析版本 目标org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version 失败的. NullPointerException-> [帮助1] org.apache.maven.lifecycle.LifecycleExecutionException:失败 执行目标 org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version 项目ccm-agent上的(解析版本):的执行解析版本 目标org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version 失败.
Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version (parse-versions) on project ccm-agent: Execution parse-versions of goal org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version failed. NullPointerException -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version (parse-versions) on project ccm-agent: Execution parse-versions of goal org.codehaus.mojo:build-helper-maven-plugin:1.7:parse-version failed.
如何从文件中读取一些属性并以正确的方式配置pom.xml?
How can I read some properties from a file and configure pom.xml in correct way?
推荐答案
最后似乎很简单.我使用了initialize
阶段.将其更改为validate
可解决问题:
It appeared to be very simple at the end. I used initialize
phase. Changing it to validate
fixed the problem:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
这篇关于Maven:从属性文件中设置pom.xml中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!