netflix.nebula.dependency.recommender.publisher.MavenBomXmlGenerator.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nebula-dependency-recommender Show documentation
Show all versions of nebula-dependency-recommender Show documentation
Allows projects to leave off version numbers in dependencies section and have versions recommended by other sources
package netflix.nebula.dependency.recommender.publisher
import netflix.nebula.dependency.recommender.ModuleNotationParser
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.publish.maven.MavenPublication
class MavenBomXmlGenerator {
Project project
MavenBomXmlGenerator(Project project) {
this.project = project
}
void fromConfigurations(Closure configurationsClosure) {
MavenPublication pub = configurationsClosure.delegate.delegate
Iterable configurations
def configurationsRet = configurationsClosure()
if(configurationsRet instanceof Configuration)
configurations = [(Configuration) configurationsRet]
else if(Iterable.class.isAssignableFrom(configurationsRet.class))
configurations = configurationsRet
generateDependencyManagementXml(pub, configurations.collect { getManagedDependencies(it) }.flatten())
}
void withDependencies(Closure dependenciesClosure) {
MavenPublication pub = dependenciesClosure.delegate.delegate
Iterable dependencies = null
def dependenciesRet = dependenciesClosure()
if(dependenciesRet instanceof String)
dependencies = [(String) dependenciesRet]
else if(Iterable.class.isAssignableFrom(dependenciesRet.class))
dependencies = dependenciesRet
generateDependencyManagementXml(pub, dependencies.collect { ModuleNotationParser.parse(it) })
}
protected static generateDependencyManagementXml(MavenPublication pub, Iterable deps) {
pub.pom.withXml {
Node root = it.asNode()
def dependencyManagement = root.getByName("dependencyManagement")
// when merging two or more sources of dependencies, we want to only create one dependencyManagement section
Node dependencies
if(dependencyManagement.isEmpty())
dependencies = root.appendNode("dependencyManagement").appendNode("dependencies")
else
dependencies = dependencyManagement[0].getByName("dependencies")[0]
deps.each { mvid ->
Node dep = dependencies.appendNode("dependency")
dep.appendNode("groupId").value = mvid.group
dep.appendNode("artifactId").value = mvid.name
dep.appendNode("version").value = mvid.version
}
}
}
protected static Set getManagedDependencies(Configuration configuration) {
getManagedDependenciesRecursive(configuration.resolvedConfiguration.firstLevelModuleDependencies,
new HashSet())
}
protected static Set getManagedDependenciesRecursive(Set deps, Set all) {
deps.each { dep ->
all << dep.module.id
getManagedDependenciesRecursive(dep.children, all)
}
return all
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy