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 gradle.properties 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/gradle.properties it applies to the whole use account,
- when stored at <current-project-root>/gradle.properties it applies just to the current project.
The property org.gradle.java.home
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 gradle.properties:
org.gradle.java.home=/opt/oracle/jdk1.7.0_45
Using the shell this is easily done with this command:
echo org.gradle.java.home=/opt/oracle/jdk1.7.0_45 >> gradle.properties
Since JDK installation paths vary from developer machine to developer machine do not check in gradle.properties into our SCM. We use git and mercurial as our main SCMs. So depending on the SCM add gradle.properties to either .gitignore
or .hgignore
. Using the shell this is easily done with one of these commands.
echo gradle.properties >> .gitignore echo gradle.properties >> .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 gradle.properties: 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 gradle.properties file.
How do you handle multiple projects depending on different JDK versions? Leave a comment!