From 988dd1793e8d8e64a9d2e9f3cf293f1c257934af Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 26 Sep 2024 12:25:18 +0200 Subject: [PATCH] refactor(ort-utils): Introduce a static Java version property Align with `BUILD_JDK` to not require an `Environment` instance. Signed-off-by: Sebastian Schuberth --- .../gradle-inspector/src/funTest/kotlin/GradleFunTest.kt | 3 ++- .../gradle-inspector/src/main/kotlin/GradleInspector.kt | 2 +- .../gradle/src/funTest/kotlin/GradleFunTest.kt | 3 ++- plugins/package-managers/sbt/src/main/kotlin/Sbt.kt | 2 +- utils/ort/src/main/kotlin/Environment.kt | 9 +++++++-- .../src/main/kotlin/OrtScriptCompilationConfiguration.kt | 2 +- utils/test/src/main/kotlin/Utils.kt | 2 +- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/package-managers/gradle-inspector/src/funTest/kotlin/GradleFunTest.kt b/plugins/package-managers/gradle-inspector/src/funTest/kotlin/GradleFunTest.kt index 5086b6de26154..c9d01ef15671e 100644 --- a/plugins/package-managers/gradle-inspector/src/funTest/kotlin/GradleFunTest.kt +++ b/plugins/package-managers/gradle-inspector/src/funTest/kotlin/GradleFunTest.kt @@ -33,13 +33,14 @@ import org.ossreviewtoolkit.model.toYaml import org.ossreviewtoolkit.plugins.versioncontrolsystems.git.GitCommand import org.ossreviewtoolkit.utils.common.Os import org.ossreviewtoolkit.utils.common.ProcessCapture +import org.ossreviewtoolkit.utils.ort.Environment import org.ossreviewtoolkit.utils.test.getAssetFile import org.ossreviewtoolkit.utils.test.matchExpectedResult import org.ossreviewtoolkit.utils.test.patchActualResult class GradleFunTest : StringSpec() { private val projectDir = getAssetFile("projects/synthetic/gradle").toGradle() - private val isJava9OrAbove = System.getProperty("java.version").split('.').first().toInt() >= 9 + private val isJava9OrAbove = Environment.JAVA_VERSION.split('.').first().toInt() >= 9 private fun installGradleWrapper(version: String) { println("Installing Gradle wrapper version $version.") diff --git a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt index fcfc57fb54298..d38c53d450b39 100644 --- a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt +++ b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt @@ -183,7 +183,7 @@ class GradleInspector( val javaHome = options[OPTION_JAVA_VERSION]?.let { val requestedVersion = Semver.coerce(it) - val runningVersion = Semver.coerce(Environment().javaVersion) + val runningVersion = Semver.coerce(Environment.JAVA_VERSION) if (requestedVersion != runningVersion) { JavaBootstrapper.installJdk("TEMURIN", it) .onFailure { e -> diff --git a/plugins/package-managers/gradle/src/funTest/kotlin/GradleFunTest.kt b/plugins/package-managers/gradle/src/funTest/kotlin/GradleFunTest.kt index 91f32f97000f5..70af9a4269e93 100644 --- a/plugins/package-managers/gradle/src/funTest/kotlin/GradleFunTest.kt +++ b/plugins/package-managers/gradle/src/funTest/kotlin/GradleFunTest.kt @@ -33,13 +33,14 @@ import org.ossreviewtoolkit.model.toYaml import org.ossreviewtoolkit.plugins.versioncontrolsystems.git.GitCommand import org.ossreviewtoolkit.utils.common.Os import org.ossreviewtoolkit.utils.common.ProcessCapture +import org.ossreviewtoolkit.utils.ort.Environment import org.ossreviewtoolkit.utils.test.getAssetFile import org.ossreviewtoolkit.utils.test.matchExpectedResult import org.ossreviewtoolkit.utils.test.patchActualResult class GradleFunTest : StringSpec() { private val projectDir = getAssetFile("projects/synthetic/gradle") - private val isJava9OrAbove = System.getProperty("java.version").split('.').first().toInt() >= 9 + private val isJava9OrAbove = Environment.JAVA_VERSION.split('.').first().toInt() >= 9 private fun installGradleWrapper(version: String) { println("Installing Gradle wrapper version $version.") diff --git a/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt b/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt index 35f0a8b850ae5..f8a37996618e0 100644 --- a/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt +++ b/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt @@ -128,7 +128,7 @@ class Sbt( // https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html#build-tool-compatibility-table val javaHome = options[OPTION_JAVA_VERSION]?.let { val requestedVersion = Semver.coerce(it) - val runningVersion = Semver.coerce(Environment().javaVersion) + val runningVersion = Semver.coerce(Environment.JAVA_VERSION) if (requestedVersion != runningVersion) { JavaBootstrapper.installJdk("TEMURIN", it) .onFailure { e -> diff --git a/utils/ort/src/main/kotlin/Environment.kt b/utils/ort/src/main/kotlin/Environment.kt index ecbe6fd79d5f7..bb9ccd5582dfa 100644 --- a/utils/ort/src/main/kotlin/Environment.kt +++ b/utils/ort/src/main/kotlin/Environment.kt @@ -42,7 +42,7 @@ data class Environment( /** * The version of Java used to run ORT. */ - val javaVersion: String = System.getProperty("java.version"), + val javaVersion: String = JAVA_VERSION, /** * Name of the operating system, defaults to [Os.name]. @@ -86,9 +86,14 @@ data class Environment( JarFile(codeSource?.location?.file).use { it.manifest.mainAttributes.getValue("Build-Jdk") } - }.getOrDefault(System.getProperty("java.version")) + }.getOrDefault(JAVA_VERSION) } + /** + * The version of Java used to run ORT. + */ + val JAVA_VERSION: String by lazy { System.getProperty("java.version") } + /** * A string that is supposed to be used as the User Agent when using ORT as an HTTP client. */ diff --git a/utils/scripting/src/main/kotlin/OrtScriptCompilationConfiguration.kt b/utils/scripting/src/main/kotlin/OrtScriptCompilationConfiguration.kt index a05ae75ff7cd0..1a2a7a0e7cc34 100644 --- a/utils/scripting/src/main/kotlin/OrtScriptCompilationConfiguration.kt +++ b/utils/scripting/src/main/kotlin/OrtScriptCompilationConfiguration.kt @@ -41,7 +41,7 @@ import org.ossreviewtoolkit.utils.ort.Environment import org.ossreviewtoolkit.utils.ort.ortDataDirectory class OrtScriptCompilationConfiguration : ScriptCompilationConfiguration({ - compilerOptions("-jvm-target", Environment().javaVersion.substringBefore('.')) + compilerOptions("-jvm-target", Environment.JAVA_VERSION.substringBefore('.')) defaultImports( "org.apache.logging.log4j.kotlin.logger", diff --git a/utils/test/src/main/kotlin/Utils.kt b/utils/test/src/main/kotlin/Utils.kt index 2d7f211cd7b6f..62eaada38772e 100644 --- a/utils/test/src/main/kotlin/Utils.kt +++ b/utils/test/src/main/kotlin/Utils.kt @@ -135,7 +135,7 @@ fun patchActualResult( return custom.entries.fold(result) { text, (pattern, replacement) -> text.replace(pattern.toRegex(), replacement) } .replace(ORT_VERSION_REGEX) { "${it.groupValues[1]}: \"HEAD\"" } - .replace(JAVA_VERSION_REGEX) { "${it.groupValues[1]}: \"${System.getProperty("java.version")}\"" } + .replace(JAVA_VERSION_REGEX) { "${it.groupValues[1]}: \"${Environment.JAVA_VERSION}\"" } .replace(ENV_VAR_REGEX) { "${it.groupValues[1]} {}" } .replace(ENV_TOOL_REGEX) { "${it.groupValues[1]} {}" } .replace(TIMESTAMP_REGEX) { "${it.groupValues[1]}: \"${Instant.EPOCH}\"" }