From 62886aa9c353dfaa09b2f64bc5c6eb46c261319c Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 13 Mar 2024 14:20:15 +0100 Subject: [PATCH 1/5] chore(Gradle): Remove unneeded default imports See [1]. [1]: https://docs.gradle.org/current/userguide/writing_build_scripts.html#script-default-imports Signed-off-by: Sebastian Schuberth --- buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts index 59f51b87ce882..7b88c261c913d 100644 --- a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts @@ -21,11 +21,6 @@ import io.gitlab.arturbosch.detekt.Detekt import io.gitlab.arturbosch.detekt.report.ReportMergeTask import org.gradle.accessors.dm.LibrariesForLibs -import org.gradle.api.JavaVersion -import org.gradle.api.attributes.TestSuiteType -import org.gradle.api.plugins.jvm.JvmTestSuite -import org.gradle.api.tasks.compile.JavaCompile -import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent import org.gradle.kotlin.dsl.dependencies From 1979936136b2e23ec0564f9af3937c8a74b39947 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 13 Mar 2024 15:38:30 +0100 Subject: [PATCH 2/5] chore(Gradle): Remove an unneeded work-around for KT-48745 The bogus warning from [1] cannot be reproduced anymore when running ./gradlew --rerun-tasks --no-build-cache --no-configuration-cache \ :buildSrc:build [1]: https://youtrack.jetbrains.com/issue/KT-48745 Signed-off-by: Sebastian Schuberth --- buildSrc/build.gradle.kts | 12 ------------ .../main/kotlin/ort-kotlin-conventions.gradle.kts | 6 ------ 2 files changed, 18 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 9c8d90328d744..e5f5705fb1a32 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -19,8 +19,6 @@ import org.gradle.accessors.dm.LibrariesForLibs -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - private val Project.libs: LibrariesForLibs get() = extensions.getByType() @@ -44,13 +42,3 @@ dependencies { implementation(libs.plugin.kotlin) implementation(libs.plugin.mavenPublish) } - -val javaVersion = JavaVersion.current() -val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaVersion.majorVersion) } - .getOrDefault(enumValues().max()) - -tasks.withType().configureEach { - // Align this with Kotlin to avoid errors, see https://youtrack.jetbrains.com/issue/KT-48745. - sourceCompatibility = maxKotlinJvmTarget.target - targetCompatibility = maxKotlinJvmTarget.target -} diff --git a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts index 7b88c261c913d..074c553423e3a 100644 --- a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts @@ -147,12 +147,6 @@ tasks.withType().configureEach detekt@{ finalizedBy(mergeDetektReports) } -tasks.withType().configureEach { - // Align this with Kotlin to avoid errors, see https://youtrack.jetbrains.com/issue/KT-48745. - sourceCompatibility = maxKotlinJvmTarget.target - targetCompatibility = maxKotlinJvmTarget.target -} - tasks.withType().configureEach { val hasSerialization = plugins.hasPlugin(libs.plugins.kotlinSerialization.get().pluginId) From 0c020d26d18f1695dbdb6622014a53ed6c7ae85d Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 13 Mar 2024 22:16:29 +0100 Subject: [PATCH 3/5] build(Gradle): Allow to configure the build JDK via toolchains The previous code was calling `JavaVersion.current()` at build time, assuming that the Java version running Gradle is the same Java version used for building ORT. This assumption makes it more difficult to build ORT for different Java versions in the same environment. Solve that by using Gradle's toolchains mechanism [1] instead. Choose Java 11 as the default for the new `javaLanguageVersion` property to support analyzing older projects with ORT, also see [2]. [1]: https://docs.gradle.org/current/userguide/toolchains.html [2]: https://github.com/oss-review-toolkit/ort/issues/8249 Signed-off-by: Sebastian Schuberth --- .../src/main/kotlin/ort-kotlin-conventions.gradle.kts | 11 +++++++++-- gradle.properties | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts index 074c553423e3a..4c38cd487029f 100644 --- a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts @@ -35,6 +35,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile private val Project.libs: LibrariesForLibs get() = extensions.getByType() +val javaLanguageVersion: String by project + plugins { // Apply core plugins. jacoco @@ -105,8 +107,13 @@ detekt { basePath = rootDir.path } -val javaVersion = JavaVersion.current() -val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaVersion.majorVersion) } +java { + toolchain { + languageVersion = JavaLanguageVersion.of(javaLanguageVersion) + } +} + +val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaLanguageVersion) } .getOrDefault(enumValues().max()) val mergeDetektReportsTaskName = "mergeDetektReports" diff --git a/gradle.properties b/gradle.properties index 0dae07ff0e255..a7989e0751fb9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,6 +23,9 @@ org.gradle.parallel = true kotlin.code.style = official +# The version of the JDK to use for building ORT. +javaLanguageVersion = 11 + # The version of the SPDX license list which is used to import license texts and generate SPDX enums. Must be a valid # tag, see https://github.com/spdx/license-list-data/tags. spdxLicenseListVersion = 3.23 From 5ad03e93dd67536a90308b22b88d5a23e2814f4a Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 14 Mar 2024 08:13:19 +0100 Subject: [PATCH 4/5] refactor(test-utils): Use ORT's `Environment` to patch existing results Ensure to get values in exactly the same way as main ORT itself does. Signed-off-by: Sebastian Schuberth --- utils/test/src/main/kotlin/Utils.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/test/src/main/kotlin/Utils.kt b/utils/test/src/main/kotlin/Utils.kt index d80c3b29d595e..bd5d027d81a16 100644 --- a/utils/test/src/main/kotlin/Utils.kt +++ b/utils/test/src/main/kotlin/Utils.kt @@ -89,11 +89,13 @@ fun patchExpectedResult( definitionFile: File? = null, custom: Map = emptyMap() ): String { + val env = Environment() + val replacements = buildMap { - put("", System.getProperty("java.version")) - put("", System.getProperty("os.name")) - put("\"\"", Runtime.getRuntime().availableProcessors().toString()) - put("\"\"", Runtime.getRuntime().maxMemory().toString()) + put("", env.javaVersion) + put("", env.os) + put("\"\"", env.processors.toString()) + put("\"\"", env.maxMemory.toString()) if (definitionFile != null) { val projectDir = definitionFile.parentFile From 93a7f37e7b61fdd6a77664c23feac01dd9241039 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 13 Mar 2024 22:22:02 +0100 Subject: [PATCH 5/5] feat(cli): Print the JDK version ORT was built with Signed-off-by: Sebastian Schuberth --- .../kotlin/ort-kotlin-conventions.gradle.kts | 6 ++++++ .../assets/git-repo-expected-output.yml | 1 + ...ndencies-expected-result-with-curations.yml | 1 + cli/src/main/kotlin/OrtMain.kt | 6 +++--- .../assets/result-with-issues-graph-old.yml | 1 + .../test/assets/result-with-issues-graph.yml | 1 + .../test/assets/result-with-issues-scopes.yml | 1 + ...t-multi-project-example-expected-output.yml | 1 + .../sbt-multi-project-example-graph-old.yml | 1 + .../assets/sbt-multi-project-example-graph.yml | 1 + .../pnpm-workspaces-expected-output.yml | 1 + ...t-multi-project-example-expected-output.yml | 1 + .../sbt-http4s-template-expected-output.yml | 1 + .../scan-result-for-synthetic-gradle-lib.yml | 2 ++ ...ntegration-all-pkgs-expected-ort-result.yml | 2 ++ ...gration-subset-pkgs-expected-ort-result.yml | 2 ++ utils/ort/src/main/kotlin/Environment.kt | 18 ++++++++++++++++++ utils/test/src/main/kotlin/Utils.kt | 1 + 18 files changed, 45 insertions(+), 3 deletions(-) diff --git a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts index 4c38cd487029f..8cea28a21f0f1 100644 --- a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts @@ -113,6 +113,12 @@ java { } } +tasks.withType().configureEach { + manifest { + attributes["Build-Jdk"] = javaToolchains.compilerFor(java.toolchain).map { it.metadata.jvmVersion } + } +} + val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaLanguageVersion) } .getOrDefault(enumValues().max()) diff --git a/cli/src/funTest/assets/git-repo-expected-output.yml b/cli/src/funTest/assets/git-repo-expected-output.yml index ef77cbebb5026..3b6f280282408 100644 --- a/cli/src/funTest/assets/git-repo-expected-output.yml +++ b/cli/src/funTest/assets/git-repo-expected-output.yml @@ -47,6 +47,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/cli/src/funTest/assets/gradle-all-dependencies-expected-result-with-curations.yml b/cli/src/funTest/assets/gradle-all-dependencies-expected-result-with-curations.yml index af9d29dd2a511..5ead4c4f0d4e3 100644 --- a/cli/src/funTest/assets/gradle-all-dependencies-expected-result-with-curations.yml +++ b/cli/src/funTest/assets/gradle-all-dependencies-expected-result-with-curations.yml @@ -40,6 +40,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/cli/src/main/kotlin/OrtMain.kt b/cli/src/main/kotlin/OrtMain.kt index 8afa8bd33a55a..84d0ea0292913 100644 --- a/cli/src/main/kotlin/OrtMain.kt +++ b/cli/src/main/kotlin/OrtMain.kt @@ -190,9 +190,9 @@ class OrtMain : CliktCommand( cell( """ - The OSS Review Toolkit, version ${Theme.Default.info(version)}. - - Running$command$user under Java ${env.javaVersion} on ${env.os} + The OSS Review Toolkit, version ${Theme.Default.info(version)}, + built with JDK ${env.buildJdk}, running under Java ${env.javaVersion}. + Executing$command$user on ${env.os} with ${env.processors} CPUs and a maximum of $maxMemInMib MiB of memory. """.trimIndent() ) diff --git a/model/src/test/assets/result-with-issues-graph-old.yml b/model/src/test/assets/result-with-issues-graph-old.yml index 6460cfe9bff0c..813f1a184b827 100644 --- a/model/src/test/assets/result-with-issues-graph-old.yml +++ b/model/src/test/assets/result-with-issues-graph-old.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: 4 diff --git a/model/src/test/assets/result-with-issues-graph.yml b/model/src/test/assets/result-with-issues-graph.yml index cf2a9da9839e4..30b98b128a789 100644 --- a/model/src/test/assets/result-with-issues-graph.yml +++ b/model/src/test/assets/result-with-issues-graph.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: 4 diff --git a/model/src/test/assets/result-with-issues-scopes.yml b/model/src/test/assets/result-with-issues-scopes.yml index 5357088ec6ecc..e48882fd41e74 100644 --- a/model/src/test/assets/result-with-issues-scopes.yml +++ b/model/src/test/assets/result-with-issues-scopes.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: 4 diff --git a/model/src/test/assets/sbt-multi-project-example-expected-output.yml b/model/src/test/assets/sbt-multi-project-example-expected-output.yml index ae1862c8d8a67..61689dfc575bd 100644 --- a/model/src/test/assets/sbt-multi-project-example-expected-output.yml +++ b/model/src/test/assets/sbt-multi-project-example-expected-output.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/model/src/test/assets/sbt-multi-project-example-graph-old.yml b/model/src/test/assets/sbt-multi-project-example-graph-old.yml index 69f775974c037..75faa6da133a5 100644 --- a/model/src/test/assets/sbt-multi-project-example-graph-old.yml +++ b/model/src/test/assets/sbt-multi-project-example-graph-old.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/model/src/test/assets/sbt-multi-project-example-graph.yml b/model/src/test/assets/sbt-multi-project-example-graph.yml index 0d75737b403dc..4349d7f016361 100644 --- a/model/src/test/assets/sbt-multi-project-example-graph.yml +++ b/model/src/test/assets/sbt-multi-project-example-graph.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/plugins/package-managers/node/src/funTest/assets/projects/synthetic/pnpm-workspaces-expected-output.yml b/plugins/package-managers/node/src/funTest/assets/projects/synthetic/pnpm-workspaces-expected-output.yml index a437ece9332bd..59ffd4138f563 100644 --- a/plugins/package-managers/node/src/funTest/assets/projects/synthetic/pnpm-workspaces-expected-output.yml +++ b/plugins/package-managers/node/src/funTest/assets/projects/synthetic/pnpm-workspaces-expected-output.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/plugins/package-managers/sbt/src/funTest/assets/projects/external/sbt-multi-project-example-expected-output.yml b/plugins/package-managers/sbt/src/funTest/assets/projects/external/sbt-multi-project-example-expected-output.yml index ae1862c8d8a67..61689dfc575bd 100644 --- a/plugins/package-managers/sbt/src/funTest/assets/projects/external/sbt-multi-project-example-expected-output.yml +++ b/plugins/package-managers/sbt/src/funTest/assets/projects/external/sbt-multi-project-example-expected-output.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/plugins/package-managers/sbt/src/funTest/assets/projects/synthetic/sbt-http4s-template-expected-output.yml b/plugins/package-managers/sbt/src/funTest/assets/projects/synthetic/sbt-http4s-template-expected-output.yml index 96addfafd0e71..e024c4d9dbada 100644 --- a/plugins/package-managers/sbt/src/funTest/assets/projects/synthetic/sbt-http4s-template-expected-output.yml +++ b/plugins/package-managers/sbt/src/funTest/assets/projects/synthetic/sbt-http4s-template-expected-output.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/plugins/reporters/web-app/src/funTest/assets/scan-result-for-synthetic-gradle-lib.yml b/plugins/reporters/web-app/src/funTest/assets/scan-result-for-synthetic-gradle-lib.yml index 7b6d47ae8f313..282034b52a401 100644 --- a/plugins/reporters/web-app/src/funTest/assets/scan-result-for-synthetic-gradle-lib.yml +++ b/plugins/reporters/web-app/src/funTest/assets/scan-result-for-synthetic-gradle-lib.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" @@ -188,6 +189,7 @@ scanner: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/scanner/src/funTest/assets/scanner-integration-all-pkgs-expected-ort-result.yml b/scanner/src/funTest/assets/scanner-integration-all-pkgs-expected-ort-result.yml index b677cab0abe06..5aab6ad8bdfc5 100644 --- a/scanner/src/funTest/assets/scanner-integration-all-pkgs-expected-ort-result.yml +++ b/scanner/src/funTest/assets/scanner-integration-all-pkgs-expected-ort-result.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" @@ -186,6 +187,7 @@ scanner: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/scanner/src/funTest/assets/scanner-integration-subset-pkgs-expected-ort-result.yml b/scanner/src/funTest/assets/scanner-integration-subset-pkgs-expected-ort-result.yml index fd739c4ab3f28..4ff01026e6357 100644 --- a/scanner/src/funTest/assets/scanner-integration-subset-pkgs-expected-ort-result.yml +++ b/scanner/src/funTest/assets/scanner-integration-subset-pkgs-expected-ort-result.yml @@ -16,6 +16,7 @@ analyzer: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" @@ -105,6 +106,7 @@ scanner: end_time: "1970-01-01T00:00:00Z" environment: ort_version: "HEAD" + build_jdk: "" java_version: "" os: "" processors: "" diff --git a/utils/ort/src/main/kotlin/Environment.kt b/utils/ort/src/main/kotlin/Environment.kt index da8324526b996..a6f51acd409c0 100644 --- a/utils/ort/src/main/kotlin/Environment.kt +++ b/utils/ort/src/main/kotlin/Environment.kt @@ -21,6 +21,7 @@ package org.ossreviewtoolkit.utils.ort import java.io.File import java.lang.Runtime +import java.util.jar.JarFile import org.ossreviewtoolkit.utils.common.Os @@ -33,6 +34,11 @@ data class Environment( */ val ortVersion: String = ORT_VERSION, + /** + * The JDK version ORT was built with. + */ + val buildJdk: String = BUILD_JDK, + /** * The version of Java used. */ @@ -71,6 +77,18 @@ data class Environment( */ val ORT_VERSION by lazy { this::class.java.`package`.implementationVersion ?: "IDE-SNAPSHOT" } + /** + * The version of the OSS Review Toolkit as a string. + */ + val BUILD_JDK: String by lazy { + runCatching { + val codeSource = this::class.java.protectionDomain.codeSource + JarFile(codeSource?.location?.file).use { + it.manifest.mainAttributes.getValue("Build-Jdk") + } + }.getOrDefault(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/test/src/main/kotlin/Utils.kt b/utils/test/src/main/kotlin/Utils.kt index bd5d027d81a16..7c2cfa7ef40d6 100644 --- a/utils/test/src/main/kotlin/Utils.kt +++ b/utils/test/src/main/kotlin/Utils.kt @@ -92,6 +92,7 @@ fun patchExpectedResult( val env = Environment() val replacements = buildMap { + put("", env.buildJdk) put("", env.javaVersion) put("", env.os) put("\"\"", env.processors.toString())