Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven tool is not set in Jenkins pipeline

I have this stage in my Jenkins pipeline:

   stage('Build') {
       def mvnHome = tool 'M3'
       sh '''for f in i7j-*; do
                 (cd $f && ${mvnHome}/bin/mvn clean package)
             done
             wait'''
   }

In Jenkins » Manage Jenkins » Global Tool Configuration I have a Maven installation called M3, version 3.3.9.

Global Tool Configuration » Maven Installations

When running this pipeline, mvnHome is empty because I get this in the log:

+ /bin/mvn clean install -Dmaven.test.skip=true
/var/lib/jenkins/jobs/***SNIP***/script.sh: 3: /var/lib/jenkins/jobs/***SNIP***/script.sh: /bin/mvn: not found

I did find a path /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/M3 on the Jenkins server, which works, but I would prefer not to use a hard coded path to mvn in this script.

How do I fix this?


EDIT: Summary of the answer, using tool and withEnv.

My working code is now:

   stage('Build') {
        def mvn_version = 'M3'
        withEnv( ["PATH+MAVEN=${tool mvn_version}/bin"] ) {
        sh '''for f in i7j-*; do
                (cd $f && mvn clean package -Dmaven.test.skip=true -Dadditionalparam=-Xdoclint:none  | tee ../jel-mvn-$f.log) &
              done
              wait'''
        }
   }
like image 300
Amedee Van Gasse Avatar asked Mar 09 '17 09:03

Amedee Van Gasse


People also ask

Why Maven project is not showing in Jenkins?

Kindly note that If this Maven Project option is not visible, we need to check whether the "Maven Integration" plugin is installed in Jenkins. If not installed, then install it and restart Jenkins.

How will you configure Maven in Jenkins?

In the Jenkins dashboard (Home screen), click Manage Jenkins from the left-hand side menu. Then, click on 'Configure System' from the right hand side. In the Configure system screen, scroll down till you see the Maven section and then click on the 'Add Maven' button. Uncheck the 'Install automatically' option.


2 Answers

You can use your Tools in Jenkinsfile with the tool and withEnv snippets.

Should looks like this:

def mvn_version = 'M3'
withEnv( ["PATH+MAVEN=${tool mvn_version}/bin"] ) {
  //sh "mvn clean package"
}
like image 66
mszalbach Avatar answered Oct 06 '22 16:10

mszalbach


The easiest way should be to use is tools directives:

pipeline {
  agent any
  tools {
    maven 'M3'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -B -DskipTests clean package'
      }
    }
  }
}

M3 is the name pre-configured in Global Tool Configuration, see the docs: https://jenkins.io/doc/book/pipeline/syntax/#tools

like image 21
zhangfx Avatar answered Oct 06 '22 18:10

zhangfx