maven override project.version from command line

Maven

Maven Problem Overview


I wonder if it is possible to override project.version/version variable in maven via command line?

The following doesn't work:

mvn deploy -Dversion=1.0-test-SNAPSHOT  
mvn deploy -Dproject.version=1.0-test-SNAPSHOT  

Thanks in advance for your help!

My pom.xml is:

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>just.another.company.name</groupId>
	<artifactId>my-pom</artifactId>
	<version>1.3-SNAPSHOT</version>
	<name>My Module</name>
	<packaging>jar</packaging>
	<description>POM Project for My Module</description>
</project>

Maven Solutions


Solution 1 - Maven

Simply you can't override project.version. Try:

mvn versions:set -DnewVersion=<version>

Usually, I start by updating the parentPom

mvn versions:set -DnewVersion=1.0.3-SNAPSHOT

This would be enough if the parent pom contained all child projects if not you will need to go to a super project that contain all child project and execute

mvn versions:update-child-modules

Also the two previous steps will be enough if the parentPom exists in relative path if not you will need to install parentPom project with the new version

mvn install

Usually this will be enough if you don't have module depend on another module. if you do, declare its version as a project.version like that and it will be reflected automatically

<version>${project.version}</version>

Check the maven Versions Plugin docs.

Solution 2 - Maven

In Maven 3.5+, if you put the following in your pom.xml

<version>${revision}</version>
...
<properties>
    <revision>1.0.0-SNAPSHOT</revision>
</properties>

it will use 1.0.0-SNAPSHOT by default, but you can override it on the command line:

mvn -Drevision=2.0.0-SNAPSHOT clean package

Apparently this only works if the placeholder property is called revision (you can also use changelist and sha1 placeholders in your version).

See https://maven.apache.org/maven-ci-friendly.html

Solution 3 - Maven

Yes, you can override the project version by passing the required variable from the command line.

Make sure you are using the variable in your pom.xml

Eg:

mvn deploy -Dversion=1.0-test-SNAPSHOT 

in pom.xml

<version>${version}</version>

This should get picked during runtime. If this approach does not works, can you please show how you are using them in your pom.xml ?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmr.nothingView Question on Stackoverflow
Solution 1 - MavenmebadaView Answer on Stackoverflow
Solution 2 - MavenJohn VelonisView Answer on Stackoverflow
Solution 3 - MavenArpitView Answer on Stackoverflow