How to enforce a java compiler version with gradle?
Asked Answered
M

3

18

In all of my projects, I use gradle and specify the following:

sourceCompatibility = "1.7"; // for example
targetCompatibility = "1.7"; // defaults to sourceCompatibility

Now, I have three different versions of the JDK installed, from 1.6 to 1.8. In order to switch from one version to another, I source shell files to change PATH, JAVA_HOME and even JDK_HOME.

By accident it can happen that I use the wrong JDK version and I don't want that... Is there a possibility to check that the compiler version is equal to targetCompatibility before attempting any compilation task?

Mahla answered 21/1, 2015 at 10:57 Comment(0)
B
17

I use the following:

task checkJavaVersion << {
    if (!JavaVersion.current().isJava6()) {
        String message = "ERROR: Java 1.6 required but " +
                          JavaVersion.current() +
                          " found. Change your JAVA_HOME environment variable.";
        throw new IllegalStateException(message);
    }
}

compileJava.dependsOn checkJavaVersion
Bunion answered 21/1, 2015 at 11:2 Comment(1)
Hmm, interesting... Do you happen to know by any chance (otherwise I'll search it) whether it is possible to use JavaVersion instances as arguments to *Compatibility?Mahla
M
22

Answer to self, and thanks to @JBNizet for providing the initial solution...

The solution is indeed to use JavaVersion, and it happens that both sourceCompatibility and targetCompatibility accept a JavaVersion as an argument...

Therefore the build file has become this:

def javaVersion = JavaVersion.VERSION_1_7;
sourceCompatibility = javaVersion;
targetCompatibility = javaVersion; // defaults to sourceCompatibility

And then the task:

task enforceVersion << {
    def foundVersion = JavaVersion.current();
    if (foundVersion != javaVersion) 
        throw new IllegalStateException("Wrong Java version; required is "
            + javaVersion + ", but found " + foundVersion);
}

compileJava.dependsOn(enforceVersion);

And it works:

$ ./gradlew clean compileJava
:clean UP-TO-DATE
:enforceVersion FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/fge/src/perso/grappa-tracer-backport/build.gradle' line: 55

* What went wrong:
Execution failed for task ':enforceVersion'.
> Wrong Java version; required is 1.7, but found 1.8
Mahla answered 21/1, 2015 at 11:50 Comment(1)
Thanks for sharing this! Very helpful.. In my case however, I implemented this with a check to see if (foundVersion.isJava7Compatible()) { ... }. Just wanted to share because I thought it was nifty. :)Wassail
B
17

I use the following:

task checkJavaVersion << {
    if (!JavaVersion.current().isJava6()) {
        String message = "ERROR: Java 1.6 required but " +
                          JavaVersion.current() +
                          " found. Change your JAVA_HOME environment variable.";
        throw new IllegalStateException(message);
    }
}

compileJava.dependsOn checkJavaVersion
Bunion answered 21/1, 2015 at 11:2 Comment(1)
Hmm, interesting... Do you happen to know by any chance (otherwise I'll search it) whether it is possible to use JavaVersion instances as arguments to *Compatibility?Mahla
B
2

If you want the version to be checked for all tasks, you can add an assertion in build.gradle to enforce it:

assert JavaVersion.current().isJava9Compatible(): "Java 9 or newer is required"

With Gradle 5.4.1, the failure looks like this:

$ ./gradlew --quiet build
FAILURE: Build failed with an exception.

* Where:
Build file '/home/codehearts/build.gradle' line: 15

* What went wrong:
A problem occurred evaluating root project 'Codehearts'.
> Java 9 or newer is required. Expression: org.gradle.api.JavaVersion.current().isJava9Compatible()

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

You can also check exact versions if needed:

assert JavaVersion.current().isJava9(): "Java 9 is required"
Ballflower answered 15/5, 2019 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.