Maven versioning best practices

MavenVersioning

Maven Problem Overview


What is the best way to change version of Maven project, to release this version and then return back to *-SNAPSHOT development.

Currently I'm doing following:

  • retrieve current version (most likely with SNAPSHOT) from pom.xml
  • increment version (mvn -DnewVersion=<something> versions:set), respecting rules described in the question Maven artifact version for patches
  • mvn:install to sent to repo
  • renaming version once again adding SNAPSHOT postfix.
  • committing changes (using some version control system)

I have a strong feeling I'm doing something wrong and/or inefficient.

Maven Solutions


Solution 1 - Maven

You should use the maven-release-plugin to release your artifacts. Than automatically all your versions will be incremented by the release-plugin. The exception might be if you are going from 1.0.3-SNAPSHOT to 1.1.0-SNAPSHOT . The timeline for developing with Maven is:

1.0.0-SNAPSHOT
1.0.0
1.0.1-SNAPSHOT
1.0.1
1.0.2-SNAPSHOT
1.0.2
..

To go for the step from a SNAPSHOT to a release version you should use the maven release plugin you can release an artifact simply by using:

First step:

mvn release:prepare 

The final step:

mvn release:perform

If you would like to accept the defaults you can simply add -B like:

mvn -B release:prepare 

or you can combine those steps into a single one:

mvn -B release:prepare release:perform

The above can also be used from within a CI solution.

Using mvn install is only intended to install the artifacts into your local repository. If you are working with a real one like a repository manager (which i can recommend) you have to use:

mvn deploy 

One requirement for using the release plugin is to configure the scm area in your pom (i hope you are using a version control?).

Solution 2 - Maven

If you want more control over the release phase, the maven-release-plugin (mrp) will not help you much.

In that case I have modified versions-maven-plugin to be able to increment version and also to add / remove SNAPSHOT suffix.

Thanks to these new features you are able to write script that do the same thing as mrp, but you have full control over each step.

For example, mrp commits changed version before it actually tries to build it. If the build fails, you have to revert that commit or, in case of SVN, you have to do another revert-commit.

> Note: I am not the original author of the increment function. I have adopted it > from autoincrement-versions-maven-plugin, as stated on github page.

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
QuestionshabuncView Question on Stackoverflow
Solution 1 - MavenkhmarbaiseView Answer on Stackoverflow
Solution 2 - MavenPetr ÚjezdskýView Answer on Stackoverflow