Extract version ID from POM in a Jenkins pipeline
Use readMavenPom
like this:
pom = readMavenPom file: 'pom.xml'
pom.version
See Model reference for properties (like the above version).
For this to work, one has to install Pipeline Utility Steps plugin
In Jenkins 2.138.3 there are two different types of pipelines.
Declarative and Scripted pipelines.
"Declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefore should be easier for those new to pipelines, allow for graphical editing and much more. scripted pipelines is the fallback for advanced requirements."
jenkins pipeline: agent vs node?
Here is an example of a Declarative Pipeline:
pipeline {
agent any
environment {
//Use Pipeline Utility Steps plugin to read information from pom.xml into env variables
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
}
stages {
stage('Test') {
steps {
echo "${VERSION}"
}
}
}
}
Example of Scripted Pipeline
node('master') {
stage('Test') {
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
echo "IMAGE: ${IMAGE}"
echo "VERSION: ${VERSION}"
}
}
Here are some good links:
Declarative https://github.com/jenkinsci/pipeline-examples/blob/master/declarative-examples/jenkinsfile-examples/mavenDocker.groovy
Scripted https://bulldogjob.com/articles/726-exploring-jenkins-pipelines-a-simple-delivery-flow
You could always shell out and use maven to query the version, e.g. like so:
echo sh(returnStdout: true,
script: 'mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate ' +
'-Dexpression=project.version -q -DforceStdout ' +
'--batch-mode -U -e -Dsurefire.useFile=false'
).trim()
# Note: Use "./mvnw" with Maven wrapper
Where
-
-Dexpression=project.version -q -DforceStdout
are mandatory (see here for details), and -
--batch-mode -U -e -Dsurefire.useFile=false
are recommended for CI usage (see here for details).
This is also implemented in the shared library ces-build-lib. Here it is as convenient as
echo mvn.version
The Getting Started with Pipeline page showed yet another option. It has some disadvantages, though (see bellow and comments):
def version() {
def matcher = readFile('pom.xml') =~ '<version>(.+?)</version>'
matcher ? matcher[0][1] : null
}
This always matches the first <version>
tag to be found in the pom.xml
. This should be the version of the maven module or its parent in most cases. It could be the version of the parent, though.
You can try readMavenPom
function that is available.
More info is here: https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readmavenpom-read-a-maven-project-file
I usually use the map to do this.
def pomFile = readFile(pomName)
def pom = new XmlParser().parseText(pomFile)
def gavMap = [:]
gavMap['groupId'] = pom['groupId'].text().trim()
gavMap['artifactId'] = pom['artifactId'].text().trim()
gavMap['version'] = pom['version'].text().trim()