When setting up a Maven Jenkins job, you have the possibility of selecting a JDK in the options. This is great unless you want to set the JDK programatically in a pre-build shell script for example. To achieve the latter I did the following:
- Add a new JDK with path ‘${JAVA_HOME}’. You can do so in the Jenkins tool options. Name it something like ‘jdk_java_home_var’
- In your Maven Jenkins job, create a file named ‘${WORKSPACE}/env.properties’
and pipe into it:
echo "JAVA_HOME=<new JDK location>" > ${WORKSPACE}/env.properties
- Load the environment variables from the
env.properties
file in a pre-build step - Select the JDK to be ‘jdk_java_home_var’
- Save
That should be it!