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 8a1b9a8e37b34..33d80ca9fe382 100644 --- a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt +++ b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt @@ -32,6 +32,7 @@ import org.apache.logging.log4j.kotlin.logger import org.gradle.tooling.GradleConnector import org.gradle.tooling.events.ProgressListener import org.gradle.tooling.internal.consumer.DefaultGradleConnector +import org.gradle.tooling.model.build.BuildEnvironment import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory import org.ossreviewtoolkit.analyzer.PackageManager @@ -68,6 +69,8 @@ import org.ossreviewtoolkit.utils.ort.okHttpClient import org.ossreviewtoolkit.utils.ort.ortToolsDirectory import org.ossreviewtoolkit.utils.spdx.SpdxOperator +import org.semver4j.Semver + /** * The names of Gradle (Groovy, Kotlin script) build files for a Gradle project. */ @@ -156,15 +159,25 @@ class GradleInspector( .splitOnWhitespace() .map { it.unquote() } + val environment = connection.model(BuildEnvironment::class.java).get() + val buildGradleVersion = Semver.coerce(environment.gradle.gradleVersion) + + logger.info { "The project at '$projectDir' uses Gradle version $buildGradleVersion." } + // In order to debug the plugin, pass the "-Dorg.gradle.debug=true" option to the JVM running ORT. This will // then block execution of the plugin until a remote debug session is attached to port 5005 (by default), // also see https://docs.gradle.org/current/userguide/troubleshooting.html#sec:troubleshooting_build_logic. val model = connection.model(OrtDependencyTreeModel::class.java) - .addProgressListener(ProgressListener { logger.debug { it.displayName } }) + .apply { + // Work around https://github.com/gradle/gradle/issues/28464. + if (logger.delegate.isDebugEnabled && buildGradleVersion?.isEqualTo("8.5.0") != true) { + addProgressListener(ProgressListener { logger.debug(it.displayName) }) + } + } .setJvmArguments(jvmArgs) .setStandardOutput(stdout) .setStandardError(stderr) - .withArguments("--init-script", initScriptFile.path) + .withArguments("-Duser.home=${Os.userHomeDirectory}", "--init-script", initScriptFile.path) .get() if (stdout.size() > 0) { diff --git a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt index 12db8fefe9dea..26b3e378f4b81 100644 --- a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt +++ b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt @@ -36,6 +36,7 @@ import org.eclipse.aether.repository.WorkspaceRepository import org.gradle.tooling.GradleConnector import org.gradle.tooling.events.ProgressListener import org.gradle.tooling.internal.consumer.DefaultGradleConnector +import org.gradle.tooling.model.build.BuildEnvironment import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory import org.ossreviewtoolkit.analyzer.PackageManager @@ -62,6 +63,8 @@ import org.ossreviewtoolkit.utils.common.temporaryProperties import org.ossreviewtoolkit.utils.common.unquote import org.ossreviewtoolkit.utils.ort.createOrtTempFile +import org.semver4j.Semver + private val GRADLE_USER_HOME = Os.env["GRADLE_USER_HOME"]?.let { File(it) } ?: Os.userHomeDirectory.resolve(".gradle") private val GRADLE_BUILD_FILES = listOf("build.gradle", "build.gradle.kts") @@ -212,10 +215,19 @@ class Gradle( val stdout = ByteArrayOutputStream() val stderr = ByteArrayOutputStream() - val dependencyTreeModel = connection - .model(OrtDependencyTreeModel::class.java) + val environment = connection.model(BuildEnvironment::class.java).get() + val buildGradleVersion = Semver.coerce(environment.gradle.gradleVersion) + + logger.info { "The project at '$projectDir' uses Gradle version $buildGradleVersion." } + + val dependencyTreeModel = connection.model(OrtDependencyTreeModel::class.java) + .apply { + // Work around https://github.com/gradle/gradle/issues/28464. + if (logger.delegate.isDebugEnabled && buildGradleVersion?.isEqualTo("8.5.0") != true) { + addProgressListener(ProgressListener { logger.debug(it.displayName) }) + } + } .addJvmArguments(jvmArgs) - .addProgressListener(ProgressListener { logger.debug { it.displayName } }) .setStandardOutput(stdout) .setStandardError(stderr) .withArguments("-Duser.home=${Os.userHomeDirectory}", "--init-script", initScriptFile.path)