0

I read this post

How do I tell Gradle to use specific JDK version?

which seems to miss the point. Developers should be responsible for setting their own JAVA_HOME to where they installed it which is different on different OS.

What I would like is to set which jdk major version should be in use like 1.8 so it fails if their JAVA_HOME is pointing to 1.7 with a nice error message saying please fix your JAVA_HOME variable.

Is this possible in gradle?

The important part is to have something in my git repo that says gradle.jdk.must.be=1.8 and if using JAVA_HOME OR using gradle.properties from their home directory or whatever, it tries to compile using JAVA_HOME and if it fails, it should tell them nicely to setup JAVA_HOME or gradle.properties or whatever.

Related post: How to set JDK version in Gradle project WITHOUT explicit JDK path?

The point being when someone checks out a project, it is sort of confusing when it just doesn't compile and they don't know why with no error telling them what to fix.

thanks, Dean

Community
  • 1
  • 1
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

1 Answers1

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

compileJava.dependsOn checkJavaVersion
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • awesome, works like a charm on the command line. (gradle plugin still imports it just fine with no messages :( but oh well, close enough). – Dean Hiller Apr 19 '16 at 01:14