How to run two Java versions on same jenkins slave

75 Views Asked by At

I am using a jenkins slave with Java version 7 which is used to deploy application.

Now have to use Java 17 and also Java 7 on same slave machine. How can we run two Java versions for deploying application with different versions?

I can dot his by adding one more slave in jenkins with Java 17, but I am searching if I can do this by same slave.

2

There are 2 best solutions below

1
On

How we do this at our company is by using the "JDK installations" configuration in Jenkins. That is found in Jenkins > Manage Jenkins > Tools and you can add various JDK tools to Jenkins and then you can use those in your pipelines like outlined here: https://stackoverflow.com/a/48368506/21960203

In general I've found this to be the best way to handle when you need to have multiple versions of Java for different projects.

0
On

While the approach to manually set up the JDK tools via Jenkins settings will definitely work, it requires a constant maintenance (as well as administrative access to your Jenkins instance) and has another major drawback - you need to additionally maintain your Maven/Gradle/other build tools on all agents to ensure the correctness and immutability of your build environment.

If you're able to use containers as build environment I would highly recommend to do that instead of depending on the state of the build agents.