Android的依赖库失踪POM与摇篮 [英] Android library dependencies missing from POM with Gradle

查看:199
本文介绍了Android的依赖库失踪POM与摇篮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的摇篮构建一个Android库项目,并部署到Maven仓库作为AAR。

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

使用应用插件:'行家'没有POM文件是present,只是神器

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

使用应用插件:'Maven的发布生成一个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?

摇篮2.2和Android摇篮插件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'
            }
        }
    }
}

更新

问题的根本原因是,这个项目采用的味道。 行家

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.compile.allDependencies.each { 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
            }      
        }
    }
}

这篇关于Android的依赖库失踪POM与摇篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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