Web Architecture, Java Ecosystem, Software Craftsmanship

Version Numbers for Continuous Delivery with Maven and Docker

Posted on Jul 11, 2016

Dealing with version numbers is an important challenge on the way to Continuous Delivery. The classical versioning approach (“8.2.0”) and release workflow is inappropriate, because it can’t be automated properly. This post shows how we can leverage the Git commit hash to get rid of manual workflows and automate the Continuous Delivery pipeline. At the end, every build will produce an artifact which is potentially shippable. We’ll implement this solution with Maven and Docker.

Version Numbers for Continuous Delivery with Maven and Docker


Let’s consider the principles of Continuous Delivery.

  • “Every build is a potential release”
  • “Eliminate manual bottlenecks”
  • “Automate wherever possible”

That’s exactly what we like to achieve. But let’s take a look at some concrete problems with the typical Maven release workflow and classical version numbers.

No Automation

Typically, a commit triggers a snapshot build, which produces a snapshot artifact (“8.1.2-SNAPSHOT”). When the developer feels that the software has reached a stable state, he triggers a dedicated release build. Therefore, he assigns a version number (“8.2.0”) up front.

A human has to assign a version number and trigger the release build.

A human has to assign a version number and trigger the release build.

What are the drawbacks of this approach?

  • We need to manually trigger a dedicated release workflow.
  • The version number needs to be assigned manually by a human.
  • Moreover, just by looking at the version number “8.2.0” it’s not clear, which commits are included in the artifact. We need a Git tag.

Arbitrary Snapshots

Additionally, snapshots cause a lot of problems.

Unclear and changing content of snapshot artifacts.

Unclear and changing content of snapshot artifacts.

  • Not traceable. We can’t say, which commits are included in the current snapshot artifact.
  • Unreliable. The snapshot artifact can change from one second to another. This can easily lead to trouble.
  • Error-Prone. It’s easy to override a snapshot accidentally (e.g. when creating a Git branch, but forgetting to change the version number in the pom. Building from the Git branch will override the develop snapshot.)

Annoying Maven-Release-Plugin

Finally, the maven-release-plugin is also causing troubles:

  • Overhead. The maven-release plugin runs 3 (!) full build and test cycles, manipulates the POM 2 times and creates 3 Git revisions.
  • Not isolated. The plugin can easily get in a mess when someone else commits changes during the release build.
  • Not atomic. If something went wrong in the last step (e.g. during the artifact upload), we are facing an invalid state. We have to clean up the created Git tag, Git revisions and fix the wrong version in the pom manually.

Solution: Leveraging the Git Commit Hash

We use the Git commit hash as the version number for the built artifact.

Using the Git commit hash as the version number.

Using the Git commit hash as the version number.

  • Every built artifact has a machine-assigned unique number.
  • Therefore, every artifact is potentially shippable. So there is no need for a dedicated release workflow anymore!
  • => The delivery pipeline is significantly simplified and automated
  • Traceable. It’s obvious, which commits are included.
  • Reproducible. Just check out the revision and build the artifact again (assuming no snapshot dependencies).
  • Basically, a Git tag is not necessary anymore.


Let’s integrate the proposed version naming in a Maven build. The example build produces a simple Spring Boot service as a fat Jar. Additionally, we wrap the fat Jar in a Docker image and deploy the image instead of the Jar. The complete sources can be found on Github.

To increase the human-readability, we prefix the Git commit hash with the commit timestamp. Hence, our version numbers will look like this:

format:  yyyyMMdd-HHmmss.abbreviatedCommitHash
example: 20160702-152019.75c54f5

Use the Commit Hash as the Version Number

That’s the most important step. We use the git-commit-id-plugin. The plugin reads the commit hash and the commit timestamp from the .git directory and makes it available in the pom via Maven properties.

        <dateFormat>yyyyMMdd-HHmmss</dateFormat><!--  human-readable part of the version number -->
        <generateGitPropertiesFile>false</generateGitPropertiesFile><!-- somehow necessary. otherwise the variables are not available in the pom -->

Next, we can define a Maven property for our version number that contains the commit timestamp and hash.


Now we can use the property in the version tag.



> mvn package # creates the jar with the desired version number
> java -jar target/versioning-continuous-delivery-20160702-180249.193a613.jar # starts the service

Create a Docker Image and Tag the Image with the Version Number

We use the docker-maven-plugin of io.fabric8 to create the image. The important part is the tagging. We create two tags. One tag is named after the commit id (“20160702-153902.7c12eb5”) and one is named “latest”. The latest-tag will simplify local testing.

        <authConfig> <!-- or use "docker login" up front to store the credentials on the local machine. -->
                    <tags><!-- define additional tags for the image -->
                            java -jar /maven/${project.artifactId}-${version.number}.jar

Don’t forget the assembly.xml and the following property:



> mvn package # creates a docker image and installs it to the local repository

> docker images
REPOSITORY                                                TAG                              IMAGE ID            CREATED             SIZE
phauer/versioning-continuous-delivery                     20160702-174649.28b5299          12d7a526c506        2 seconds ago       135.8 MB
phauer/versioning-continuous-delivery                     latest                           12d7a526c506        2 seconds ago       135.8 MB

> docker-compose up # starts the local image with the tag "latest"

> mvn deploy # pushes the image to the docker registry

The complete pom.xml and docker-compose.yml can be found on Github.

Open Issues and Discussion

Git Tags for Deployed Artifacts

Sometimes it’s handy to have a dedicated Git tag to mark an artifact, which was really deployed. You are free to tag the corresponding Git revision after deployment. You can do this either manually or integrate the tagging in your final deploy process. The latter keeps the workflow automated.

Dealing with the Artifact Flood

As every build leads to a new artifact, we are facing a huge amount of artifacts in our repository. Hence, we have to clean up.

  • But how do we know which versions have been deployed? We don’t want to delete the artifact that is currently in production. So we have to maintain a list of deployed versions and skip them during clean up. We can achieve this for instance by using Git tags, which are created when the artifact is deployed (see above). But we can also save the version list wherever we like.
  • Let’s be honest. Deleting old artifacts which have been in production a while ago is no problem. Have you ever rolled back to a 1-year-old artifact? Usually, you only need the current deployed artifact and its direct predecessor.

Check for Snapshot Dependencies

The maven-release-plugin aborts the build, if it finds snapshot dependencies. That’s basically fine, because snapshot dependencies prevent the reproducibility of the build. We are now lacking this check. However, I claim that this is no big deal in practice.

  • It happens rarely. Usually a developer pays attention and removes snapshot dependencies before he releases it.
  • When we apply the proposed version concept also for our libraries, we have no snapshots at all.
  • Even if our artifact contains snapshots, the snapshot is still bundled up within the artifact (fat Jar or Docker image). The artifact is saved in the registry, tested and finally deployed. Do we really need to reproduce the build if we have the built artifact in our registry?

Use Build Timestamp instead of Commit Hash

There are versioning proposals that use the timestamp of the build instead of the commit hash. But I prefer the commit hash due to the following reasons:

  • Obvious tracing of version numbers to commits.
  • Basically, no Git tag necessary.
  • Reproducible artifact based on given revision.

Is Docker Necessary for this Versioning Approach?

Basically, not. The proposed approach can also be applied if your Maven build produces only a Jar. In this case we also use the commit hash for the version number of the Jar.

However, there is the problem, that by default the properties created by the git-commit-id-plugin are not resolved for the maven-install-plugin and the maven-deploy-plugin. A solution for this can be found in this issue on GitHub.

Moreover, Docker makes things easier. Maven only allows one version number for an artifact. Docker allows multiple tags for a single image. This way, you can tag an image with “7d1dcc” and “latest”. The latest-tag is useful for local testing. You always get the latest image without permanently incrementing the version number (e.g. in the local docker-compose.yml).