perjantai 5. syyskuuta 2014

Getting project version from Maven project in Jenkins

Execute system Groovy Script with following script
import hudson.FilePath
import hudson.remoting.VirtualChannel

def pomFile = build.getParent().getWorkspace().child('pom.xml').readToString();
def project = new XmlSlurper().parseText(pomFile);      
def param = new hudson.model.StringParameterValue("MAVEN_VERSION", project.version.toString());
def paramAction = new hudson.model.ParametersAction(param);
build.addAction(paramAction);
Now you can use the "MAVEN_VERSION" in build, for example pass it on with "Trigger parameterized build on other projects" post build action by adding predefined parameters:
project_version=${MAVEN_VERSION}
Or in some shell commands
build.sh ${MAVEN_VERSION}
I've found this to be useful when one project deploys artefacts into repository, and another project wants to use those artefacts with exact version number.