How to get access to a Maven POM version in a Freestyle build?

2,138 views
Skip to first unread message

Eric B

unread,
Jul 26, 2017, 5:24:52 PM7/26/17
to Jenkins Users
Due to my project structure, and the desire to use the .mvn/maven.config configuration file to specify my default maven configuration, I have created my build definition as a Freestyle project.

The build runs successfully, however I would like to include the version number in a tag in a post-build step.  Unfortunately, the maven POM_VERSION variable(s) are not accessible in a Freestyle project (only in a Maven build project).  Is there some other way I can extract the Version value from the pom.xml file?

I found a link in StackOverflow (https://stackoverflow.com/a/15879521/827480) executing a system groovy script but it was from 2013, and does not seem to export the POM_VERSION value as an environment variable in Jenkins 2.46+.  Additionally, from my understanding, this kind of workaround would only execute on a master node, which may be problematic.

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("POM_VERSION", project.version.toString())
currentBuild.addAction(new hudson.model.ParametersAction(param));



Is there another (ie: better) way to extract the version number from the pom file in a freestyle build?

Thanks,

Eric

Cuong Tran

unread,
Jul 27, 2017, 3:17:33 PM7/27/17
to Jenkins Users
Eric,

You certainly can run groovy script if you have the EnvInject plugin.  Another way to obtain the pom version is to run shell command:

    mvn -N -Dexec.executable="echo" -Dexec.args='${project.version}' org.codehaus.mojo:exec-maven-plugin:1.3.1:exec

Cuong Tran

unread,
Jul 27, 2017, 4:24:27 PM7/27/17
to Jenkins Users
Make sure to use "-q" to get just the version:

VERSION=$(mvn -q -N -Dexec.executable="echo" -Dexec.args='${project.version}' org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)

Eric B

unread,
Jul 28, 2017, 12:50:18 AM7/28/17
to Jenkins Users
Thanks - I added the EnvInject plugin and it works properly now.

Thanks,

Eric
Reply all
Reply to author
Forward
0 new messages