Android library dependencies missing from POM with

2019-01-19 13:36发布

问题:

I am using Gradle to build an Android library project and deploy it to maven repository as an aar.

The library has some dependencies, which should be included in the POM

With apply plugin: 'maven' no POM file is present, just the artifact

With apply plugin: 'maven-publish' a POM file is generated, but it does not include any dependencies

Any ideas? Is this just not supported?

Gradle 2.2 and Android Gradle Plugin 1.1.0

First approach:

configurations {
    archives {
        extendsFrom configurations.default
    }
}

afterEvaluate { project ->
    uploadArchives {

        configuration = configurations.archives

        repositories {
            mavenDeployer {
                repository(url: "http://nexus-url") {
                    authentication(userName: nexusUsername, password: nexusPassword)

                pom.groupId = 'com.example'
                pom.version = '123-SNAPSHOT'
                pom.artifactId = 'foo'
                pom.packaging = 'aar'

                pom.project {
                    artifactId = 'bar'
                    packaging 'aar'
                    description 'baz'
            }
        }
    }
}

Also tried it without wrapping it in afterEvaluate

Second approach:

publishing {
    publications {
        sdk(MavenPublication) {
            groupId 'com.example'
            artifactId 'foo'
            version = "0.123-SNAPSHOT"
            artifact("$buildDir/outputs/aar/app-sdk-debug.aar")
        }
    }
    repositories {
        maven {
            url "http://nexus-url"
            credentials {
                username 'foo'
                password 'bar'
            }
        }
    }
}

Update

The root cause of the problem is that this project uses flavors. Without flavors the pom is generated properly when using apply plugin: 'maven'

回答1:

This is the solution that worked for me in the end:

publishing {
    publications {
        sdk(MavenPublication) {
            artifactId libName
            artifact "${project.buildDir}/outputs/aar/${libName}-${project.version}.aar"

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                // for dependencies and exclusions
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.withType(ModuleDepend‌​ency) { ModuleDependency dp ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dp.group)
                    dependencyNode.appendNode('artifactId', dp.name)
                    dependencyNode.appendNode('version', dp.version)

                    // for exclusions
                    if (dp.excludeRules.size() > 0) {
                        def exclusions = dependencyNode.appendNode('exclusions')
                        dp.excludeRules.each { ExcludeRule ex ->
                            def exclusion = exclusions.appendNode('exclusion')
                            exclusion.appendNode('groupId', ex.group)
                            exclusion.appendNode('artifactId', ex.module)
                        }
                    }
                }
            }
        }
    }

    repositories {
        maven {
            name 'myrepo'
            url 'https://maven.foo.com'

            credentials {
                username mavenUsername
                password mavenPassword
            }      
        }
    }
}


回答2:

try mavenDeployer: http://gradle.org/docs/current/userguide/maven_plugin.html

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
            pom.version = '1.0Maven'
            pom.artifactId = 'myMavenName'
        }
    }
}

here you could set the pom details. You'll get a new goal called uploadArchives. When executed, it deploys to a given repo.