【问题标题】:Android library dependencies missing from POM with Gradle带有 Gradle 的 POM 中缺少 Android 库依赖项
【发布时间】:2015-05-03 20:32:12
【问题描述】:

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

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

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

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

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

Gradle 2.2 和 Android Gradle 插件 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

【问题讨论】:

    标签: android maven gradle android-library


    【解决方案1】:

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

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

    【讨论】:

    • 最好将configurations.compile.allDependencies.each替换成configurations.compile.allDependencies.withType(ModuleDependency),以免使用compile fileTree(dir: 'libs', include: ['*.jar'])时出现异常
    • 注意configurations.compile.allDependencies。已过时。对于当前版本的 gradle,它应该是 configurations.implementation.allDependencies 或它的变体(例如 api 而不是 implementation)
    • @Syzygy 上面的代码有错误,因为ModuleDependency in with method and before dp -> 不是同一个词,是不是拼写错误?
    • 当我在一个模块中有多种口味时该怎么办?我不想在每个风味发布中都写 pox.withXml。
    • 不确定,但也许matchingFallbacks 可以提供帮助? developer.android.com/studio/build/…
    【解决方案2】:

    试试 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 的新目标。执行时,它会部署到给定的存储库。

    【讨论】:

    • 当心 SNAPSHOT-Dependencies,Gradle 不遵循这个 maven 概念,这意味着如果您部署 SNAPSHOT-dependencies,如果它已经获得相同的版本(AFAIK),gradle 将不会更新它们
    • 谢谢,但这实际上是我提到apply plugin: 'maven' 时的意思,我会为这两种变体发布一些代码
    • 需要再读一遍 -> 你不希望在这个 pom.xml 中存在依赖关系。没有理由这样做,因为您的依赖项已融入到工件中。这就是编译所做的。这个 pom 只是对你的工件的描述。
    • 只烘焙 jar 依赖项。aar 依赖项是可传递的,需要在 POM 中引用或显式包含在主机应用程序的构建文件中。以otto为例:central.maven.org/maven2/com/squareup/otto/1.3.6/otto-1.3.6.pom
    • 似乎我从来没有遇到过这个问题,不过还是建议:看看这里github.com/spring-projects/gradle-plugins/tree/master/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2020-12-09
    • 2014-07-17
    • 1970-01-01
    相关资源
    最近更新 更多