From d077087997dbf36c5f6a055a01286d173d4cb1b1 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Fri, 24 Nov 2023 11:48:58 +0300 Subject: [PATCH] chore: set Java LTS release for build script compilation Previously the build failed if the default Java was newer than 20. Now we explicitly specify jvmTarget for the build script compilation. Note: it does not impact the target JVM for the resulting JMeter binaries. --- .../gradle-plugin/build.gradle.kts | 23 +++++++++++++++++++ ...-logic.kotlin-dsl-gradle-plugin.gradle.kts | 21 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/build-logic-commons/gradle-plugin/build.gradle.kts b/build-logic-commons/gradle-plugin/build.gradle.kts index 59d18a29ea1..90f37494f4c 100644 --- a/build-logic-commons/gradle-plugin/build.gradle.kts +++ b/build-logic-commons/gradle-plugin/build.gradle.kts @@ -16,6 +16,8 @@ */ import org.gradle.kotlin.dsl.support.expectedKotlinDslPluginsVersion +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { `kotlin-dsl` @@ -29,4 +31,25 @@ dependencies { // to make it work. // See https://github.com/gradle/gradle/issues/17016 regarding expectedKotlinDslPluginsVersion implementation("org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:$expectedKotlinDslPluginsVersion") + // It seems to be the best way to make KotlinCompile available for use in build-logic.kotlin-dsl-gradle-plugin.gradle.kts + implementation("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:$embeddedKotlinVersion") +} + +// We need to figure out a version that is supported by the current JVM, and by the Kotlin Gradle plugin +val currentJava = JavaVersion.current() +if (currentJava > JavaVersion.VERSION_1_8) { + // We want an LTS Java release for build script compilation + val latestSupportedLts = listOf("25", "21", "17", "11") + .intersect(JvmTarget.values().mapTo(mutableSetOf()) { it.target }) + .first { JavaVersion.toVersion(it) <= currentJava } + + tasks.withType().configureEach { + options.release.set(JavaVersion.toVersion(latestSupportedLts).majorVersion.toInt()) + } + + tasks.withType().configureEach { + kotlinOptions { + jvmTarget = latestSupportedLts + } + } } diff --git a/build-logic-commons/gradle-plugin/src/main/kotlin/build-logic.kotlin-dsl-gradle-plugin.gradle.kts b/build-logic-commons/gradle-plugin/src/main/kotlin/build-logic.kotlin-dsl-gradle-plugin.gradle.kts index 449e3c9b7c3..ee27e739c3c 100644 --- a/build-logic-commons/gradle-plugin/src/main/kotlin/build-logic.kotlin-dsl-gradle-plugin.gradle.kts +++ b/build-logic-commons/gradle-plugin/src/main/kotlin/build-logic.kotlin-dsl-gradle-plugin.gradle.kts @@ -15,6 +15,9 @@ * limitations under the License. */ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + plugins { id("java-library") id("org.gradle.kotlin.kotlin-dsl") // this is 'kotlin-dsl' without version @@ -24,3 +27,21 @@ tasks.validatePlugins { failOnWarning.set(true) enableStricterValidation.set(true) } + +val currentJava = JavaVersion.current() +if (currentJava > JavaVersion.VERSION_1_8) { + // We want an LTS Java release for build script compilation + val latestSupportedLts = listOf("25", "21", "17", "11") + .intersect(JvmTarget.values().mapTo(mutableSetOf()) { it.target }) + .first { JavaVersion.toVersion(it) <= currentJava } + + tasks.withType().configureEach { + options.release.set(JavaVersion.toVersion(latestSupportedLts).majorVersion.toInt()) + } + + tasks.withType().configureEach { + kotlinOptions { + jvmTarget = latestSupportedLts + } + } +}