Skip to content

Commit

Permalink
refactor(ort-utils): Introduce a static Java version property
Browse files Browse the repository at this point in the history
Align with `BUILD_JDK` to not require an `Environment` instance.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Sep 27, 2024
1 parent c363749 commit 988dd17
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/sbt/src/main/kotlin/Sbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
9 changes: 7 additions & 2 deletions utils/ort/src/main/kotlin/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion utils/test/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"" }
Expand Down

0 comments on commit 988dd17

Please sign in to comment.