69

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  

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>
4
  • why not just try a hello world project ?
    – mebada
    Commented Aug 7, 2013 at 10:26
  • 8
    @mebada, if I wouldn't try this several times on different projects I wouldn't ask. Why not just provide solution if you know it?
    – mr.nothing
    Commented Aug 7, 2013 at 11:17
  • referring to Your pom, Simply you can't do that ... project.version should be static
    – mebada
    Commented Aug 7, 2013 at 11:57
  • <version>${env.SOME_VARIABLE}</version>, then $ SOME_VARIABLE="1.0.0-SNAPSHOT" mvn ... Check out my answer below.
    – igops
    Commented Mar 4, 2023 at 12:51

4 Answers 4

86

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.

7
  • 1
    Works as a charm, but one thing I didn't mentioned... One of the modules is dependent on another module's artifact, so I need to override this as well. Just mentioned it, sorry.
    – mr.nothing
    Commented Aug 7, 2013 at 12:15
  • Suddenly, this doesn't work on bamboo continious integration system. Have no time to ivestigate it, just providing fact. According to logs it hangs after the following: Searching for local aggregator root... So I assume it can't find parent pom...
    – mr.nothing
    Commented Aug 7, 2013 at 13:59
  • 1
    Use versions:commit to remove de POM backup: mvn versions:set -DnewVersion=1.0.3 versions:commit . Commented Aug 25, 2016 at 0:57
  • @mebada: this works flawlessly Agusti: you are right. mvn versions:commit is necessary to remove version backup pom files.
    – Gaurav
    Commented Dec 17, 2018 at 6:55
  • This is not usefull for me as it change pom.xml and my Docker build rebuild all every time even nothing has changed.
    – burtsevyg
    Commented Oct 30, 2019 at 16:51
40

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

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

5
  • Somehow in multi-module projects with ${revision} in place I cannot build modules separately even when passing revision explicitly on the command line. The error says, that the build cannot find dependencies with the version ${revision} Commented Feb 10, 2022 at 8:07
  • 2
    Hm, according to maven.apache.org/maven-ci-friendly.html#dependencies you have to use ${project.version} instead of ${revision} in the dependency and add flatten-maven-plugin to the parent pom, but I couldn't get it to work even when I did that. Commented Feb 10, 2022 at 21:31
  • 1
    Pay attention to the note that the name must be "revision". If you use any name other than the 3 listed mvn will print warnings that an expression is being used when <version> should be a constant and threatening that such "malformed" projects may not be supported in future.
    – msc
    Commented Apr 1, 2022 at 3:36
  • From my testing, ${revision} seems to work in the project.version and project.parent.version fields in any POM, but not in project.dependencyManagement.dependencies.dependency.version. I assume it wouldn't work in project.dependencies.dependency.version either. maven-release-plugin replaces the first two uses with the hardcoded release version, but leaves the other and it fails to resolve. Commented Nov 3, 2023 at 0:09
  • Very similar behavior with flatten-maven-plugin also. I tried using versions:set afterward to fix up the dependency management entries, but Maven wouldn't let it run because the POMs were already invalid by that point. Ultimately, this functionality doesn't seem to work with BOMs. Commented Nov 3, 2023 at 0:09
9

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 ?

5
  • Actually, that doesn't work for me for some reason. Please refer to my pom.xml above.
    – mr.nothing
    Commented Aug 7, 2013 at 11:52
  • 1
    It only works if you have the ${version} placeholder ... uh... in place.
    – eckes
    Commented Jan 9, 2015 at 21:11
  • 3
    I'm facing the same situation, using Maven 3.x.x. The passed parameter will be ignored. Maven complains about the containing expression.
    – Reporter
    Commented Aug 31, 2017 at 12:49
  • And what happens if you don't serve the version variable to you build process? Empty version or a string ${version}. I think the approach is ok but better is the answer of John Velonis. It is more error tollerant.
    – Maik
    Commented Jun 2, 2021 at 18:56
  • 2
    Downvoted because ${version}, or any name but the 3 listed in John Velonis's answer, results in the warning from mvn noted by Reporter.
    – msc
    Commented Apr 1, 2022 at 3:40
-3

Just use the environment variables:

<project>
    <version>${env.SOME_VARIABLE}</version>
</project>
$ SOME_VARIABLE="1.0.0-SNAPSHOT" mvn clean install ...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.