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

Filter out duplicated dependencies from pom.xml #368

Merged
merged 6 commits into from
May 3, 2022
Merged
Changes from 3 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 @@ -38,7 +38,8 @@ import org.gradle.kotlin.dsl.withGroovyBuilder
/**
* Writes the dependencies of a Gradle project in a `pom.xml` format.
*
* Includes the dependencies of the subprojects. Does not include the transitive dependencies.
* Includes the dependencies of the subprojects. Does not include
* the transitive dependencies.
*
* ```
* <dependencies>
Expand All @@ -51,6 +52,9 @@ import org.gradle.kotlin.dsl.withGroovyBuilder
* </dependencies>
* ```
*
* When there are several versions of the same dependency, only the one with
* the newest version is retained.
*
* @see PomGenerator
*/
internal class DependencyWriter
Expand Down Expand Up @@ -103,7 +107,8 @@ fun Project.dependencies(): SortedSet<ScopedDependency> {
val subprojectDeps = subproject.depsFromAllConfigurations()
dependencies.addAll(subprojectDeps)
}
return dependencies.toSortedSet()

return dependencies.deduplicate()
}

/**
Expand Down Expand Up @@ -132,3 +137,19 @@ private fun Project.depsFromAllConfigurations(): Set<ScopedDependency> {
private fun Dependency.isExternal(): Boolean {
return this.javaClass.kotlin.isSubclassOf(AbstractExternalModuleDependency::class)
}

/**
* Filters out duplicated dependencies by group and name.
*
* When there are several versions of the same dependency, the method will retain only
* the one with the newest version.
*
* Sometimes, a project uses several versions of the same dependency. This may happen
* when different modules of the project use different versions of the same dependency.
* But for our `pom.xml`, which has clearly representative character, a single version
* of a dependency is quite enough.
*/
private fun MutableSet<ScopedDependency>.deduplicate() =
groupBy { it.dependency().run { "$group:$name" } }
.map { it.value.maxOrNull()!! }
.toSortedSet()