Gradle not including dependencies in published pom.xml

Solution 1:

I was able to work around this by having the script add the dependencies to the pom directly using pom.withXml.

//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')

    //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
    configurations.compile.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}

This works for my project, it may have unforeseen consequences in others.

Solution 2:

I'm upgraded C.Ross solution. This example will generate pom.xml with dependecies from compile configuration and also with special build type dependecies, for example if you use different dependencies for release or debug version (debugCompile and releaseCompile). And also it adding exlusions

publishing {
    publications {
        // Create different publications for every build types (debug and release)
        android.buildTypes.all { variant ->
            // Dynamically creating publications name
            "${variant.name}Aar"(MavenPublication) {

                def manifest = new XmlSlurper().parse(project.android.sourceSets.main.manifest.srcFile);
                def libVersion = manifest['@android:versionName'].text()
                def artifactName = project.getName()

                // Artifact properties
                groupId GROUP_ID
                version = libVersion
                artifactId variant.name == 'debug' ? artifactName + '-dev' : artifactName

                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")

                pom.withXml {
                    //Creating additional node for dependencies
                    def dependenciesNode = asNode().appendNode('dependencies')

                    //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile)
                    def configurationNames = ["${variant.name}Compile", 'compile']

                    configurationNames.each { configurationName ->
                        configurations[configurationName].allDependencies.each {
                            if (it.group != null && it.name != null) {
                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                dependencyNode.appendNode('groupId', it.group)
                                dependencyNode.appendNode('artifactId', it.name)
                                dependencyNode.appendNode('version', it.version)

                                //If there are any exclusions in dependency
                                if (it.excludeRules.size() > 0) {
                                    def exclusionsNode = dependencyNode.appendNode('exclusions')
                                    it.excludeRules.each { rule ->
                                        def exclusionNode = exclusionsNode.appendNode('exclusion')
                                        exclusionNode.appendNode('groupId', rule.group)
                                        exclusionNode.appendNode('artifactId', rule.module)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Solution 3:

With gradle 3 implemention was introduced. Replace compile with implementation. Use this instead.

pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')
    configurations.implementation.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}

Solution 4:

I guess it has something to do with the from components.java directive, as seen in the guide. I had a similar setup and it made the difference to add the line into the publication block:

publications {
    mavenJar(MavenPublication) {
        artifactId 'rest-security'
        artifact jar
        from components.java
    }
}

Solution 5:

Kotlin DSL version of the accepted answer:

    create<MavenPublication>("maven") {
        groupId = "com.example"
        artifactId = "sdk"
        version = Versions.sdkVersionName
        artifact("$buildDir/outputs/aar/Example-release.aar")
        pom.withXml {
            val dependenciesNode = asNode().appendNode("dependencies")
            val configurationNames = arrayOf("implementation", "api")
            configurationNames.forEach { configurationName ->
                configurations[configurationName].allDependencies.forEach {
                    if (it.group != null) {
                        val dependencyNode = dependenciesNode.appendNode("dependency")
                        dependencyNode.appendNode("groupId", it.group)
                        dependencyNode.appendNode("artifactId", it.name)
                        dependencyNode.appendNode("version", it.version)
                    }
                }
            }
        }
    }