Skip to content

Commit 6e94cb3

Browse files
oheger-boschsschuberth
authored andcommitted
feat(Maven): Filter source bundles from dependencies
In Tycho's dependency tree, source code bundles appear together with regular dependencies and the suffix ".source". Filter out these artifacts, as they are not really dependencies in the context of ORT. In an upcoming commit, such source artifacts will be assigned to the packages generated for dependencies. Signed-off-by: Oliver Heger <[email protected]>
1 parent 826652a commit 6e94cb3

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

plugins/package-managers/maven/src/main/kotlin/utils/DependencyTreeParser.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import org.eclipse.aether.repository.RemoteRepository
3636
/** The [Json] instance to use for parsing of dependency trees in JSON format. */
3737
internal val JSON = Json { ignoreUnknownKeys = true }
3838

39+
/** The suffix that classifies an OSGi bundle as a source bundle. */
40+
private const val SOURCE_BUNDLE_SUFFIX = ".source"
41+
3942
/**
4043
* A data class to represent a node in the JSON output generated by the Maven Dependency Plugin.
4144
*/
@@ -79,10 +82,30 @@ internal fun parseDependencyTree(
7982
private fun DependencyTreeMojoNode.toDependencyNode(repositories: List<RemoteRepository>): DependencyNode {
8083
val artifact = DefaultArtifact(groupId, artifactId, classifier, type, version)
8184
val dependency = Dependency(artifact, scope)
82-
val childNodes = children.map { it.toDependencyNode(repositories) }
85+
val childNodes = children.filterSourceBundles().map { it.toDependencyNode(repositories) }
8386

8487
return DefaultDependencyNode(dependency).apply {
8588
children = childNodes
8689
setRepositories(repositories)
8790
}
8891
}
92+
93+
/**
94+
* Filter out source bundles from this collection of [DependencyTreeMojoNode]s. Tycho reports source code bundles
95+
* (denoted by the suffix ".source") as regular dependencies. As this does not make sense for ORT, remove them, but
96+
* only if the corresponding binary bundle is present as well. This is to reduce the likelihood of dropping a
97+
* relevant dependency by accident.
98+
*/
99+
private fun Collection<DependencyTreeMojoNode>.filterSourceBundles(): Collection<DependencyTreeMojoNode> {
100+
if (none { isSourceBundle(it) }) return this
101+
102+
val bundleIds = mapTo(mutableSetOf()) { it.artifactId }
103+
return filterNot { node ->
104+
isSourceBundle(node) && node.artifactId.removeSuffix(SOURCE_BUNDLE_SUFFIX) in bundleIds
105+
}
106+
}
107+
108+
/**
109+
* Return a flag whether the given [node] represents a source bundle.
110+
*/
111+
private fun isSourceBundle(node: DependencyTreeMojoNode): Boolean = node.artifactId.endsWith(SOURCE_BUNDLE_SUFFIX)

plugins/package-managers/maven/src/test/kotlin/utils/DependencyTreeParserTest.kt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,96 @@ class DependencyTreeParserTest : WordSpec({
339339
compareNodes(projectNode1, it)
340340
}
341341
}
342+
343+
"remove source code bundles in dependencies" {
344+
val projectNode = DependencyTreeMojoNode(
345+
"org.ossreviewtoolkit",
346+
"module",
347+
"1.2.3-SNAPSHOT",
348+
"pom",
349+
"",
350+
"",
351+
listOf(
352+
DependencyTreeMojoNode(
353+
"org.apache.commons",
354+
"commons-configuration2",
355+
"2.11.0",
356+
"jar",
357+
"compile",
358+
""
359+
),
360+
DependencyTreeMojoNode(
361+
"p2.eclipse.plugin",
362+
"org.objectweb.asm",
363+
"9.1.0",
364+
"jar",
365+
"compile",
366+
""
367+
),
368+
DependencyTreeMojoNode(
369+
"p2.eclipse.plugin",
370+
"org.objectweb.asm.source",
371+
"9.1.0",
372+
"jar",
373+
"compile",
374+
""
375+
)
376+
)
377+
)
378+
379+
val projectDependencies = parseDependencyTree(
380+
inputStreamFor(projectNode),
381+
listOf(createProject("module"))
382+
).toList()
383+
384+
projectDependencies.shouldBeSingleton { node ->
385+
node.children.map { it.artifact.artifactId } shouldContainExactlyInAnyOrder listOf(
386+
"commons-configuration2",
387+
"org.objectweb.asm"
388+
)
389+
}
390+
}
391+
392+
"only remove source code bundles if there is a matching regular bundle" {
393+
val projectNode = DependencyTreeMojoNode(
394+
"org.ossreviewtoolkit",
395+
"module",
396+
"1.2.3-SNAPSHOT",
397+
"pom",
398+
"",
399+
"",
400+
listOf(
401+
DependencyTreeMojoNode(
402+
"org.apache.commons",
403+
"commons-configuration2",
404+
"2.11.0",
405+
"jar",
406+
"compile",
407+
""
408+
),
409+
DependencyTreeMojoNode(
410+
"p2.eclipse.plugin",
411+
"org.objectweb.asm.source",
412+
"9.1.0",
413+
"jar",
414+
"compile",
415+
""
416+
)
417+
)
418+
)
419+
420+
val projectDependencies = parseDependencyTree(
421+
inputStreamFor(projectNode),
422+
listOf(createProject("module"))
423+
).toList()
424+
425+
projectDependencies.shouldBeSingleton { node ->
426+
node.children.map { it.artifact.artifactId } shouldContainExactlyInAnyOrder listOf(
427+
"commons-configuration2",
428+
"org.objectweb.asm.source"
429+
)
430+
}
431+
}
342432
}
343433
})
344434

0 commit comments

Comments
 (0)