Continuous Delivery with Maven and Jenkins
Motivation: Every jenkins build produces a potential release and as such each build should have a unique version number. This post shows how to use the Jenkins build number to archive this.
The basic idea is to use a variable as part of the <version>…</version> declaration in your pom.xml. This variable is set to 1.0-SNAPSHOT by default for local builds and when executed on jenkins to ‘1.0-<BUILD_NUMBER>’.
As variable name for the version ${revision}
is used as it is explicitly allowed since maven 3.2.1 at that location
(see improvement MNG-5576).
This is the warning if you try to use a different variable name e.g. ${myVar} instead of ${revision}:
[WARNING] Some problems were encountered while building the effective model for de.torstenwalter.examples:jenkins-versioning:jar:${myVar}
[WARNING] 'version' contains an expression but should be a constant. @ line 7, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
This snippet sets the default value for revision to 1.0-SNAPSHOT:
baseRevision
is meant to provide the base version e.g. 1.0 or 2.3 where the build number is later appended or -LATEST for
local builds.
The build number is included as part of the version by using a maven profile which is automatically enabled when the
environment variable BUILD_NUMBER
is present.