How to select multiple JDK version in declarative pipeline Jenkins

YourAboutMeIsBlank picture YourAboutMeIsBlank · Dec 19, 2017 · Viewed 20.2k times · Source

I want to use different JDK versions for different stages in Jenkins declarative pipeline. In the first stage I am using Java 8. In the second stage i am using Java 6. How to select multiple JDK version in declarative pipeline in Jenkins?

  pipeline {
  agent any

  tools {
    jdk 'jdk_1.8.0_151'
    jdk 'jdk_1.6.0_45'
  }

  stages {
    stage('java 8') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
    stage('java 6') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
  }
}

Answer

Mor Lajb picture Mor Lajb · Jan 21, 2018

you can add a tools section for each stage.

pipeline {
        agent any

        stages {  
            stage ("first") {
                tools {
                   jdk "jdk-1.8.101"
                }
                steps {
                    sh 'java -version'
                }
            }
            stage("second"){
                tools {
                   jdk "jdk-1.8.152"
                }
                steps{
                    sh 'java -version'
                }
            }
       }
}