How to select multiple JDK version in declarative pipeline Jenkins How to select multiple JDK version in declarative pipeline Jenkins jenkins jenkins

How to select multiple JDK version in declarative pipeline Jenkins


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'                }            }       }}


I would recommend you to use different docker images for each stage if you want to have different JDK versions. You can achieve using the docker hub openjdk images with the correct tag.https://hub.docker.com/r/library/openjdk/

https://hub.docker.com/r/library/openjdk/tags/Something like that:

pipeline {agent nonestages {    stage('openjdk:7-jdk') {        agent {            docker { image 'jdk7_image' }        }        steps {            sh 'java -version'        }    }    stage('java8') {        agent {            docker { image 'openjdk:8-jdk' }        }        steps {            sh 'java -version'        }    }}

}


From the Pipeline tools directive:

tools: A section defining tools to auto-install and put on the PATH. The tool name must be pre-configured in Jenkins under Manage Jenkins → Global Tool Configuration.

From the pipeline-examples and cloudbess example:

  pipeline {  agent any  tools {    jdk 'jdk_1.8.0_151'  }  stages {    stage('jdk 8') {      steps {        sh 'java -version'        sh 'javac -version'      }    }    stage('jdk 6') {      steps {        withEnv(["JAVA_HOME=${tool 'openjdk_1.6.0_45'}", "PATH=${tool 'openjdk_1.6.0_45'}/bin:${env.PATH}"]) {          sh 'java -version'          sh 'javac -version'        }      }    }    stage('global jdk') {      steps {        sh 'java -version'        sh 'javac -version'      }    }  }}