Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address a possible Gradle progress listener bug #8425

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
Loading