// Based on https://stackoverflow.com/a/42160584/453052

project.ext.buildPomXmlDependencies = { pom, configurations ->
    pom.withXml {
        final rootNode = asNode().appendNode('dependencies')
        addConfigurationDependencies(rootNode, configurations.api, 'compile')
        addConfigurationDependencies(rootNode, configurations.compile, 'compile')
        addConfigurationDependencies(rootNode, configurations.implementation, 'runtime')
    }
}

private static def addConfigurationDependencies(rootNode, Configuration configuration, String scope) {
    configuration.dependencies.each { dep -> addChildDependency(rootNode, dep, scope) }
}

private static def addChildDependency(rootNode, Dependency dep, String scope) {
    if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
        return

    final childNode = rootNode.appendNode('dependency')
    childNode.appendNode('groupId', dep.group)
    childNode.appendNode('artifactId', dep.name)
    childNode.appendNode('version', dep.version)
    childNode.appendNode('scope', scope)

    if (!dep.transitive) {
        // If this dependency is transitive, we should force exclude all its dependencies them from the POM
        final exclusionNode = childNode.appendNode('exclusions').appendNode('exclusion')
        exclusionNode.appendNode('groupId', '*')
        exclusionNode.appendNode('artifactId', '*')
    } else if (!dep.properties.excludeRules.empty) {
        // Otherwise add specified exclude rules
        final exclusionNode = childNode.appendNode('exclusions').appendNode('exclusion')
        dep.properties.excludeRules.each { ExcludeRule rule ->
            exclusionNode.appendNode('groupId', rule.group ?: '*')
            exclusionNode.appendNode('artifactId', rule.module ?: '*')
        }
    }
}
