How to get the Maven Version of a project in Jenkins?

0 votes

Can the Jenkins build be aware of the Maven version number of a project after processing the POM?

Aug 9, 2018 in Jenkins by Tyrion anex
• 8,280 points
1,711 views

3 answers to this question.

0 votes

Follow the below steps:

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}).

answered Aug 9, 2018 by Sophie may
• 9,870 points
0 votes

Since the groovy script is a bit complex in my view,You could also do it this way :

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

Assuming that you have your project name within a line or two above/below version like a normal pom:

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

You can use grep command to suck the version number with it.

answered Jul 31 by Sirajul
• 38,420 points
+2 votes

Here is a snippet from our Jenkins declarative pipeline:

steps {
    sh "mvn -N help:effective-pom -Doutput=target/pom-effective.xml"

    script {
        pom = readMavenPom(file: 'target/pom-effective.xml')
        projectArtifactId = pom.getArtifactId()
        projectGroupId = pom.getGroupId()
        projectVersion = pom.getVersion()
        projectName = pom.getName()
    }

    echo "Building ${projectArtifactId}:${projectVersion}"
}

Because this code lets Maven perform all interpolations, the result is a clean model with everything resolved. So it works even with properties in version.

answered Oct 4 by Petr Doležal
Thanks, that was helpful... I have upvoted your answer :)

Related Questions In Jenkins

0 votes
1 answer

How to fix a broken build for my project in jenkins?

The user needs to open the console ...READ MORE

answered Oct 11 in Jenkins by Sirajul
• 38,420 points
226 views
+1 vote
1 answer

How to configure SCM Plastic Maven Project in Jenkins?

Hey @Anoop, In order to create a ...READ MORE

answered 5 hours ago in Jenkins by Sirajul
• 38,420 points
10 views
+2 votes
5 answers
0 votes
2 answers
0 votes
2 answers

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

If you are talking about executing another ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 38,420 points
6,504 views
0 votes
1 answer

How to construct Maven installation in Jenkins for each Slave node?

I’m assuming that Java and Maven are ...READ MORE

answered Aug 1, 2018 in Jenkins by Sophie may
• 9,870 points
833 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
215 views
0 votes
1 answer