Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: set property in pom.xml from properties file

Tags:

maven

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?

like image 854
Dragon Avatar asked Sep 02 '13 13:09

Dragon


People also ask

How do I set system properties in POM xml?

To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"

Is it possible to refer a property defined in your POM xml file?

User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.

What is properties tag in POM xml?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.


2 Answers

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>
like image 103
Dragon Avatar answered Sep 29 '22 04:09

Dragon


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.

like image 33
Aaron Digulla Avatar answered Sep 29 '22 03:09

Aaron Digulla