Are you looking for a easy way to handle versioning of your maven projects? Use the jgitver maven plugin! In this post we will setup the plugin and show some of the main features.

For a complete overview of the plugin and its workings please visit the jgitver github here.

As an example we will use a maven project from this previous post on how to create your own maven plugin. The complete code for this post can be found on github here.

Setup

The plugin is easiest to in combination with the maven wrapper. With this wrapper you can run maven commands without having this installed on your computer or use a different version. Some project archetypes or cli generators already add this wrapper. You can also use the following command.

mvn -N io.takari:maven:wrapper

You can now run maven commands with the added executable.

./mvnw clean install

If we take a look at the output we see that we build version 0.0.0-SNAPSHOT, as defined in the pom.

Screenshot 2020 11 22 at 19.02.58 1024x139 - JGITVER

jgitver configuration

The .mvn directory holds the configuration of the maven wrapper. This is the place we add the jgitver plugin by adding an exetensions file extensions.xml with the following content.

<?xml version="1.0"?>
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
  <extension>
    <groupId>fr.brouillard.oss</groupId>
    <artifactId>jgitver-maven-plugin</artifactId>
    <version>1.5.1</version>
  </extension>
</extensions>

A new build now results in the following version.

Screenshot 2020 11 22 at 19.09.22 1024x139 - JGITVER

It’s a combination of the version, the branch that I’m on and SNAPSHOT. Cool! Now we would like to release a stable verion 1.0.0. This would be a build from the master branch so lets merge.

Screenshot 2020 11 22 at 19.13.32 1024x139 - JGITVER

After merging we set a tag for this stable version.

git tag 1.0.0 -m "Stable version 1.0.0"

Another build wil use this tag to craft the maven version. Note that the reference to the branch wil be omitted for the master branch.

Screenshot 2020 11 22 at 19.15.59 1024x139 - JGITVER

Hopefully you found this to be a nice introduction to the jgitver plugin and hopefully it will help you manage your versions. Happy versioning!