Use multiple JDKs in one Jenkinsfile
As usual, the Jenkins pipeline will only use the default JDK to execute the task. How to use multiple JDKs in one Jenkinsfile for different stages?
First, define the multiple JDKs in Jenkins “Global Tool Configuration”.
Now we have for example JDK11 and JDK8(used as default JDK)
And here is one sample pipeline file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#!/usr/bin/env groovy node { jdk = tool name: 'JDK11' env.JAVA_HOME = "${jdk}" echo "jdk installation path is: ${jdk}" withMaven() { // use pipeline-maven-plugin to config the maven stage('checkout') { checkout scm } stage('check java') { sh "$JAVA_HOME/bin/java -version" } stage('clean') { sh "mvn clean" } stage('backend tests') { sh "mvn test -Pdev" } stage('build docker') { sh "mvn dockerfile:build" } stage('public docker') { sh "mvn dockerfile:push" } stage('stage use default JDK8') { jdk = tool name: 'JDK8' env.JAVA_HOME = "${jdk}" sh "mvn special-stage" } } } |
This is the solution.