首页 文章

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

提问于
浏览
9

我正在使用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'
            }
        }
    }
}

Update

问题的根本原因是该项目使用了味道 . 如果没有味道,使用 apply plugin: 'maven' 时可以正确生成pom

2 回答

  • 15

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

    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
                }      
            }
        }
    }
    
  • 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 的新目标 . 执行时,它将部署到给定的仓库 .

相关问题