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.
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:
Note: if only one JDK is available, the option is hidden.

- 4,559
- 2
- 18
- 37
-
Under Manage Jenkins > Global Tool Configuration > Under JDK Name I see only one option "Install automatically". Under this I see option "Add Installer" ("Extract *zip" "Run Batch/Shell command") In your post I see JAVA_HOME location given so did you install 1.8 already? I am stuck here, how to proceed further. – Kpras Feb 17 '23 at 06:31
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

- 3,388
- 3
- 36
- 45
In Jenkins > Manage Jenkins > Configure System > Global properties > Add environment variable. JAVA_HOME environment variable

- 1
- 1