Skip to content

Commit

Permalink
chore: only warn for Java versions that do not break the build
Browse files Browse the repository at this point in the history
Signed-off-by: Jendrik Johannes <[email protected]>
  • Loading branch information
jjohannes committed May 15, 2024
1 parent 4a5064d commit fbea6a7
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,29 @@ plugins {
version =
providers.fileContents(rootProject.layout.projectDirectory.versionTxt()).asText.get().trim()

// Fail the build if Gradle is started with a Java version that does not exactly match
val javaVersion = "21.0.1"
val javaVersionMajor = JavaVersion.VERSION_21
val javaVersionPatch = "0.1"

val currentJavaVersionMajor = JavaVersion.current()
val currentJavaVersion = providers.systemProperty("java.version").get()
val expectedJavaVersion = "$javaVersionMajor.$javaVersionPatch"

if (currentJavaVersion != expectedJavaVersion) {
val message =
"Gradle runs with Java $currentJavaVersion. This project works best running with Java $expectedJavaVersion. " +
"\n - From commandline: change JAVA_HOME and/or PATH to point at Java $expectedJavaVersion installation." +
"\n - From IntelliJ: change 'Gradle JVM' in 'Gradle Settings' to point at Java $expectedJavaVersion installation."

if (currentJavaVersionMajor.ordinal < javaVersionMajor.ordinal) { // fail if version is too old
throw (RuntimeException(message))
} else {
logger.lifecycle("WARN: $message")
}
}

if (currentJavaVersion != javaVersion) {
throw RuntimeException(
"Gradle runs with Java $currentJavaVersion. This project requires Gradle to run with Java $javaVersion. " +
"\n - From commandline: change JAVA_HOME and/or PATH to point at Java $javaVersion installation." +
"\n - From IntelliJ: change 'Gradle JVM' in 'Gradle Settings' to point at Java $javaVersion installation."
)
java {
sourceCompatibility = javaVersionMajor
targetCompatibility = javaVersionMajor
}

configurations.all {
Expand Down

0 comments on commit fbea6a7

Please sign in to comment.