All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.nebula.dependency.recommender.publisher.MavenBomXmlGenerator.groovy Maven / Gradle / Ivy

Go to download

Allows projects to leave off version numbers in dependencies section and have versions recommended by other sources

There is a newer version: 12.5.1
Show newest version
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