Skip to content

Commit cc758f2

Browse files
fix(gradle-plugin): replace usages of deprecated methods
Add a test for multiple Gradle versions. Signed-off-by: Sergej Koščejev <[email protected]>
1 parent 305488d commit cc758f2

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.ossreviewtoolkit.plugins.packagemanagers.gradle
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import io.kotest.datatest.withData
5+
import io.kotest.matchers.should
6+
import org.ossreviewtoolkit.analyzer.resolveSingleProject
7+
import org.ossreviewtoolkit.model.toYaml
8+
import org.ossreviewtoolkit.utils.test.getAssetFile
9+
import org.ossreviewtoolkit.utils.test.matchExpectedResult
10+
11+
class GradleVersionsTest : FunSpec({
12+
withData(
13+
null, // 7.x, from the project's gradle/gradle-wrapper.properties
14+
"8.14.3",
15+
"9.0.0",
16+
"9.3.1"
17+
) { gradleVersion ->
18+
val definitionFile = getAssetFile("projects/synthetic/gradle-library/app/build.gradle")
19+
val expectedResultFile = getAssetFile("projects/synthetic/gradle-library-expected-output-app.yml")
20+
21+
val result = GradleFactory.create(javaVersion = "17", gradleVersion = gradleVersion)
22+
.resolveSingleProject(definitionFile, resolveScopes = true)
23+
24+
result.toYaml() should matchExpectedResult(expectedResultFile, definitionFile)
25+
26+
}
27+
})

plugins/package-managers/gradle/src/main/resources/init.gradle

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import groovy.transform.ToString
2121
import groovy.transform.TupleConstructor
22+
import org.gradle.internal.build.BuildState
2223

2324
import javax.inject.Inject
2425

@@ -218,7 +219,7 @@ class AbstractOrtDependencyTreePlugin<T> implements Plugin<T> {
218219

219220
try {
220221
resolvedArtifacts = configuration.resolvedConfiguration.lenientConfiguration
221-
.getArtifacts(Specs.SATISFIES_ALL)
222+
.getArtifacts()
222223
} catch (ResolveException e) {
223224
project.logger.info("Artifacts for configuration '${configuration.name}' could not be " +
224225
"resolved, therefore no information about artifact classifiers and extensions is " +
@@ -387,7 +388,7 @@ class AbstractOrtDependencyTreePlugin<T> implements Plugin<T> {
387388
return new OrtDependencyImpl(id.group, id.module, id.version, classifier, extension, [] as Set, dependencies,
388389
error, warning, pomFile, null)
389390
} else if (id instanceof ProjectComponentIdentifier) {
390-
if (id.build.isCurrentBuild() || !project.gradle.gradleVersion.isAtLeastVersion(7, 2)) {
391+
if (isCurrentBuild(id.build, project) || !project.gradle.gradleVersion.isAtLeastVersion(7, 2)) {
391392
def dependencyProject = project.rootProject.findProject(id.projectPath)
392393
return new OrtDependencyImpl(groupId: dependencyProject.group.toString(),
393394
artifactId: dependencyProject.name, version: dependencyProject.version.toString(),
@@ -416,6 +417,22 @@ class AbstractOrtDependencyTreePlugin<T> implements Plugin<T> {
416417
}
417418
}
418419

420+
private static boolean isCurrentBuild(BuildIdentifier buildIdentifier, Project project) {
421+
if (project.gradle.hasProperty('buildPath')) {
422+
// Gradle 9.1 and above
423+
return buildIdentifier.buildPath == project.gradle.buildPath
424+
}
425+
426+
if (buildIdentifier.hasProperty('currentBuild')) {
427+
// Gradle 8
428+
return buildIdentifier.currentBuild as boolean
429+
}
430+
431+
// Gradle 9.0, using internal API
432+
BuildIdentifier projectBuildIdentifier = project.services.get(BuildState).buildIdentifier
433+
return buildIdentifier.buildPath == projectBuildIdentifier.buildPath
434+
}
435+
419436
private static boolean canBeResolved(Configuration configuration) {
420437
// The canBeResolved property is available since Gradle 3.3.
421438
boolean canBeResolved = !configuration.hasProperty('canBeResolved') || configuration.canBeResolved
@@ -489,7 +506,7 @@ class AbstractOrtDependencyTreePlugin<T> implements Plugin<T> {
489506
if (id instanceof ModuleComponentIdentifier) {
490507
return toIdentifier(id.group, id.module, id.version)
491508
} else if (id instanceof ProjectComponentIdentifier) {
492-
if (id.build.currentBuild || !project.gradle.gradleVersion.isAtLeastVersion(7, 2)) {
509+
if (isCurrentBuild(id.build, project) || !project.gradle.gradleVersion.isAtLeastVersion(7, 2)) {
493510
def dependencyProject = project.rootProject.findProject(id.projectPath)
494511
return toIdentifier(dependencyProject.group.toString(), dependencyProject.name,
495512
dependencyProject.version.toString())

0 commit comments

Comments
 (0)