Change version of Maven project without manipulating the POM file

2.7k views Asked by At

Is it somehow possible to change the version of a Maven project without manipulating the POM file?

Let's say I have a Maven project with version 1.5.0-SNAPSHOT but I want to build it as 1.5.46.

The Versions Maven Plugin unfortunately modifies the POM files.

5

There are 5 answers

0
J Fabian Meier On

You can

  1. Make a copy of your pom as temppom.xml
  2. Replace the version in temppom.xml
  3. Build with mvn -f temppom.xml.
  4. Delete temppom.xml.
0
Robert Scholte On

Maven supports delivery friendly versions, see https://issues.apache.org/jira/browse/MNG-5576 . For more details I would suggest to talk with @khmarbaise

3
Aisatora On

Try to override project version with

mvn versions:set -DnewVersion=<version>

in your particular case:

mvn versions:set -DnewVersion=1.5.46
0
yinon On

Since Maven 3.5.0 this is possible using a special predefined property: ${revision}. Define the property with a default value (e.g. 1.5.0-SNAPSHOT) and when needed, set it during execution to a specific version (e.g. 1.5.46).

For example, define the following in your pom.xml:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>foo</artifactId>
  <name>Foo Module</name>
  <version>${revision}</version>
  ...
  <properties>
    <revision>1.5.0-SNAPSHOT</revision>
  </properties>
</project>

Build it using the default value:

mvn clean install

This will produce an artifact identified as org.example:foo:1.5.0-SNAPSHOT.

In order to build a specific version, set the revision property, for example:

mvn clean install -Drevision=1.5.46

This will produce an artifact identified as org.example:foo:1.5.46.

For further details, see the Maven CI Friendly Versions page.

0
Anatolii Stepaniuk On

The plugin provides a goal to revert the changes made by it:

mvn versions:revert