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,700 points
21,948 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
• 10,610 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, 2019 by Sirajul
• 59,230 points
+3 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, 2019 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, 2019 in Jenkins by Sirajul
• 59,230 points
7,929 views
+1 vote
1 answer

How to configure SCM Plastic Maven Project in Jenkins?

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

answered Nov 19, 2019 in Jenkins by Sirajul
• 59,230 points
967 views
0 votes
1 answer
+2 votes
5 answers
0 votes
2 answers
+5 votes
3 answers

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

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 10,610 points
117,098 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,458 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP