Home > Back-end >  Jenkins pipeline uses different java version from the one specified in the script
Jenkins pipeline uses different java version from the one specified in the script

Time:12-09

My spring boot application uses java 11 but my jenkins server has java 8 installed. Hence I added a new JDK in the global tool configuration. I specified enter image description here

I specified the JDK name in my pipeline script as shown below but when I do java -version from the pipeline script I still get the result as jdk 8. The JAVA_HOME path shows up correctly. Could someone guide me on what could be the issue?

Thanks

pipeline {
    agent any
    tools {
        jdk 'AdoptOpenJDK11'
        maven 'Maven_3_8_1'
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo $JAVA_HOME$'
                sh 'java -version'
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}

Jenkins logs are as below

  echo /var/jenkins_home/tools/hudson.model.JDK/AdoptOpenJDK11/jdk11_13$
/var/jenkins_home/tools/hudson.model.JDK/AdoptOpenJDK11/jdk11_13$
[Pipeline] sh
  java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
[Pipeline] sh
  mvn -B -DskipTests clean package
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

CodePudding user response:

I worked around the problem by specifying a docker image for the 'Build' stage

        stage('Build') {
            agent {
                docker {
                    image 'maven:3.8.1-adoptopenjdk-11'
                    args '-u root -v /root/.m2:/root/.m2 -v "$pwd":/usr/src/app -w /usr/src/app'
                }
            }
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
  • Related