Gradle Defaults

Some of our older Java projects are still stuck on Java 1.6 due to restrictions imposed by the production environment. More recent projects use Java 1.7. For our developers this means multiple versions of the JDK need to be available. But only one can be the system wide default. So we use a current JRE as the system wide default and keep various JDK version in /opt. But how do we select the appropriate JDK for building a specific project? Well we use gradle as our build tool and it has a nifty feature.
The file keeps some handy settings which can either be applied to the whole user account or just a single project depending where it is stores:

  • when stored at ~/.gradle/ it applies to the whole use account,
  • when stored at <current-project-root>/ it applies just to the current project.

The property holds the path to the build JDK. So if your JDK is located at /opt/oracle/jdk1.7.0_45 just add this line to your

Using the shell this is easily done with this command:

echo >>

Since JDK installation paths vary from developer machine to developer machine do not check in into our SCM. We use git and mercurial as our main SCMs. So depending on the SCM add to either .gitignore or .hgignore. Using the shell this is easily done with one of these commands.

echo >> .gitignore
echo >> .hgignore

To make sure we build with the intended JDK we add these line to our build.gradle file:

sourceCompatibility = '1.7'  // or '1.6'
targetCompatibility = '1.7'  // or '1.6'

There is one more property our developers usually keep in the account wide org.gradle.daemon=true. This enables the gradle daemon for all project builds. The daemon can significantly improve the startup and execution time of Gradle. For other properties, checkout the documentation of the file.

How do you handle multiple projects depending on different JDK versions? Leave a comment!