Is it possible to have Gradle install the JDK?

I know the normal pattern is to configure the JDK in Jenkins, choose the JDK in the Jenkins job, have Gradle use that JDK.

However that does not work for me. We use feature/bugfix branches in git and have one Jenkins build that builds all the different branches. The problem is that some branches needs to be build using JDK 1.7 and other using JDK 1.8.

So the idea is to have gradlew download the correct JDK (just like it downloads the correct Gradle version.). That way all branches can be build by the same Jenkins job but with different JDKs.

An added benefit is I can upgrade the JDK on a feature branch, get it compiled and tested before merging into the release branch. That way upgrading JDK is a regular feature and does not require changes in Jenkins.

Is that possible today? Do you have the same problem? How do you fix it?

Gradle doesn’t manage this itself today.

Other users have solved this in the past by adding some content to the gradlew scripts as you suggest. Exactly how you do this depends on your platform, where you obtain JDKs from, where you install them too etc.

That is, the solution is likely to be custom to your environment.

Another solution would be to look at something like Docker for defining images in which you run your Gradle tests.