使用 Gradle 的 POM 中缺少 Android 库依赖项 [英] Android library dependencies missing from POM with Gradle

查看:60
本文介绍了使用 Gradle 的 POM 中缺少 Android 库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

Gradle 2.2 and 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'
            }
        }
    }
}

更新

问题的根本原因是这个项目使用了flavors.使用 apply plugin: 'maven'

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

推荐答案

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

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

这篇关于使用 Gradle 的 POM 中缺少 Android 库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆