Building using Maven

There are a number of Maven plugins available for building and deploying Docker images. We’re going to use Fabric8.

Git hash based version numbers

There are a number of Maven plugins available for interacting with git, we recommend the Maven git commit id plugin. This plugin will allow us to make certain git based properties available to the build, which we can then use to compute the version number.

To use it, add this plugin to the build section of your parent POM:


Now, in the properties section of the parent POM, create a version number based on the commit time and id. Using the commit time is useful because it makes it possible to sort tags chronologically:


The version number will only change when you create a new git commit. If you are not using the latest tag in your deployment spec, then make sure when you update your project and want to redeploy, that you commit your changes first so you get a new version number, and to ensure that version number correlates to what is in the git repository at that commit hash.

Configuring the Fabric8 Docker plugin


There are two things to pay careful attention to here.

  • The base image we’re using is adoptopenjdk:11-jre-hotspot. You can use any Docker image that provides a JRE, this is the one we recommend for open source users of Kubernetes.
  • docker:build is added to the package phase do that the Docker image is built when running mvn package
  • Versions latest and ${version.number} are tagged
  • Artifact with dependencies is set for the assembly. Meaning your application and its dependencies are added to /maven in the Docker image

Building the Docker image

To build and publish the image run the following. The Docker username and registry are taken from system properties.

Note that the registry to push to must include the username, for example

Security information (i.e. user and password) can be specified in multiple ways as described in section docker-maven-plugin authentication.

mvn -Ddocker.username=<user> -Ddocker.registry=<registry>/<user> package docker:push
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.