Skip to content

Commit

Permalink
build: require a fixed 'major.minor.patch' Java version to be install…
Browse files Browse the repository at this point in the history
…ed (#13108)

Signed-off-by: Jendrik Johannes <[email protected]>
  • Loading branch information
jjohannes authored May 16, 2024
1 parent 474f5ff commit 109caa1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,29 @@ plugins {
version =
providers.fileContents(rootProject.layout.projectDirectory.versionTxt()).asText.get().trim()

// Fail the build if Gradle is started with an old version of Java
val MIN_JAVA_VERSION = JavaVersion.VERSION_21
val CUR_JAVA_VERSION = JavaVersion.current()

if (CUR_JAVA_VERSION.ordinal < MIN_JAVA_VERSION.ordinal) {
throw StopExecutionException(
"ERROR: Gradle is started with Java " +
CUR_JAVA_VERSION +
". This project requires running Gradle with Java " +
MIN_JAVA_VERSION +
" or above." +
" Please check your JAVA_HOME and/or PATH and configure the default JDK to use Java version " +
MIN_JAVA_VERSION +
" or above."
)
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")
}
}

java {
sourceCompatibility = MIN_JAVA_VERSION
targetCompatibility = MIN_JAVA_VERSION

toolchain {
languageVersion = JavaLanguageVersion.of(21)
vendor = JvmVendorSpec.ADOPTIUM
}
sourceCompatibility = javaVersionMajor
targetCompatibility = javaVersionMajor
}

configurations.all {
Expand Down Expand Up @@ -148,9 +146,10 @@ tasks.withType<JavaCompile>().configureEach {
// By default, Gradle only tracks the major version as defined in the toolchain (e.g. 17).
// Since the full version is encoded in 'module-info.class' files, it should be tracked as
// it otherwise leads to wrong build cache hits.
inputs.property("fullJavaVersion", providers.systemProperty("java.version"))
inputs.property("fullJavaVersion", currentJavaVersion)

options.encoding = "UTF-8"
options.isFork = true // run compiler in separate JVM process (independent of toolchain setup)

doLast {
// Make sure consistent line ending are used in files generated by annotation processors by
Expand Down
5 changes: 1 addition & 4 deletions build-logic/settings-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@

plugins { `kotlin-dsl` }

dependencies {
implementation("org.gradle.toolchains:foojay-resolver:0.7.0")
implementation("com.gradle:gradle-enterprise-gradle-plugin:3.15.1")
}
dependencies { implementation("com.gradle:gradle-enterprise-gradle-plugin:3.15.1") }
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ pluginManagement {
}
}

plugins {
id("com.gradle.enterprise")
id("org.gradle.toolchains.foojay-resolver-convention")
}
plugins { id("com.gradle.enterprise") }

// Enable Gradle Build Scan
gradleEnterprise {
Expand Down

0 comments on commit 109caa1

Please sign in to comment.