Gradle不包括已发布的pom.xml中的依赖项

C. *_*oss 44 android gradle pom.xml maven

我有一个Gradle项目,我正在使用maven-publisher插件将我的android库安装到maven local和maven repo.

这可行,但生成的pom.xml不包含任何依赖项信息.是否有解决方法来包含该信息,或者我是否被迫返回maven插件并执行所需的所有手动配置?


研究我意识到我没有告诉出版物依赖的位置,我只是指定输出/工件,所以我需要一种方法将它链接MavenPublication到依赖项,但我还没有找到如何在文档.

------------------------------------------------------------
Gradle 1.10
------------------------------------------------------------

Build time:   2013-12-17 09:28:15 UTC
Build number: none
Revision:     36ced393628875ff15575fa03d16c1349ffe8bb6

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:          2.2.0
JVM:          1.7.0_60 (Oracle Corporation 24.60-b09)
OS:           Mac OS X 10.9.2 x86_64

相关的build.gradle部分

//...
apply plugin: 'android-library'
apply plugin: 'robolectric'
apply plugin: 'maven-publish'

//...
repositories {
     mavenLocal()
     maven  {
         name "myNexus"
         url myNexusUrl
     }
     mavenCentral()
}

//...
android.libraryVariants
publishing {
    publications {
        sdk(MavenPublication) {
            artifactId 'my-android-sdk'
            artifact "${project.buildDir}/outputs/aar/${project.name}-${project.version}.aar"
        }
    }
    repositories {
        maven  {
            name "myNexus"
            url myNexusUrl
            credentials {
                username myNexusUsername
                password myNexusPassword
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

生成的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example.android</groupId>
  <artifactId>my-android-sdk</artifactId>
  <version>gradle-SNAPSHOT</version>
  <packaging>aar</packaging>
</project>
Run Code Online (Sandbox Code Playgroud)

C. *_*oss 77

我能够通过让脚本直接使用将依赖项添加到pom来解决这个问题pom.withXml.

//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')

    //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
    configurations.compile.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于我的项目,它可能会在其他项目中产生无法预料的后果.

  • 我需要添加一个检查`it.group`,`it.name`不是null或未指定,以便它跳过android studio中lib目录的默认依赖.没有它,gradle会生成无效的POM文件并崩溃. (7认同)
  • +1太棒了.在我的`*(MavenPublication)`块中添加了这个,我所有的依赖项现在都在我的pom中.很多thx (6认同)
  • 如果您已切换到4.1 gradle version/3.0.1 cradle插件,则可以将"compile"替换为"implementation". (3认同)
  • "编译"今天对我不起作用.什么工作是"releaseCompileClasspath".例如,要遍历依赖项,请使用"configurations.releaseCompileClasspath.allDependencies.each" (3认同)
  • 假设aar具有其他aar依赖性。那么,如何发布它们呢?我尝试了相同的方法。该应用程序已构建,但崩溃(NoClassDefFoundError)。在将这些内部依赖关系包含在应用程序中之后,它可以正常运行而不会崩溃。请让我知道是否还有其他适当的方法可以做到这一点。 (2认同)

coo*_*rok 29

我升级了C.Ross解决方案.此示例将生成pom.xml,其中包含来自编译配置的依赖项以及特殊构建类型依赖项,例如,如果对发布或调试版本使用不同的依赖项(debugCompile和releaseCompile).而且它还添加了exlusions

publishing {
    publications {
        // Create different publications for every build types (debug and release)
        android.buildTypes.all { variant ->
            // Dynamically creating publications name
            "${variant.name}Aar"(MavenPublication) {

                def manifest = new XmlSlurper().parse(project.android.sourceSets.main.manifest.srcFile);
                def libVersion = manifest['@android:versionName'].text()
                def artifactName = project.getName()

                // Artifact properties
                groupId GROUP_ID
                version = libVersion
                artifactId variant.name == 'debug' ? artifactName + '-dev' : artifactName

                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")

                pom.withXml {
                    //Creating additional node for dependencies
                    def dependenciesNode = asNode().appendNode('dependencies')

                    //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile)
                    def configurationNames = ["${variant.name}Compile", 'compile']

                    configurationNames.each { configurationName ->
                        configurations[configurationName].allDependencies.each {
                            if (it.group != null && it.name != null) {
                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                dependencyNode.appendNode('groupId', it.group)
                                dependencyNode.appendNode('artifactId', it.name)
                                dependencyNode.appendNode('version', it.version)

                                //If there are any exclusions in dependency
                                if (it.excludeRules.size() > 0) {
                                    def exclusionsNode = dependencyNode.appendNode('exclusions')
                                    it.excludeRules.each { rule ->
                                        def exclusionNode = exclusionsNode.appendNode('exclusion')
                                        exclusionNode.appendNode('groupId', rule.group)
                                        exclusionNode.appendNode('artifactId', rule.module)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ped*_*mão 8

implemention引入了gradle 3 。替换compileimplementation。改用这个。

pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')
    configurations.implementation.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}
Run Code Online (Sandbox Code Playgroud)


Mea*_*man 6

已接受答案的 Kotlin DSL 版本:

    create<MavenPublication>("maven") {
        groupId = "com.example"
        artifactId = "sdk"
        version = Versions.sdkVersionName
        artifact("$buildDir/outputs/aar/Example-release.aar")
        pom.withXml {
            val dependenciesNode = asNode().appendNode("dependencies")
            val configurationNames = arrayOf("implementation", "api")
            configurationNames.forEach { configurationName ->
                configurations[configurationName].allDependencies.forEach {
                    if (it.group != null) {
                        val dependencyNode = dependenciesNode.appendNode("dependency")
                        dependencyNode.appendNode("groupId", it.group)
                        dependencyNode.appendNode("artifactId", it.name)
                        dependencyNode.appendNode("version", it.version)
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)