Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a command-line property to overwrite a maven property

Tags:

I have a pom file in which version numbers of some dependencies rely on the project version property specified in the settings of the pom file. Can I overwrite this via the command-line? If so, how?

Here's the long story:

We are currently transitioning our projects to maven, but we're not all the way there yet. There are multiple modules that are still not built with maven and are therefore dependencies in our project (they are built into jars through ant). Upon release, we want all of these jars to be built and contain the same version number as the parent project. For a release, two steps are performed (until we can get everything using maven)

  1. The jars are built in ant with the correct release version (12.12.4.0).
  2. The maven release plugin is used to deploy the project to our artifact repository.

In the second step command-line arguments are used to specify the release:

mvn release:prepare -DreleaseVersion=12.12.4.0 -DdevelopmentVersion=12.12.4.1-SNAPSHOT -Dtag=iv-12.12.4.0 

I would like the pom file to be updated with the version specified. However, when this command is run, the version within the pom file (12.12.4.0-SNAPSHOT) is still being used. This fails the "checking dependencies and plugins for snapshots" step and I am required to resolve my jars that still have the 12.12.4.0-SNAPSHOT version used from the maven version property.

This led me to the original question of how I can override this so that the version resolves to that specified on the command line. Additional questions that could get me past this is: How to allow the maven release plugin to update the pom file before this check? How to skip the snapshot check (not desirable)

I could create a property within the pom file that I can overwrite, but then I'd have to maintain the version number in two places within the pom file.

Thoughts?

like image 494
Noremac Avatar asked Dec 04 '12 17:12

Noremac


People also ask

How do I pass system properties to maven?

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

How do you override a parent pom?

Overriding configurations from a parent pom can be done by adding the combine. self="override" attribute to the element in your pom. It appears that for Maven2. 2.1 if you do this in a profile it doesn't merge with plugins defined in parent profiles but overrides them.

What is D in maven command?

-D, --define <arg> Defines a system property. This is the option most frequently used to customized the behavior of Maven plugins.


1 Answers

Put parameters directly to pom from command line for example:

mvn clean install -Dtestng.version=6.3.1

Example:

     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">         <modelVersion>4.0.0</modelVersion>         <groupId>com.test</groupId>         <artifactId>test</artifactId>         <version>1.0.0</version>         <name>test</name>         <properties>                 <testng.version>6.4</testng.version>         </properties>          <dependencies>                 <dependency>                         <groupId>org.testng</groupId>                         <artifactId>testng</artifactId>                         <version>${testng.version}</version>                         <scope>test</scope>                 </dependency>         </dependencies> </project> 

If you run it normally testng version 6.4 will be used. But if you run it like: mvn clean install -Dtestng.version=6.3.1 testng version 6.3.1 will be used.

See Setting Maven params in Jenkins

like image 88
Andrzej Jozwik Avatar answered Sep 21 '22 06:09

Andrzej Jozwik