Force Maven to Use Java 8 While Running Pipeline

Hi Team,

I am using Jenkins 2.441 on Ubuntu 22.04 VM and also using the same VM as agent . I have install Jenkins with OpenJDK 17 and few of my projects are using Java 8 while running mvn commands during the pipeline.

So currently on my Jenkins VM there two different Java versions (17 and 8). So When run the pipeline and it starts executing mvn command and find Java 17 which ultimately fails the pipeline. But When I switch to Java 8 on the OS side then pipeline works but Jenkins UI will stop working as Java 8 is not supported.

Now the ask is how to force maven to use java 8 and Jenkins Controller to use Java 17.

One option is that I can ask developers to make change in the code so that it starts using Java 17 but I don’t want to go in that direction.

please suggests what can be done here.

Thanks in advance.

Either create another (docker based) agent that just contains Java 8 or select the correct Java version in the pipeline (see Global Tool Configuration).

Are you running the pipeline on the built-in node or have you configured a dedicated agent that just happens to run on the same VM?
In any case you could set a node property and override the JAVA_HOME environment variable to point to java8. This would work as long as all builds require Java8.
If you have pipeline that need Java 8 and others that need Java 17, then the only way is to have either dedicated agents (which is not optimal for scaling reasons), or you need to adjust the pipeline that they ensure the correct Java is found.

Hi Markus,

After setting up JAVA_HOME environment variable that points to Java 8 on agent properties fix the issue for me.

Thank you very much quick solution.

Pradeep Kumar