Skip to content

Commit

Permalink
chore: require a fixed 'major.minor.patch' Java version to be installed
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 c428dca commit 4a5064d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,18 @@ 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."
// Fail the build if Gradle is started with a Java version that does not exactly match
val javaVersion = "21.0.1"
val currentJavaVersion = providers.systemProperty("java.version").get()

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 = MIN_JAVA_VERSION
targetCompatibility = MIN_JAVA_VERSION

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

configurations.all {
// In case published versions of a module are also available, always prefer the local one
resolutionStrategy.preferProjectModules()
Expand Down Expand Up @@ -148,9 +133,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 4a5064d

Please sign in to comment.