avatarWei Kang

Summary

This context provides a tutorial on how to increment versions for a Maven build Java project using the Build Helper Maven plugin and the Versions Maven plugin.

Abstract

The tutorial begins by assuming that the reader has a Maven project with a specific version in the pom.xml file. It then introduces the necessary plugins, the Build Helper Maven plugin and the Versions Maven plugin, which are required to increment the version of the project. The Build Helper Maven plugin provides a goal called build-helper:parse-version that parses a version string and sets properties containing the component parts of the version. The Versions Maven plugin provides a goal called versions:set that sets the current project’s version and propagates that change onto any child modules as necessary. The tutorial then provides examples of how to use these plugins to increment the patch, minor, and major versions of the project. Finally, the tutorial shows how to create profiles in the pom.xml file to activate the version incrementation when given specific properties.

Opinions

  • The tutorial assumes that the reader has a basic understanding of Maven and its configuration files.
  • The tutorial provides clear and concise examples of how to use the Build Helper Maven plugin and the Versions Maven plugin to increment the version of a Maven project.
  • The tutorial suggests that the reader should open the pom.xml file to verify that the version has been updated after running the versions:set goal.
  • The tutorial recommends creating profiles in the pom.xml file to activate the version incrementation when given specific properties.
  • The tutorial provides a link to the complete pom.xml file for the tutorial.
  • The tutorial suggests that the reader should stay tuned for the next article, which will show how to increment a version that does not follow the Semantic versioning.
  • The tutorial provides a link to a referral program for Medium membership.

How To Increment Versions For Maven Build Java Project — Part 1

One of the ways to increment versions for a Maven build Java project.

Photo by NeONBRAND on Unsplash

In this article, I will be sharing one of the ways I have learned to increment versions for a Maven build Java project.

Before we start, let’s assume we have the following information in our pom.xml.

Reference: https://raw.githubusercontent.com/weikangchia/increment-version-maven/master/pom.xml

The plugins that we will need are:

Reference: https://raw.githubusercontent.com/weikangchia/increment-version-maven/steps/1-add-plugins/pom.xml

Build Helper Maven Plugin

The Build Helper Maven plugin provides a goal, called the build-helper:parse-version. This goal parses a version string (default: ${project.version}) and set properties containing the component parts of the version.

[propertyPrefix].majorVersion
[propertyPrefix].minorVersion
[propertyPrefix].incrementalVersion
[propertyPrefix].qualifier
[propertyPrefix].buildNumber
[propertyPrefix].nextMajorVersion
[propertyPrefix].nextMinorVersion
[propertyPrefix].nextIncrementalVersion
[propertyPrefix].nextBuildNumber

FYI: The default value for the propertyPrefix is parsedVersion

The goal also set the following properties:

[formattedPropertyPrefix].majorVersion
[formattedPropertyPrefix].minorVersion
[formattedPropertyPrefix].incrementalVersion
[formattedPropertyPrefix].buildNumber
[formattedPropertyPrefix].nextMajorVersion
[formattedPropertyPrefix].nextMinorVersion
[formattedPropertyPrefix].nextIncrementalVersion
[formattedPropertyPrefix].nextBuildNumber

FYI: The default value for the formattedPropertyPrefix is formattedVersion

Now let’s try with an example:

mvn build-helper:parse-version help:effective-pom

In the output, we will see the following properties:

Screenshot of the properties section of the output from the effective-pom

Bump A Patch Version

Format: ${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}

Output: 0.0.1

Bump A Minor Version

Format: ${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.0

Output: 0.1.0

Bump A Major Version

Format: ${parsedVersion.majorVersion}.0.0

Output: 1.0.0

Versions Maven Plugin

The Versions Maven plugin provides a goal, called the versions:set. This goal sets the current project’s version and based on that change propagates that change onto any child modules as necessary.

Now let’s try with an example:

mvn versions:set -DnewVersion=0.0.1

In the output, we will see the following messages to inform us that it has been updated successfully.

Screenshot of the output

You can now open your pom.xml and see that the project version has been updated to 0.0.1.

That’s it, we have all the ingredients, we can now put them together to increment versions for our Maven build Java project.

First, let’s update the Build Helper Maven plugin in the plugin section of our pom.xml.

Next, create 3 profiles to do the following:

  • bump-patch This profile will be activated when given the property, bumpPatch
  • bump-minor This profile will be activated when given the property, bumpMinor
  • bump-major This profile will be activated when given the property, bumpMajor

Now let’s try out the 3 profiles that we have just created.

bumpPatch

mvn validate -DbumpPatch

Output:

Screenshot of the output

bumpMinor

mvn validate -DbumpMinor

Output:

Screenshot of the output

bumpMajor

mvn validate -DbumpMajor

Output:

Screenshot of the output

And there we have it. I hope you have found this useful. Thank you for reading. You may find the complete pom.xml for this article from here.

Stay tuned for the next article, as I will show you how to increment a version that doesn’t follow the Semantic versioning (e.g. 1.0.0_1.0.0)✌️.

If you are not a Medium member yet and want to become one, click here.

Java
Versioning
Maven Plugin
Maven
Cicd
Recommended from ReadMedium