Maven: set property in pom.xml from properties file

I have a multi-module project with many dependencies on different versions of modules. The versions are currently hard-coded and must be manually changed. Therefore, I decided to put all of them in the properties file and get the values โ€‹โ€‹of the properties from it during the build of the project.

Here is how I am trying to do this:

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> 

file version.properties

 module1.version=1.1 module2.version=1.8 module3.version=5.4 

pom.xml module example

 <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> 

Build failed:

Failed to fulfill target org.codehaus.mojo: build-helpers-Maven-plugin: 1.7: parse-versions for ccm-agent project: Perform parsing versions target org.codehaus.mojo: build-helper-maven -plugin: 1.7: parse-version failed. NullPointerException โ†’ [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: failed to fulfill the goal org.codehaus.mojo: build-helpers-Maven-plugin: 1.7: parse-versions for the ccm-agent project: Version parsing target org.codehaus.mojo: build-helper-maven-plugin: 1.7: parse-version failed.

How can I read some properties from a file and configure pom.xml correctly?

+8
maven
source share
2 answers

In the end, it turned out to be very simple. I used the 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> 
+6
source share

You should not use property / variable substitution inside <parent> elements.

The main reason here is that Maven must read the parent POM before it can start the property extension, since the parent POM can also define properties.

+2
source share

All Articles