33

One of my project requires Java 1.8, but sometimes we didn't notice we are using older java so that we will get some strange errors.

I want to add the checking in build.gradle, so that when we run any task, it will firstly check the version, and prints error and quit immediately.

I tried to add the checking directly in build.gradle on the first line, but it still do some others tasks e.g. (clean, compileJava) before the checking happens, when I run:

$ ./gradlew

How to do it correctly?

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Gradle build script is parsed and executed as it is. So if you put your check directly into script, not in some task, it should be executed at once. Can you share the relevant part of the build.gradle file? – Nikem Jan 21 '15 at 06:43
  • @Nikem, thanks, just found there is a `buildSrc` directory under the project root, and the tasks defined in it are running before my checking code. It's fine to let them running first – Freewind Jan 21 '15 at 06:55
  • 1
    @Freewind, there's no need to use `buildSrc` here. Just add the code checking version of java at the very beginning of `build.gradle` script. – Opal Jan 21 '15 at 10:01

2 Answers2

56

If you put the check very early in your build lifecycle (plain check in the beginning of your build.gradle file or in the apply method of a plugin) you shouldn't see any tasks executed.

you can use JavaVersion enum for that which is part of the gradle api:

if(JavaVersion.current() != JavaVersion.VERSION_1_8){
    throw new GradleException("This build must be run with java 8")
}
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • It could also be checked sooner, like in the beginning of your `settings.gradle`. – eskatos Jul 30 '15 at 19:29
  • well if you want to check it really soon. put it in a init script ;-) – Rene Groeschke Jul 30 '15 at 20:00
  • Sure. Note that init scripts either force you to add `--init-script your-script` to your command lines or must sit outside the project (in $HOME or $GRADLE_HOME). See https://docs.gradle.org/current/userguide/init_scripts.html – eskatos Jul 30 '15 at 21:50
12

The accepted answer is nice however it could be improved a little bit by making it more generic. Indeed instead of comparing the current version with an explicit version, we could rely on the value of targetCompatibility instead (assuming it has been set properly) as next:

if (JavaVersion.current() != project.targetCompatibility) {
    throw new GradleException("The java version used ${JavaVersion.current()} is not the expected version ${project.targetCompatibility}.")
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • And how does one set `targetCompatibility`? edit: Just found https://stackoverflow.com/a/44799256/119790 – Ian Vaughan Dec 12 '18 at 23:04
  • Beware that order of execution is important when checking against current project settings: Checks like the above might be used pretty early in the build file, in which case `project.targetCompatibility` might still be the current java version and the tests succeeds. That can't happen with comparing against an explicit version. – Thorsten Schöning Jun 22 '20 at 09:53