10
votes

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>

file version.properties

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

example of module 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>

Build fails with:

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.

How can I read some properties from a file and configure pom.xml in correct way?

2

2 Answers

7
votes

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

You must not use properties / variable replacement inside of <parent> elements.

The main reason here is that Maven must read the parent POM before it can start expanding properties since the parent POM might define properties as well.