Skip to content

Commit

Permalink
feat(gradle-inspector): Allow to customize the Java home for analysis
Browse files Browse the repository at this point in the history
Note that `setJavaHome()` [1] sets the Java home "to use for the Gradle
process". However, as by default (unless toolchains are used [2]) "Gradle
will compile Java code to the language level of the JVM running Gradle"
[3], this will impact the Java target of the build.

See #8249 for context.

[1]: https://docs.gradle.org/current/javadoc/org/gradle/tooling/ConfigurableLauncher.html#setJavaHome(java.io.File)
[2]: https://docs.gradle.org/8.10/userguide/building_java_projects.html#sec:compiling_with_toolchain
[3]: https://docs.gradle.org/8.10/userguide/building_java_projects.html#sec:java_cross_compilation

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Sep 6, 2024
1 parent 7373195 commit 8ce9483
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 6 additions & 0 deletions model/src/main/resources/reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ ort:
# another package manager generates files that this package manager requires to run correctly.
mustRunAfter: [NPM]

GradleInspector:
options:
# An optional path to the Java home to use for Gradle project analysis. By default the Java version that ORT
# itself is run with will be used.
javaHome: "/path/to/java/home"

Yarn2:
options:
# If set to true, disable verification of HTTPS certificate of remote registries. Useful when using a proxy to
Expand Down
10 changes: 5 additions & 5 deletions model/src/test/kotlin/config/AnalyzerConfigurationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class AnalyzerConfigurationTest : WordSpec({
"override an existing entry" {
val original = AnalyzerConfiguration(
packageManagers = mapOf(
"Gradle" to PackageManagerConfiguration(
"GradleInspector" to PackageManagerConfiguration(
mustRunAfter = listOf("Npm"),
options = mapOf("gradleVersion" to "7.6.1")
options = mapOf("gradleVersion" to "7.6.1", "javaHome" to "/path/to/java/home")
),
"Npm" to PackageManagerConfiguration(
mustRunAfter = listOf("Yarn")
Expand All @@ -148,17 +148,17 @@ class AnalyzerConfigurationTest : WordSpec({

val patched = AnalyzerConfiguration(
packageManagers = mapOf(
"Gradle" to PackageManagerConfiguration(
"GradleInspector" to PackageManagerConfiguration(
mustRunAfter = listOf("Npm"),
options = mapOf("gradleVersion" to "8.0.2")
options = mapOf("gradleVersion" to "8.0.2", "javaHome" to "/path/to/java/home")
),
"Npm" to PackageManagerConfiguration(
mustRunAfter = listOf("Yarn")
)
)
)

original.withPackageManagerOption("Gradle", "gradleVersion", "8.0.2") shouldBe patched
original.withPackageManagerOption("GradleInspector", "gradleVersion", "8.0.2") shouldBe patched
}

"add a non-existing entry" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ private val GRADLE_USER_HOME = Os.env["GRADLE_USER_HOME"]?.let { File(it) } ?: O
*/
const val OPTION_GRADLE_VERSION = "gradleVersion"

/**
* The name of the option to specify the Java home to use.
*/
const val OPTION_JAVA_HOME = "javaHome"

/**
* The [Gradle](https://gradle.org/) package manager for Java.
*
Expand Down Expand Up @@ -160,6 +165,11 @@ class GradleInspector(
if (logger.delegate.isDebugEnabled && buildGradleVersion?.isEqualTo("8.5.0") != true) {
addProgressListener(ProgressListener { logger.debug(it.displayName) })
}

options[OPTION_JAVA_HOME]?.also {
logger.info { "Setting Java home for project analysis to '$it'." }
setJavaHome(File(it))
}
}
.setJvmArguments(jvmArgs)
.setStandardOutput(stdout)
Expand Down

0 comments on commit 8ce9483

Please sign in to comment.