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:

<build>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
  </plugin>
</build>

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <version>0.2.12-SNAPSHOT</version>
  <scm>
    <connection>scm:git:url.git</connection>
    <developerConnection>scm:git:url.git</developerConnection>
    <url>project url</url>
    <tag>tag-0.2.12</tag>
  </scm>
</project>

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 this link: https://www.geekyhacker.com/continuous-integration-with-spring-boot-maven-github-and-jenkins/