Maven release plugin

Default featured post

Maven is a predominant build automation tool for Java projects. Even though it has created more than ten years ago, it’s still very popular. One of the reasons for maven popularity is its plugins available in abundance. One of those plugins is maven release plugin which eases up the process of releasing and versioning the project. In this article, I cover how to use the maven release plugin to perform releases easily.

Configurations

To use the plugin, firstly, we need to add it to the pom.xml file like below:

Then we have to configure the plugin to point to the version control URL, usually Git.

There are a couple of things to note in regard to the above snippet. First of all, the version should have -SNAPSHOT, otherwise, the plugin fails to release. More about it later. Second of all, both connection and developerConnection should point to a .git URL. Lastly, to have a nice tag in Git, it’s necessary to have the tag under scm section.

Performing a release

Now that we have everything ready. Let’s start try to release. That can be achieve with a single command like this:

$ mvn -B clean release:clean release:prepare release:perform -Dusername=$GIT_USERNAME -Dpassword=$GIT_PASSWORD

The release:clean prepare a clean release. This means that it starts everything again. When a release fails, maven can continue from the failing point, but release:clean ensures everything starts from the beginning.

The release:prepare does the preparation for a release. This happens by removing the -SNAPSHOT from the pom.xml and modifying the tag. Additionally, it does a single commit prepare release tag-version.

Lastly, release:perform actually releases the new version of the product. And increases the minor version by one and adds back -SNAPSHOT. Additionally, it reverts back the tag. And it commits to version control with the message of prepare for next development iteration.

Additionally, -B flag is passed in front of mvn so that maven does not prompt for asking a release version and uses whatever exists in pom.xml.

Note that we have passed the Git username and password directly to maven. If you configure your CI with SSH key those switches will not be necessary.

The release plugin also has the dry run flag which activates with release:prepare -DdryRun=true.

The process of releasing software is pretty easy using maven release plugin. However, what described here is not all the features of the release plugin. I highly recommend to go through the official documentation at the link below to become more familiar with the plugin.
https://maven.apache.org/maven-release/maven-release-plugin/index.html

If you are interested in continuous integration with Jenkins and maven make sure to check one of my previous tutorial at the link below:
https://geekyhacker.com/2017/10/12/continuous-integration-with-spring-boot-maven-github-and-jenkins/