Skip to content

Commit

Permalink
chore: set Java LTS release for build script compilation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vlsi committed Nov 24, 2023
1 parent 15f5ca0 commit d077087
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions build-logic-commons/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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<JavaCompile>().configureEach {
options.release.set(JavaVersion.toVersion(latestSupportedLts).majorVersion.toInt())
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = latestSupportedLts
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<JavaCompile>().configureEach {
options.release.set(JavaVersion.toVersion(latestSupportedLts).majorVersion.toInt())
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = latestSupportedLts
}
}
}

0 comments on commit d077087

Please sign in to comment.