使用Gradle从POM中缺少Android库依赖项

时间:2015-03-03 08:55:21

标签: android maven gradle android-library

我正在使用Gradle构建一个Android库项目,并将其作为aar部署到maven存储库。

库有一些依赖项,应该包含在POM中

使用apply plugin: 'maven'时,不存在POM文件,只有工件

使用apply plugin: 'maven-publish'生成POM文件,但它不包含任何依赖项

有什么想法吗?这只是不支持吗?

Gradle 2.2和Android Gradle Plugin 1.1.0

第一种方法:

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'
            }
        }
    }
}

在没有将其包装在afterEvaluate

中的情况下也尝试了

第二种方法:

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'
            }
        }
    }
}

更新

问题的根本原因是该项目使用了味道。没有风味,使用apply plugin: 'maven'

时会正确生成pom

2 个答案:

答案 0 :(得分:16)

这是最终对我有用的解决方案:

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.implementation.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
            }      
        }
    }
}

答案 1 :(得分:0)

尝试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'
        }
    }
}

在这里你可以设置pom细节。您将获得一个名为uploadArchives的新目标。执行时,它将部署到给定的仓库。