Specify JAVA_HOME in Jenkins for Java 11

Viewed 14620

I have several projects running in Java 1.8 and Jenkins groovy script + Ansible acting as a pipleine to do deploys. Some of the projects are now migrated to Java 11 (OpenJDK). How do I configure Jenkins to build these projects in migrated to Java 11?

3 Answers

In Jenkins 2.x, under Jenkins | Manage Jenkins | Global Tool Configuration,
you should find a section "JDK Installations".

Configure multiple your jdk there. I'd not recommend "Install automatically" for Oracle JDK given their licensing changes since April 2019.

JDK Installations

This question describes handling multiple jdk in a pipeline.

Corresponding the names in the image example the tools values would be:

  tools {
    jdk 'jdk1.8'
    jdk 'jdk1.6'
  }

For maven,

Use a maven toolchain / profiles to be able to dynamically choose which to use: https://maven.apache.org/guides/mini/guide-using-toolchains.html

https://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html

ps: for freestyle jobs, the choice in in the General tab: enter image description here

Note: if only one JDK is available, the option is hidden.

First you need to add java 11 jdk by going to Manage Jenkins menu. This will fix the build issues.

For groovy script: You cannot set JAVA_HOME in jenkins and expect maven to pick up correctly. It wont work. The successful approach was to add this in the pipeline:

env.JAVA_HOME="${tool 'openjdk_11_0_1'}"
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"

"openjdk_11_0_1" beeing the name of the java configuration initially registered into Jenkins

Related