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?
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!"
User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With