Is there an built-in or elegant way to require a minimum JVM version for running a build script?


(Sean Gilligan) #1

I would like to be able to define a minimum JVM version for the Gradle buildscript (build.gradle) itself. Is there an built-in or elegant way to do this?

This issue came up while trying to build Terasology on a JDK 6 system: https://github.com/MovingBlocks/Terasology/issues/687

The best solution I can come up with is:

if (!org.gradle.api.JavaVersion.current().isJava7Compatible()) {

// The build script (this file) requires Java 7+, throw error if not present

throw new GradleScriptException(“Build script requires Java 7”, null)

}

It would be nice if there were a buildScriptCompatibility property that is set to a JavaVersion. Is there something like that?


(Ben Manes) #2

I use,

assert System.properties['java.specification.version'] == '1.7'

Assert displays a nice error message automatically.


(Sean Gilligan) #3

Yeah, Andrew is right. I want Java7 or later, not exactly Java 7. Looks like that’s what I’m going to submit in a pull request for Terasology.

I’m thinking about opening an enhancement issue for Gradle asking for a project property for defining a JavaVersion for the buildscript itself, something like:

buildScriptCompatibility = 1.7

// Java version required to run the buildscript

Comments?


(Sean Gilligan) #4

Looks like I can’t submit Jira issues, so I created a problem report on this forum that will hopefully convince someone to open one: http://forums.gradle.org/gradle/topics/gradle_build_files_or_projects_should_have_a_property_specifying_the_minimum_javaversion_to_run_the_build_script


(Andrew Oberstar) #5

Combination of both of your ideas to get the one liner and the compatible vs exact version:

assert org.gradle.api.JavaVersion.current().isJava7Compatible()