How To Use Separate JDK Versions For Each Jenkins Job
Blog Post
In modern software development, managing multiple Java projects often requires using different Java Development Kit (JDK) versions to meet specific project requirements. Whether you’re dealing with legacy applications that rely on older JDKs or cutting-edge projects optimized for the latest Java features, ensuring seamless compatibility across your Jenkins CI/CD pipelines is crucial.
At LinqTV, where we continually strive to deliver world-class streaming solutions, managing diverse environments is a critical part of our development process. One of the challenges we’ve encountered is configuring Jenkins to use separate JDK versions for different jobs without introducing complexity or inconsistencies.
This blog will walk you through how to set up and configure multiple JDKs in Jenkins, enabling you to streamline your development workflows and adapt to diverse project needs effortlessly. Whether you’re a developer, DevOps engineer, or team lead, this guide will help you unlock the full potential of Jenkins for managing multi-version Java builds.
Jenkins, a popular automation server, is highly versatile, but its compatibility with different Java versions can sometimes pose challenges. While the latest Jenkins versions support a range of Java versions, older projects or specific requirements may necessitate the use of older JDKs. The Maven Toolchain plugin offers a solution to this problem, allowing you to configure multiple JDKs and use them as needed for your Maven builds.
In this section, we’ll explore the practical steps to set up and manage multiple JDK versions in Jenkins, ensuring your development pipelines stay efficient and adaptable.
Install the JDK version required for the particular Jenkins job to run on. Let’s say Job ‘A’ is required Java 15 and Job ‘B’ is required Java 21 to run. Install both the JDKs.
Go to Manage Jenkins > Plugins > Search for Config File Provider Plugin > Install the plugin.
Go to Manage Jenkins > Managed Files > Add a new Config > Select Maven toolchains.xml > Provide the desired JDK version and path > Name the config file and save.
In this case, JDK 15.0.2 is configured.
Go to Job Configuration > Scroll down to Build Environment > Check the Provide Configuration files option > Select the config file created earlier > Provide a desired path where the config file should be copied > Scroll down to Build > Specify Goal > Save the configuration.
Note: Replace “C:\Users\Administrator\.m2\toolchains.xml” with the exact path of the XML file in your system. “-t” stands for toolchain followed by XML file path and goal.
Open the pom.xml file > Add the maven toolchains plugin dependency inside the <pluginManagement> element and the goal outside the <pluginManagement> element.
Note: The desired JDK version must be specified in the goal.
The JDK version used for the job printed in the console.
By following these steps and leveraging the Mave