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,270 points
873 views

2 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,530 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
• 25,180 points

Related Questions In Jenkins

+2 votes
5 answers
0 votes
2 answers
0 votes
2 answers

How to auto build a job in jenkins if there is any change in code on Github repository?

To automatically run build on code commit, follow ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 25,180 points
135 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
145 views
0 votes
1 answer
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
• 25,180 points
4,042 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,530 points
659 views