Are you looking for an 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 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.

maven output building proto specifications snapshot 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 extensions 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.

maven output building proto specifications branch 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 version 1.0.0. This would be a build from the master branch so let’s merge.

github merged commit into master 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.

maven output building proto specifications 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!