Getting Project Version from Maven POM in Jenkins

You can use the ${POM_VERSION} variable, which was introduced with https://issues.jenkins-ci.org/browse/JENKINS-18272


After a lot of digging around (I never realised how poorly-documented Jenkins is!) I found a quite trivial solution.

  1. Install the Groovy plugin
  2. Add a Post Step to your Maven build of type Execute **system** Groovy script
  3. Paste in the following snippet of Groovy:

Script:

import hudson.model.*;
import hudson.util.*;

def thr = Thread.currentThread();
def currentBuild = thr?.executable;
def mavenVer = currentBuild.getParent().getModules().toArray()[0].getVersion();
def newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue("MAVEN_VERSION", mavenVer));
currentBuild.addAction(newParamAction);

The build environment variable called MAVEN_VERSION will now be available for substitution into other post-build steps in the usual manner (${MAVEN_VERSION}). I'm using it for Git tagging amongst other things.