Skip to content

Commit a858e7e

Browse files
committed
Exclude plugin marker dependencies from graph
Plugin marker dependencies are an artifact of the way plugins are resolved in Gradle, and duplicate the actual plugin dependency. These add no value to the graph and should be excluded by default. Fixes #111
1 parent 20a9fef commit a858e7e

File tree

4 files changed

+34
-52
lines changed

4 files changed

+34
-52
lines changed

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/PluginDependencyExtractorTest.groovy

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -124,61 +124,29 @@ class PluginDependencyExtractorTest extends BaseExtractorTest {
124124
// Settings plugin
125125
Map<String, Map> pluginDependencies = settingsPluginsAreSupported()
126126
? [
127-
"my.settings.plugin:my.settings.plugin.gradle.plugin:1.0": [
128-
relationship: "direct",
129-
dependencies: ["com.example:settingPlugin:1.0"]
130-
],
131127
"com.example:settingPlugin:1.0" : [
132-
relationship: "indirect",
128+
relationship: "direct",
133129
dependencies: ["org.test:foo:1.0"]
134130
],
135131
"org.test:foo:1.0" : [
136132
relationship: "indirect",
137133
dependencies: []
138-
],
139-
140-
// The project plugins are resolved at build level without any transitive deps
141-
"my.project.plugin1:my.project.plugin1.gradle.plugin:1.0": [
142-
relationship: "direct",
143-
dependencies: []
144-
],
145-
"my.project.plugin2:my.project.plugin2.gradle.plugin:1.0": [
146-
relationship: "direct",
147-
dependencies: []
148-
]
149-
]
150-
: [
151-
// The project plugins are resolved at build level without any transitive deps
152-
"my.project.plugin1:my.project.plugin1.gradle.plugin:1.0": [
153-
relationship: "direct",
154-
dependencies: []
155-
],
156-
"my.project.plugin2:my.project.plugin2.gradle.plugin:1.0": [
157-
relationship: "direct",
158-
dependencies: []
159134
]
160135
]
136+
: [:]
161137

162138
// Plugin 1
163139
pluginDependencies.putAll([
164-
"my.project.plugin1:my.project.plugin1.gradle.plugin:1.0": [
165-
relationship: "direct",
166-
dependencies: ["com.example:plugin1:1.0"]
167-
],
168140
"com.example:plugin1:1.0" : [
169-
relationship: "indirect",
141+
relationship: "direct",
170142
dependencies: []
171143
]
172144
])
173145

174146
// Plugin 2
175147
pluginDependencies.putAll([
176-
"my.project.plugin2:my.project.plugin2.gradle.plugin:1.0": [
177-
relationship: "direct",
178-
dependencies: ["com.example:plugin2:1.0"]
179-
],
180148
"com.example:plugin2:1.0" : [
181-
relationship: "indirect",
149+
relationship: "direct",
182150
dependencies: ["org.test:bar:1.0"]
183151
],
184152
"org.test:bar:1.0" : [

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/SampleProjectDependencyExtractorTest.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class SampleProjectDependencyExtractorTest extends BaseExtractorTest {
2626
def manifestDependencies = manifest.resolved
2727

2828
[ // plugin dependencies
29-
"com.gradle.enterprise:com.gradle.enterprise.gradle.plugin:3.12.6",
3029
"com.gradle:gradle-enterprise-gradle-plugin:3.12.6",
3130
"com.diffplug.spotless:spotless-plugin-gradle:4.5.1",
3231
"com.diffplug.durian:durian-core:1.2.0",

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/SingleProjectDependencyExtractorTest.groovy

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,8 @@ class SingleProjectDependencyExtractorTest extends BaseExtractorTest {
365365
def manifest = gitHubManifest()
366366
manifest.sourceFile == "settings.gradle"
367367
manifest.assertResolved([
368-
"my.project.plugin:my.project.plugin.gradle.plugin:1.0": [
369-
package_url : purlFor("my.project.plugin", "my.project.plugin.gradle.plugin", "1.0"),
370-
dependencies: ["com.example:plugin:1.0"]
371-
],
372-
"com.example:plugin:1.0" : [
373-
package_url : purlFor("com.example", "plugin", "1.0"),
374-
relationship: "indirect"
368+
"com.example:plugin:1.0": [
369+
package_url : purlFor("com.example", "plugin", "1.0")
375370
]
376371
])
377372
}
@@ -401,13 +396,8 @@ class SingleProjectDependencyExtractorTest extends BaseExtractorTest {
401396
def manifest = gitHubManifest()
402397
manifest.sourceFile == "settings.gradle"
403398
manifest.assertResolved([
404-
"my.settings.plugin:my.settings.plugin.gradle.plugin:1.0": [
405-
package_url : purlFor("my.settings.plugin", "my.settings.plugin.gradle.plugin", "1.0"),
406-
dependencies: ["com.example:plugin:1.0"]
407-
],
408-
"com.example:plugin:1.0" : [
409-
package_url : purlFor("com.example", "plugin", "1.0"),
410-
relationship: "indirect"
399+
"com.example:plugin:1.0": [
400+
package_url : purlFor("com.example", "plugin", "1.0")
411401
]
412402
])
413403
}

plugin/src/main/kotlin/org/gradle/dependencygraph/extractor/DependencyExtractor.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.gradle.dependencygraph.extractor
22

3+
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
34
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
45
import org.gradle.api.artifacts.result.ResolvedComponentResult
56
import org.gradle.api.artifacts.result.ResolvedDependencyResult
@@ -230,7 +231,31 @@ abstract class DependencyExtractor :
230231
}
231232

232233
private fun getResolvedDependencies(component: ResolvedComponentResult): List<ResolvedComponentResult> {
233-
return component.dependencies.filterIsInstance<ResolvedDependencyResult>().map { it.selected }.filter { it != component }
234+
return component.dependencies
235+
.filterIsInstance<ResolvedDependencyResult>()
236+
.map { it.selected }
237+
.mapNotNull { traversePluginMarker(it) }
238+
.filter { it != component }
239+
}
240+
241+
/**
242+
* If the rawComponent represents a plugin marker artifact, then use the target plugin dependency instead.
243+
* For a plugin marker with no dependencies return null, so it can be filtered from the list.
244+
*/
245+
private fun traversePluginMarker(rawComponent: ResolvedComponentResult): ResolvedComponentResult? {
246+
if (rawComponent.id is ModuleComponentIdentifier) {
247+
val componentId = rawComponent.id as ModuleComponentIdentifier
248+
if (componentId.module == componentId.group + ".gradle.plugin") {
249+
if (rawComponent.dependencies.isEmpty()) {
250+
return null
251+
}
252+
if (rawComponent.dependencies.size == 1) {
253+
val pluginDep = rawComponent.dependencies.iterator().next() as? ResolvedDependencyResult
254+
return pluginDep?.selected
255+
}
256+
}
257+
}
258+
return rawComponent
234259
}
235260

236261
private fun createComponentNode(componentId: String, origin: DependencyOrigin, isDirectDependency: Boolean, component: ResolvedComponentResult, repositoryLookup: RepositoryUrlLookup): ResolvedDependency {

0 commit comments

Comments
 (0)