Getting a version of a project from Maven POM in Jenkins

Is there a way that Jenkins build can know about the version of the Maven project version after processing the POM?

I have some projects in which version control is controlled by Maven, and in the post-build task we would like to create a Debian package and invoke some shell scripts. I need the version number that Maven used to use as the Jenkins environment variable, so I can pass it for post-build actions.

To be clear, I don't need to know how to get Jenkins to pass the Maven version number; instead, I want Maven to pass the version number to Jenkins!

+48
maven maven-2 maven-3 hudson jenkins
Mar 27 '12 at 16:14
source share
11 answers

After long jerks (I never realized how poorly documented Jenkins is!) I found a rather trivial solution.

  • Install Groovy Plugin
  • Add a Post Step to your Maven construct of type Execute **system** Groovy script
  • Paste the following Groovy snippet:

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); 

Now the build environment variable MAVEN_VERSION will be available for replacement with other stages of the subsequent build in the usual way ( ${MAVEN_VERSION} ). I use it for git tags among other things.

+21
Apr 04 '12 at 10:40
source share

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

+60
Feb 25 '14 at 14:17
source share

As already mentioned in other answers, if you use the Maven project type, you have access to the $ POM_VERSION variable. But if you do not, you can use this sequence of steps (ugly but reliable). The execution of this method is based on the same version of maven to determine the version of pom (when handling complex inheritance of the parent / child pom, where <version> may not even be present for the child).

  • Maven step for this purpose:

    org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version -l version.log

  • Step Step : (You may need to configure the path to the .log version depending on your hierarchy)

    echo "POM_VERSION=$(grep -v '\[' version.log)" > props.properties

  • Variable step Inject Environment (environment injector plugin):

    Property File Path: props.properties

Now you can use $ POM_VERSION as if it were a Maven project.

What it does: Uses maven to print the version along with output clutter, and then smooths the output clutter, leaving only the version, writes it to a file using the properties file format and then injects it into the build environment, The reason it is better than single-line such as mvn ..... | grep -v '\[' mvn ..... | grep -v '\[' , is that using the Maven step does not make any assumptions about the installed versions of maven and will be handled by the same unattended installation like any other maven steps.

+8
Feb 03 '16 at 18:34
source share

We used the Groovy Postbuild Plugin .

  String regex = '.*\\[INFO\\] Building .+ (.+)'; def matcher = manager.getLogMatcher(regex); if (matcher == null) { version = null; } else { version = matcher.group(1); } 

Adding this to Jenkins for use later is a bit complicated. Make it a shot, although I remember that it caused us headaches. (Sorry, we did it a long time ago)

 def addBuildParameter(String key, String value) { manager.build.addAction(new hudson.model.ParametersAction(new hudson.model.StringParameterValue(key,value))); } 
+3
Mar 27 2018-12-12T00:
source share

The same need was resolved as suggested by Groovy pom parsing.

 import jenkins.util.*; import jenkins.model.*; def thr = Thread.currentThread(); def currentBuild = thr?.executable; def workspace = currentBuild.getModuleRoot().absolutize().toString(); def project = new XmlSlurper().parse(new File("$workspace/pom.xml")) def param = new hudson.model.StringParameterValue("project.version", project.version.toString()) currentBuild.addAction(new hudson.model.ParametersAction(param)); 

Add this script as a "Run Groovy script system" postulate (so you do not need to install Groovy) and paste the code into the "Groovy" command.

+3
Apr 08 '13 at 12:50
source share

I used the Pipeline Utility Steps plugin in a declarative pipeline job to get the Maven version. In the example below, I use a script variable instead of an environment variable, because it can be changed and passed between steps.

 def TAG_SELECTOR = "UNINTIALIZED" pipeline { agent any stages { stage('Build') { steps { sh "mvn --batch-mode -U deploy" script { TAG_SELECTOR = readMavenPom().getVersion() } echo("TAG_SELECTOR=${TAG_SELECTOR}") } } } } 

Note. You must approve the getVersion () method after creating the job in the section "Managing jenkins> Approving an in-process script".

See also:

+3
Jan 16 '18 at 9:23
source share

Run the Maven Plugin "exec-maven-plugin" in the "Execute Shell" as the "Conditional step" worked for me:

 mvn -q -Dexec.executable="echo" -Dexec.args='${projects.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 

Integrate in Jenkins:

 -> "Add post-build step" -> "Conditional steps (single or multiple)" -> "Execute Shell:" 

export MY_POM_VERSION = `mvn -q -Dexec.executable =" echo "-Dexec.args = '$ {projects.version}' --non-recursive org.codehaus.mojo: exec-maven-plugin: 1.3.1: exec `&& & && [[" $ {MY_POM_VERSION} "==" THE_VERSION_TO_BE_MATCHED "]] && & && echo" CONDITION_IS_MET "

  -> "Steps to run if condition is met" -> Add any build step you need 

Notes:

  • THE_VERSION_TO_BE_MATCHED should exchange with your version
  • '& & echo "CONDITION_IS_MET"' is for debugging purposes only. For the same purpose, you can add '& & echo "MY_POM_VERSION = $ {MY_POM_VERSION}" after the mvn command to understand what is happening.

This approach is more robust than grep, and it can be an alternative if Jenkins Ruby Plugin is not installed.

+1
Aug 25 '15 at 9:37
source share

You can also do:

 MAVEN_VERSION=`grep A -2 -B 2 "<your_project_name>" pom.xml | grep version | cut -d\> -f 2 | cut -d\< -f 1`-commit-"`echo $GIT_COMMIT`" 

Explanation: it is assumed that you have the name of your project on the line or above the version above / below, like a regular pom:

 <groupId>org.apache.bigtop</groupId> <artifactId>bigpetstore</artifactId> <version>1.0-SNAPSHOT</version> 

Then you can easily grep for artifactId, use the grep before / after commands to paste the version into it, then output the grep version and use the simple "unix" cut command to merge the content between the "version" tags.

I like the Jenkins-groovy integration, but it is much simpler and will work even on a build server that you have no control over (i.e. since bash is universal).

0
Feb 10 '14 at 23:26
source share

Decision:

 POM_VERSION=$( \ xmlstarlet sel \ -N x='http://maven.apache.org/POM/4.0.0' \ -t \ -v '//x:project/x:version/text()' \ pom.xml \ ) 

Explanation:

You can do this in one layer using the XPath tool from the command line, for example, mentioned in the section How to execute XPath single-line shells from the shell? ". I chose XMLStarlet , but they all have similar syntax.

When analyzing POM, you should consider namespaces. The docs here helped me figure this out.

To get the text for an element in XPath, you use the text () function, as described in XPath: select the text node .

My POM looks like this:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.foo.bar</groupId> <artifactId>foobar</artifactId> <version>1.0.6-SNAPSHOT</version> <packaging>jar</packaging> 

The disadvantage here is that if the namespace changes, you need to change the command.

0
Mar 01 '17 at 20:57
source share

Using "Run System Groovy Script" as follows:

 import jenkins.util.*; import jenkins.model.*; def thr = Thread.currentThread(); def currentBuild = thr?.executable; def projectManager = build.getProject() def file = projectManager.getWorkspace().child("pom.xml"); def project = new XmlSlurper().parseText(file.readToString()) def param = new hudson.model.StringParameterValue("currentVersion", project.version.toString()) currentBuild.addAction(new hudson.model.ParametersAction(param)); 

Using the Execute System Groovy script, you have direct access to the assembly from which you can get the project and, therefore, the "child" file in this case, pom.xml.

You do not need to create a new file, and as you can see, it offers very powerful access to every file in the workspace.

0
Apr 24 '18 at 8:17
source share

Based on @Akom's answer , preliminary steps to get POM_VERSION:

  1. Enter environment variables using the your_property_file property file. Note that if you select "Embed environment variables in the build process", the file must exist in the jenkins workspace.
  2. execute in the preliminary step execute shell the following bash script.

script

 mvn org.apache.maven.plugins:maven-help-plugin:evaluate -Dexpression=project.version -l project_version # grep for the version pattern rather than not mentioning '\[' echo "POM_VERSION=$(grep -E '^[0-9.]+(-SNAPSHOT)?$' project_version)" > your_property_file 
0
Apr 29 '19 at 12:14
source share



All Articles