4

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.

Harold L. Brown
  • 8,423
  • 11
  • 57
  • 109
  • What exactly do you mean with "not modifying the pom file"? You could of course make a defensive copy of the pom, and restore it later (or use a temporary pom file for the build which contains your favourite number). – J Fabian Meier Sep 29 '17 at 11:56
  • I only have one question: Why don't you want to change the pom? In this case it is probably better to explain the root problem instead of asking how to implement a specific solution. – Robert Scholte Sep 29 '17 at 12:17
  • @RobertScholte We want to create Maven builds with the Git commit count instead of the SNAPSHOT postfix in the version number. The functionality should work both on our Jenkins and locally. We don't want to have to make a commit every time we increment the version therefore the POM files shouldn't be modified. – Harold L. Brown Sep 29 '17 at 12:49

5 Answers5

3

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.

yinon
  • 1,418
  • 11
  • 14
1

Try to override project version with

mvn versions:set -DnewVersion=<version>

in your particular case:

mvn versions:set -DnewVersion=1.5.46
Aisatora
  • 444
  • 6
  • 17
  • 2
    Unfortunately this modifies the POM files. I don't want this behavior. – Harold L. Brown Sep 29 '17 at 11:35
  • Do you have your version in pom as a variable? – Aisatora Sep 29 '17 at 11:38
  • Using one version (1.5.45-SNAPSHOT) as another version (1.5.46) is bad. If you do that you cannot be sure which version you are really running. Don't do that. Update the version as shown above (and/or run the release plugin) and commit the modified source to the SCM of your choice. – fhossfel Sep 29 '17 at 12:19
0

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.
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

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

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
0

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

mvn versions:revert
Anatolii Stepaniuk
  • 2,585
  • 1
  • 18
  • 24