1

In Jenkins Server, there are two global environment variables defined. It's in Manage Jenkins -> Configure System -> Global Properties -> Environment variables

Name: MAVEN_HOME Value: /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven3.5.2

Name: PATH+EXTRA $PATH:/usr/local/bin:$MAVEN_HOME/bin

I see that PATH+EXTRA will add the MAVEN PATH to the PATH environment variable. This is how my existing Server set up is. Now I need to update Jenkins with Maven 3.8.2, so I downloaded Maven 3.8.2 in the server using Manage Jenkins -> Global Tool Configuration -> Maven Installations. Now I am trying to override the global MAVEN_HOME and PATH to point to MAVEN_3.8.2 path.

In the Jenkins pipeline script

def maven_version = 'maven_3.8.2'
pipeline {
    agent any
  stages {
    stage ('build') {
      steps {
        withEnv(["PATH+MAVEN=${tool maven_version}/bin"]) {
          echo "PATH is: $PATH"
          echo env.PATH
          echo env.MAVEN_HOME
          sh 'env'
          sh 'mvn --version'
        }
      }
    }
  }
}

Results:

  • echo "PATH is: $PATH" => /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

  • echo env.PATH => /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

  • echo env.MAVEN_HOME => /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2

  • sh 'env' => prints all the environment variables. Noticed following: MAVEN_HOME=/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2 PATH=$PATH:/usr/local/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Why is the PATH is being appending with Maven 3.5.2 in the front of the path. How can I let PATH point to Maven 3.8.2?

  • sh 'mvn --version' => Apache Maven 3.5.2 Maven home: /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2

How do I get the mvn --version result with maven3.8.2?

Note: I also tried with free style project, and used following commands to override the values but the mvn --version is always printing 3.5.2. Any idea if it is a bug with Jenkins unable to override the path or is there any way to do it? export MAVEN_HOME=/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2 export PATH=$PATH:$MAVEN_HOME/bin

acm
  • 77
  • 3
  • 11

1 Answers1

0

The format you used to modify the PATH variable uses concatenation that prepends the new value to the existing one. It means that

PATH+MAVEN=${tool maven_version}/bin

is an equivalent of:

PATH=${tool maven_version}/bin:$PATH

You can solve this issue by overriding the PATH variable explicitly and putting the new path at the end of the variable. Try to test the pipeline like this one:

def maven_version = 'maven_3.8.2'
pipeline {
    agent any
  stages {
    stage ('build') {
      steps {
        withEnv(["PATH=${tool maven_version}/bin:$PATH"]) {
          echo "PATH is: $PATH"
          echo env.PATH
          echo env.MAVEN_HOME
          sh 'env'
          sh 'mvn --version'
        }
      }
    }
  }
}
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Thank you for the suggestion. The output for PATH in the command sh 'env' still takes 3.5.2 in the front of the path and so mvn --verison output is 3.5.2. – acm Aug 23 '21 at 11:56
  • This worked for me. def maven_version = 'maven_3.8.2' pipeline { agent any stages { stage ('build') { steps { withEnv(["MAVEN_HOME=${tool maven_version}"]) { sh ''' export PATH=${MAVEN_HOME}/bin:$PATH env mvn --version ''' } } } } } – acm Aug 23 '21 at 12:00