Revert back to system JDK within a stage

I have configured an alternate JDK (17) at the pipeline level. How can I override that by telling a specific stage to use the system JDK 8?

For example:

def call (body) {
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {

        tools {
            maven 'apache-maven-3.9.3'
            jdk 'EclipseTemurin-17'  // Alternate JDK 17 added under Global Tool Configuration
        }

        stages {

            stage('Build & Test App') {
                steps {
                    build_jar()
                }
            }

            stage('SonarQube Analysis') {
                tools {
                    jdk "(System)"  // < Does not work. How do I revert to system JDK 8?
                }
                steps {
                    sonarqube(pipelineParams.projectId)
                }
            }
        }
    }
}

This errors out that the (System) JDK is unrecognized. However, This is how the system JDK is shown when adding a new Maven job:

image

So how can I tell Jenkins to use the system JDK within a stage, to override a non-default JDK used at the pipeline level?

Jenkins version: 2.346.3

I think that you really don’t want to tell it to use the System JDK, because that will change when you upgrade to Java 11 or Java 17 or Java 21. I think that you want to tell it to use OpenJDK-8 or you want to define a new JDK that is the exact version you need.

When you allow a job to rely on the System JDK, you’re connecting that job to the Java version that runs Jenkins rather than connecting it to the specific Java version that is needed for the job.

2 Likes