-
Notifications
You must be signed in to change notification settings - Fork 319
Drop the requirement of jvm environment variables for testJvm constraint
#9968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35a24a3
chore: Drop org.gradle.java.installations.fromEnv toolchain restriction
bric3 470c6e7
chore: Drop org.gradle.java.installations toolchain restriction for l…
bric3 c889363
chore: Drop org.gradle.java.installations toolchain restriction for l…
bric3 1104f19
feat: Allow discovery of testJvm without environment variables
bric3 a690268
feat: Only rely on Gradle's toolchain to run tests
bric3 501af3d
fix: Auto discover JAVA_xx_HOME variables from the build image
bric3 2214cb2
typo: Use `-P`
bric3 e5d8ca2
Merge remote-tracking branch 'origin/master' into bdu/avoids-need-for…
bric3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 123 additions & 49 deletions
172
buildSrc/src/main/kotlin/datadog/gradle/plugin/testJvmConstraints/TestJvmSpec.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,162 @@ | ||
| package datadog.gradle.plugin.testJvmConstraints | ||
|
|
||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.internal.provider.PropertyFactory | ||
| import org.gradle.api.provider.Provider | ||
| import org.gradle.jvm.toolchain.JavaLanguageVersion | ||
| import org.gradle.jvm.toolchain.JavaLauncher | ||
| import org.gradle.jvm.toolchain.JavaToolchainService | ||
| import org.gradle.jvm.toolchain.JavaToolchainSpec | ||
| import org.gradle.jvm.toolchain.JvmImplementation | ||
| import org.gradle.jvm.toolchain.JvmVendorSpec | ||
| import org.gradle.jvm.toolchain.internal.DefaultToolchainSpec | ||
| import org.gradle.jvm.toolchain.internal.SpecificInstallationToolchainSpec | ||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.Paths | ||
|
|
||
|
|
||
| /** | ||
| * Handles the `testJvm` property to resolve a Java launcher for testing. | ||
| * | ||
| * The `testJvm` property can be set via command line or environment variable to specify | ||
| * which JVM to use for running tests. E.g. | ||
| * | ||
| * ```shell | ||
| * ./gradlew test -PtestJvm=ZULU11 | ||
| * ``` | ||
| * | ||
| * This handles local setup, and CI environment, where the environment variables are defined here: | ||
| * * https://github.com/DataDog/dd-trace-java-docker-build/blob/a4f4bfa9d7fe0708858e595697dc67970a2a458f/Dockerfile#L182-L188 | ||
| * * https://github.com/DataDog/dd-trace-java-docker-build/blob/a4f4bfa9d7fe0708858e595697dc67970a2a458f/Dockerfile#L222-L241 | ||
| */ | ||
| class TestJvmSpec(val project: Project) { | ||
| companion object { | ||
| const val TEST_JVM = "testJvm" | ||
| } | ||
|
|
||
| private val currentJavaHomePath = project.providers.systemProperty("java.home").map { it.normalizeToJDKJavaHome() } | ||
|
|
||
| val testJvmProperty = project.providers.gradleProperty(TEST_JVM) | ||
|
|
||
| val normalizedTestJvm = testJvmProperty.map { testJvm -> | ||
| /** | ||
| * The raw `testJvm` property as passed via command line or environment variable. | ||
| */ | ||
| val testJvmProperty: Provider<String> = project.providers.gradleProperty(TEST_JVM) | ||
|
|
||
| /** | ||
| * Normalized `stable` string to the highest JAVA_X_HOME found in environment variables. | ||
| */ | ||
| val normalizedTestJvm: Provider<String> = testJvmProperty.map { testJvm -> | ||
| if (testJvm.isBlank()) { | ||
| throw GradleException("testJvm property is blank") | ||
| } | ||
|
|
||
| // "stable" is calculated as the largest X found in JAVA_X_HOME | ||
| if (testJvm == "stable") { | ||
| val javaVersions = project.providers.environmentVariablesPrefixedBy("JAVA_").map { javaHomes -> | ||
| javaHomes | ||
| .filter { it.key.matches(Regex("^JAVA_[0-9]+_HOME$")) } | ||
| .map { Regex("^JAVA_(\\d+)_HOME$").find(it.key)!!.groupValues[1].toInt() } | ||
| }.get() | ||
|
|
||
| if (javaVersions.isEmpty()) { | ||
| throw GradleException("No valid JAVA_X_HOME environment variables found.") | ||
| when (testJvm) { | ||
| "stable" -> { | ||
| val javaVersions = project.providers.environmentVariablesPrefixedBy("JAVA_").map { javaHomes -> | ||
| javaHomes | ||
| .filter { it.key.matches(Regex("^JAVA_[0-9]+_HOME$")) } | ||
| .map { Regex("^JAVA_(\\d+)_HOME$").find(it.key)!!.groupValues[1].toInt() } | ||
| }.get() | ||
|
|
||
| if (javaVersions.isEmpty()) { | ||
| throw GradleException("No valid JAVA_X_HOME environment variables found.") | ||
| } | ||
|
|
||
| javaVersions.max().toString() | ||
| } | ||
|
|
||
| javaVersions.max().toString() | ||
| } else { | ||
| testJvm | ||
| else -> testJvm | ||
| } | ||
| }.map { project.logger.info("normalized testJvm: $it"); it } | ||
|
|
||
| val testJvmHomePath = normalizedTestJvm.map { | ||
| if (Files.exists(Paths.get(it))) { | ||
| it.normalizeToJDKJavaHome() | ||
| } else { | ||
| val matcher = Regex("([a-zA-Z]*)([0-9]+)").find(it) | ||
| if (matcher == null) { | ||
| throw GradleException("Unable to find launcher for Java '$it'. It needs to match '([a-zA-Z]*)([0-9]+)'.") | ||
| } | ||
| val testJvmEnv = "JAVA_${it}_HOME" | ||
| val testJvmHome = project.providers.environmentVariable(testJvmEnv).orNull | ||
| if (testJvmHome == null) { | ||
| throw GradleException("Unable to find launcher for Java '$it'. Have you set '$testJvmEnv'?") | ||
| /** | ||
| * The home path of the test JVM. | ||
| * | ||
| * The `<testJvm>` string (`8`, `11`, `ZULU8`, `GRAALVM25`, etc.) is interpreted in that order: | ||
| * 1. Lookup for a valid path, | ||
| * 2. Look JVM via Gradle toolchains | ||
| * | ||
| * Holds the resolved JavaToolchainSpec for the test JVM. | ||
| */ | ||
| private val testJvmSpec = normalizedTestJvm.map { | ||
| val (distribution, version) = Regex("([a-zA-Z]*)([0-9]+)").matchEntire(it)?.groupValues?.drop(1) ?: listOf("", "") | ||
|
|
||
| when { | ||
| Files.exists(Paths.get(it)) -> it.normalizeToJDKJavaHome().toToolchainSpec() | ||
|
|
||
| version.isNotBlank() -> { | ||
| // Best effort to make a spec for the passed testJvm | ||
| // `8`, `11`, `ZULU8`, `GRAALVM25`, etc. | ||
| // if it is an integer, we assume it's a Java version | ||
| // also we can handle on macOs oracle, zulu, semeru, graalvm prefixes | ||
|
|
||
| // This is using internal APIs | ||
| DefaultToolchainSpec(project.serviceOf<PropertyFactory>()).apply { | ||
| languageVersion.set(JavaLanguageVersion.of(version.toInt())) | ||
| when (distribution.lowercase()) { | ||
| "oracle" -> { | ||
| vendor.set(JvmVendorSpec.ORACLE) | ||
| } | ||
|
|
||
| "zulu" -> { | ||
| vendor.set(JvmVendorSpec.AZUL) | ||
| } | ||
|
|
||
| "semeru" -> { | ||
| vendor.set(JvmVendorSpec.IBM) | ||
| implementation.set(JvmImplementation.J9) | ||
| } | ||
|
|
||
| "graalvm" -> { | ||
| vendor.set(JvmVendorSpec.GRAAL_VM) | ||
| nativeImageCapable.set(true) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| testJvmHome.normalizeToJDKJavaHome() | ||
| else -> throw GradleException( | ||
| """ | ||
| Unable to find launcher for Java '$it'. It needs to be: | ||
| 1. A valid path to a JDK home, or | ||
| 2. An environment variable named 'JAVA_<testJvm>_HOME' or '<testJvm>' pointing to a JDK home, or | ||
| 3. A Java version or a known distribution+version combination (e.g. '11', 'zulu8', 'graalvm11', etc.) that can be resolved via Gradle toolchains. | ||
| 4. If using Gradle toolchains, ensure that the requested JDK is installed and configured correctly. | ||
| """.trimIndent() | ||
| ) | ||
| } | ||
| }.map { project.logger.info("testJvm home path: $it"); it } | ||
|
|
||
| val javaTestLauncher = project.providers.zip(testJvmHomePath, normalizedTestJvm) { testJvmHome, testJvm -> | ||
| // Only change test JVM if it's not the one we are running the gradle build with | ||
| if (currentJavaHomePath.get() == testJvmHome) { | ||
| project.providers.provider<JavaLauncher?> { null } | ||
| } else { | ||
| // This is using internal APIs | ||
| val jvmSpec = org.gradle.jvm.toolchain.internal.SpecificInstallationToolchainSpec( | ||
| project.serviceOf<org.gradle.api.internal.provider.PropertyFactory>(), | ||
| project.file(testJvmHome) | ||
| ) | ||
|
|
||
| // The provider always says that a value is present so we need to wrap it for proper error messages | ||
| project.javaToolchains.launcherFor(jvmSpec).orElse(project.providers.provider { | ||
| throw GradleException("Unable to find launcher for Java $testJvm. Does '$testJvmHome' point to a JDK?") | ||
| }) | ||
| } | ||
| }.flatMap { it }.map { project.logger.info("testJvm launcher: ${it.executablePath}"); it } | ||
| /** | ||
| * The Java launcher for the test JVM. | ||
| * | ||
| * Current JVM or a launcher specified via the testJvm. | ||
| */ | ||
| val javaTestLauncher: Provider<JavaLauncher> = | ||
| project.providers.zip(testJvmSpec, normalizedTestJvm) { jvmSpec, testJvm -> | ||
| // Only change test JVM if it's not the one we are running the gradle build with | ||
| if ((jvmSpec as? SpecificInstallationToolchainSpec)?.javaHome == currentJavaHomePath.get()) { | ||
| project.providers.provider<JavaLauncher?> { null } | ||
| } else { | ||
| // The provider always says that a value is present so we need to wrap it for proper error messages | ||
| project.javaToolchains.launcherFor(jvmSpec).orElse(project.providers.provider { | ||
| throw GradleException("Unable to find launcher for Java '$testJvm'. Does $TEST_JVM point to a JDK?") | ||
| }) | ||
| } | ||
| }.flatMap { it }.map { project.logger.info("testJvm launcher: ${it.executablePath}"); it } | ||
|
|
||
| private fun String.normalizeToJDKJavaHome(): Path { | ||
| val javaHome = project.file(this).toPath().toRealPath() | ||
| return if (javaHome.endsWith("jre")) javaHome.parent else javaHome | ||
| } | ||
|
|
||
| private val Project.javaToolchains: JavaToolchainService get() = | ||
| extensions.getByName("javaToolchains") as JavaToolchainService | ||
| private fun Path.toToolchainSpec(): JavaToolchainSpec = | ||
| // This is using internal APIs | ||
| SpecificInstallationToolchainSpec(project.serviceOf<PropertyFactory>(), project.file(this)) | ||
|
|
||
| private val Project.javaToolchains: JavaToolchainService | ||
| get() = | ||
| extensions.getByName("javaToolchains") as JavaToolchainService | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we rename
stable->latest? or similar?stableis kind of misleading to me, WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant to that right now, as there were prior discussions on this.
Maybe we can have both, like
lastStable:D