Skip to content

Commit

Permalink
chore: use Gradle toolchains for JDK provisioning
Browse files Browse the repository at this point in the history
It enables automatic JDK provisioning (e.g. download), and it enables
using different JDKs for executing Gradle and for building JMeter

Java version can be specified when building with -PjdkVersion=11

You could use ./gradlew -q javaToolchains to list the detected toolchains.

See https://docs.gradle.org/8.0/userguide/toolchains.html#sec:consuming

Fixes apache#5986
  • Loading branch information
vlsi committed Jun 17, 2023
1 parent e024bf3 commit c65a2b4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
properties: |
testExtraJvmArgs=${{ matrix.testExtraJvmArgs }}
testDisableCaching=${{ matrix.testDisableCaching }}
# We provision JDKs with GitHub Actions for caching purposes, so Gradle should rather fail in case JDK is not found
org.gradle.java.installations.auto-download=false
env:
_JAVA_OPTIONS: ${{ matrix.extraJvmArgs }}
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GE_ACCESS_TOKEN }}
Expand Down
16 changes: 16 additions & 0 deletions build-logic/build-parameters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ buildParameters {
defaultValue.set(false)
description.set("Collect test coverage")
}
integer("targetJavaVersion") {
defaultValue.set(8)
mandatory.set(true)
description.set("Java version for source and target compatibility")
}
integer("jdkVersion") {
defaultValue.set(17)
mandatory.set(true)
description.set("JDK version to use for building JMeter. If the value is 0, then current Java is used. (see https://docs.gradle.org/8.0/userguide/toolchains.html#sec:consuming)")
}
string("jdkVendor") {
description.set("JDK vendor to use building JMeter (see https://docs.gradle.org/8.0/userguide/toolchains.html#sec:vendors)")
}
string("jdkImplementation") {
description.set("Vendor-specific virtual machine implementation to use building JMeter (see https://docs.gradle.org/8.0/userguide/toolchains.html#selecting_toolchains_by_virtual_machine_implementation)")
}
bool("spotbugs") {
defaultValue.set(false)
description.set("Run SpotBugs verifications")
Expand Down
19 changes: 16 additions & 3 deletions build-logic/jvm/src/main/kotlin/build-logic.java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ plugins {
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
buildParameters.jdkVersion.takeIf { it > 0 }?.let { jdkVersion ->
toolchain {
languageVersion.set(JavaLanguageVersion.of(jdkVersion))
buildParameters.jdkVendor.orNull?.let {
vendor.set(JvmVendorSpec.matching(it))
}
buildParameters.jdkImplementation.orNull?.let {
if (it.equals("J9", ignoreCase = true)) {
implementation.set(JvmImplementation.J9)
}
}
}
}
consistentResolution {
useCompileClasspathVersions()
}
Expand All @@ -44,7 +55,9 @@ tasks.configureEach<JavaCompile> {
// Use --release=8 for Java 10+ so the generated bytecode does not include methods introduced in Java 9+
options.release.set(
provider {
8.takeIf { javaCompiler.get().metadata.languageVersion.asInt() > 9 }
buildParameters.targetJavaVersion.takeIf {
javaCompiler.get().metadata.languageVersion.asInt() > 9
}
}
)
}
Expand Down
21 changes: 20 additions & 1 deletion build-logic/jvm/src/main/kotlin/build-logic.kotlin.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java-library")
id("build-logic.build-params")
id("build-logic.java")
id("build-logic.test-base")
id("com.github.autostyle")
Expand All @@ -36,6 +37,19 @@ kotlin {
if (props.bool("kotlin.explicitApi", default = true)) {
explicitApi()
}
buildParameters.jdkVersion.takeIf { it > 0 }?.let { jdkVersion ->
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(jdkVersion))
buildParameters.jdkVendor.orNull?.let {
vendor.set(JvmVendorSpec.matching(it))
}
buildParameters.jdkImplementation.orNull?.let {
if (it.equals("J9", ignoreCase = true)) {
implementation.set(JvmImplementation.J9)
}
}
}
}
}

tasks.configureEach<KotlinCompile> {
Expand All @@ -44,7 +58,12 @@ tasks.configureEach<KotlinCompile> {
apiVersion = "kotlin.api".v
}
freeCompilerArgs += "-Xjvm-default=all"
jvmTarget = java.targetCompatibility.toString()
kotlinOptions.jvmTarget = buildParameters.targetJavaVersion.let {
when {
it < 9 -> "1.8"
else -> it.toString()
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pluginManagement {
plugins {
id("com.gradle.enterprise") version "3.13.2"
id("com.gradle.common-custom-user-data-gradle-plugin") version "1.10"
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}

dependencyResolutionManagement {
Expand Down Expand Up @@ -164,6 +165,10 @@ val expectedSha512 = mapOf(
to "okhttp-4.1.0.jar",
"93E7A41BE44CC17FB500EA5CD84D515204C180AEC934491D11FC6A71DAEA761FB0EECEF865D6FD5C3D88AAF55DCE3C2C424BE5BA5D43BEBF48D05F1FA63FA8A7"
to "okio-2.2.2.jar",
"B9F87DECE28EABCCEDA58C77C3B602AEAE7A8AEF3D30DA838F4924A620B18C05D9DF86C5876BDE8AB5597C8C0CE808AD083CAF89C3A5AAC60C1E980C6C144A17"
to "foojay-resolver-0.5.0.jar",
"10BF91C79AB151B684834E3CA8BA7D7E19742A3EEB580BDE690FBA433F9FFFE3ABBD79ED3FE3F97986C3A2BADC4D14E28835A8EF89167B4B9CC6014242338769"
to "gson-2.9.1.jar",
settings.extra["com.github.vlsi.checksum-dependency.sha512"].toString()
to "checksum-dependency-plugin.jar"
)
Expand Down

0 comments on commit c65a2b4

Please sign in to comment.