avatarValentin Despa

Summary

The website content provides a detailed guide on how to configure Jenkins to use a different Java JDK version, specifically Java 17, in a Jenkinsfile pipeline without relying on Docker.

Abstract

The article titled "Jenkins: How to Use a Different Java JDK Version in Jenkinsfile" addresses the challenge of using a specific Java version, Java 17, for a project build when the Jenkins server is running on an older version, Java 11. It outlines the steps to download and configure Java 17 using the JDK Tool Plugin within Jenkins, ensuring compatibility for projects that require a newer JDK. The guide explains the process of setting up the JDK Tool Plugin, manually installing Java 17 from the Oracle Java SE 17 Archive Downloads page, and modifying the Jenkinsfile to reference the new JDK configuration. It also provides troubleshooting tips for common issues, such as incorrect JDK paths or mismatched JDK names, and encourages reader engagement through comments and sharing.

Opinions

  • The author suggests that using Docker images could be a better, cleaner, and more modern option for managing JDK versions in a pipeline, indicating a preference for containerization in CI/CD processes.
  • The article implies that the JDK Tool Plugin's default configurations may not include the latest JDK versions, necessitating manual setup for newer versions like JDK 17.
  • The author emphasizes the importance of precise naming and path configuration when setting up a new JDK in Jenkins, highlighting common pitfalls that can lead to the incorrect Java version being used.
  • By encouraging readers to leave comments, share the article, and press the applause button, the author expresses a desire for community engagement and feedback, as well as recognition for their work.
  • The author indicates that reader support, through actions like subscribing to Medium and following their YouTube channel, is valuable for the creation of future content.

Jenkins: How to Use a Different Java JDK Version in Jenkinsfile

Let’s say that your Jenkins server is running using Java 11, but you need Java 17 to build a project. How can you do that if you don’t use Docker?

Overview

In this tutorial, we will go over the process of downloading and configuring a different JDK that will be used in a Jenkinsfile pipeline configuration. This example assumes that you are not using Docker images in your pipeline, which could be a better, cleaner, and more modern option.

The status quo

Let’s define a simple pipeline in a Jenkinsfile that will help us assert the current Java version that is available. I will be using the declarative pipeline syntax in the examples below.

pipeline {
    agent any
stages {
        stage('Which Java?') {
            steps {
                sh 'java --version'
            }
        }
    }
}

If your Jenkins installation is running on Windows, you need to replace sh with bat.

The pipeline console log shows that Java 11 is currently available.

The JDK Tool Plugin

A typical Jenkins installation already includes the JDK Tool Plugin (now called Oracle Java SE Development Kit Installer). If not, go ahead and install it from the Jenkins plugin manager.

A good indication that the plugin is installed can be found by going to Manage Jenkins > Global Tool Configuration and locating the JDK section.

The Global Tool Configuration in Jenkins with the configuration options for the JDK Tool plugin.

Installing Java 17 in Jenkins

From the Global Tool Configuration, let’s add a new JDK.

If you are scrolling through the available versions, you will soon notice that you can find anything higher than JDK 9. But we wanted to install JDK 17. What a bummer!

No worries. There is still hope. The first step is to remove this installer configuration from the screen by clicking on the X.

How the configurator should look similar to the screenshot below.

Let’s head to the Java SE 17 Archive Downloads page and locate the appropriate package. Since my Jenkins installation is running on Linux, I will select the Linux x64 Compressed Archive and copy the download link.

A list of the different versions of the JDK 17 that are available for download.

Back in the JRE tool, the configuration will look as below:

There are three critical aspects to this configuration that you need to pay close attention to:

  • JDK Name — Feel free to name this JDK version as you wish, but you need to reference this name in the Jenkinsfile as well, and it needs to be exactly the same.
  • Download URL — Make sure the URL is exactly as on the download page. Ensure you don’t have spaces or other characters before or after the URL value.
  • Subdirectory — This needs to be the subdirectory where the JDK will be available after download. In the example given, the directory name is jdk-17.0.4.1 . If the name of the directory is incorrect, the road path will be added to the PATH environment variable.

Configuring the Jenkins pipeline to use a different JDK

We still need to make a few small changes to the Jenkinsfile before we can use the JDK we have just configured.

We need to specify that we want to use the JDK tool in your pipeline using the tools configuration:

tools {
    jdk 'Java17'
}

Please note that previously I have named the configuration Java17. This needs to be consistent.

The full pipeline will look as follows:

pipeline {
    agent any
    
    tools {
        jdk 'Java17'
    }
    
    stages {
            stage('Which Java?') {
                steps {
                    sh 'java --version'
                }
            }
    }
}

If you enjoy content like this and it helped you solve a problem, help me create more. Please leave a comment, share, and press that 👏 a few times (up to 50 times). And consider subscribing to Medium for more amazing content.

Rerunning the pipeline and checking the logs should show the following result:

The logs show that Java 17 is not available to the pipeline

Troubleshooting

The Java version remains unchanged

If you did everything in the tutorial and you still get the same old Java version, the most common culprit is the path to the new JDK version.

Use the following pipeline step in a stage:

echo "$PATH"

This will show in the logs the content of the PATH variable. Here is an example:

/home/valentin/jenkins/tools/hudson.model.JDK/Java17/jdk-17.0.4.1_linux-x64_bin/bin::/home/valentin/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

If you take the first path and try to access it using the terminal, you should notice that it does not exist, and this is why the new Java version is not being picked up.

Go two levels down and figure out the actual name of the folder where the JDK has been extracted.

WorkflowScript: 5: Tool type “jdk” does not have an install of “jdk17” configured — did you mean “Java17”?

This error indicates a mismatch between the JDK Name configured in the Global Tool Configuration and the name provided to the Jenkinsfile. The names need to be exactly the same. Check for spaces and upper or lower cases.

Conclusion

I hope this tutorial helped you get started using a different Java version in your Jenkins pipeline. Leave a comment in the section below if you have any questions. I would love to hear from you!

Thank you for sticking with this article until the end. If you enjoyed it, please leave a comment, share, and press that 👏 a few times (up to 50 times). It will help others discover this information, and maybe it will help someone else as well.

Follow me on Medium and YouTube if you’re interested in more tutorials like this one.

Jenkins
Jenkinsfile
Jdk 17
Java17
Jenkins Pipeline
Recommended from ReadMedium