Forked from maxs-rose/ExcludeTransitiveDependenciesFilterMultiProject.groovy
Last active
December 26, 2022 17:32
-
-
Save Griefed/5c978356ce30c102cc8a3762e78ba98b to your computer and use it in GitHub Desktop.
Properly generate only top level dependencies for multi project gradle builds when using https://github.com/jk1/Gradle-License-Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.jk1.license.ModuleData | |
import com.github.jk1.license.ProjectData | |
import com.github.jk1.license.filter.DependencyFilter | |
import org.gradle.api.artifacts.ConfigurationContainer | |
import org.gradle.api.artifacts.ResolvedDependency | |
import java.util.stream.Collectors | |
import java.util.stream.Stream | |
/** | |
* Special thanks to <a href="https://github.com/maxs-rose">maxs-rose</a> for this filter, found via a comment of them | |
* in an issue of the jk1 Gradle-License-Report plugin repo on GitHub at <a href="https://github.com/jk1/Gradle-License-Report/issues/187#issuecomment-1361238009">#187</a> | |
* | |
* Source: <a href="https://gist.github.com/maxs-rose/3889d10d3535cc07bceb631ff90504f2">maxs-rose/ExcludeTransitiveDependenciesFilterMultiProject.groovy</a> | |
*/ | |
class ExcludeTransitiveDependenciesFilterMultiProject : DependencyFilter { | |
companion object { | |
fun getFirstLevelDependenciesForConfiguration( | |
configContainer: ConfigurationContainer, configName: String | |
): Set<ResolvedDependency>? { | |
if (!configContainer.names.contains(configName)) { | |
return null | |
} | |
return configContainer.getByName(configName).resolvedConfiguration.firstLevelModuleDependencies | |
} | |
} | |
override fun filter(source: ProjectData): ProjectData { | |
val firstLevelDependencies: Set<ResolvedDependency> = source.configurations.stream().flatMap { c -> | |
val parentDeps: Stream<ResolvedDependency> = | |
getFirstLevelDependenciesForConfiguration(source.project.configurations, c.name)!!.stream() | |
val childDeps: Stream<ResolvedDependency> = source.project.subprojects.stream() | |
.flatMap { sp -> | |
getFirstLevelDependenciesForConfiguration(sp.configurations, c.name)!!.stream() | |
} | |
Stream.concat(parentDeps, childDeps) | |
}.collect( | |
Collectors.toSet() | |
) | |
val moduleDataSet: Set<ModuleData> = source.allDependencies.stream().filter { md -> | |
firstLevelDependencies.stream().anyMatch { dependency -> | |
md.name == dependency.moduleName && md.group == dependency.moduleGroup && md.version == dependency.moduleVersion | |
} | |
}.collect(Collectors.toSet()) | |
return object : ProjectData(source.project, source.configurations, source.importedModules) { | |
override fun getAllDependencies(): Set<ModuleData> { | |
return moduleDataSet | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If someone gets this to work, I owe them a beer.