Can you set org.gradle.java.home with environment variable

Hi I want to set the JDK in gradle.properties using the property “org.gradle.java.home” but I wanted to avoid a hardcoded location (as a lot of developers install the JDK to different locations and its also different on our CI server).

I know this setting doesn’t work (I get a Dir does not exist error), but is anything like this possible?

org.gradle.java.home=${env.JAVA6_HOME}

On our CI server (Jenkins) the “JAVA_HOME” environment variable is set appropriately for each build. However when developers build locally they often forget to amend the “JAVA_HOME” environment variable if they are switching from a Java 7 project to a Java 6 one.

I want to ensure the build is run with the appropriate JDK (I don’t want to compile using Java 7 with the compatibility set to 6).

I know I could edit the gradle wrapper batch file but wanted to avoid that as the change could easily get overwritten if we re-generate the wrapper files.

If I can’t do this with a setting in gradle.properties I was thinking of failing the build if the appropriate java version isn’t found. That would at least force developers to update JAVA_HOME to run the build. Does this seem like a reasonable approach or is their a better (or more standard) way to do this?

task checkJavaVersion << {
    def javaVersion = System.getProperty("java.version")
    //fail the build if we don't have the expected Java version
    if (!javaVersion.startsWith("1.6")) {
        throw new GradleException("Please update JAVA_HOME to use a 1.6 JDK")
    }
 }

There’s no way I can think of to set the java version except without specifying the java home. And the only way to set the java home is via a properties file, since it needs to be parsed before the buildscript is loaded (so we know which JDK to use to run the build!).

One option would be to leave your ‘checkJavaVersion’ in place, but recommend that developers use a ‘gradle.properties’ located in their Gradle user home directory to configure ‘org.gradle.java.home’. That way, they don’t need to reconfigure JAVA_HOME each time.

More about configuring the build environment at: http://www.gradle.org/docs/current/userguide/userguide_single.html#build_environment

Specifying required java version would certainly make a nice feature, though. Gradle could check for an ‘org.gradle.java.version’ property and: 1. Check if the current JDK satisfies the version 2. If not matching, attempt to find a JDK for that version, and launch a single-use daemon with that JDK.

We already launch a single-use daemon if ‘org.gradle.java.home’ doesn’t match, and we already have code in our test suite that locates available JDKs.

Contributions welcome!

OK thanks for the quick response. I thought as much.

An org.gradle.java.version property does sound like a good idea - if I ever get any spare time I might look into that.

Does the gradle source code build OK in Eclipse or would I need IntelliJ IDEA?

Thanks.

I know it works well in IDEA, after executing ‘gradle idea’ to setup the projects. Not sure if any of the team members use Eclipse, so it might require some tweaking. There’s no reason in theory that you can’t develop Gradle using Eclipse, however.

Wouldn’t ‘org.gradle.java.home’ be global to set even the compiler bootstrap classpath default? I am running Gradle 2.0 and I had to set both properties and bootstrap classpath.