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

时间:2014-07-14 18:44:42

标签: android maven gradle pom.xml

我有一个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
            }
        }
    }
}

生成的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>

7 个答案:

答案 0 :(得分:72)

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

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

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

答案 1 :(得分:28)

我升级了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)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

答案 2 :(得分:2)

使用gradle 3引入了implemention来代替compile。 改用它。

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

答案 3 :(得分:2)

Kotlin DSL版本的接受答案:

Dim cCount As Integer
Dim azr As Microsoft.Office.Interop.Excel.Range
azr = xlWorkSheet.UsedRange

Dim a As String
Dim b As String
a = DateTime.Now.AddDays(-3).ToString("MM/dd/yyyy HH:mm:ss tt")
b = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt")

Dim ex As String
ex = azr.Cells(cCount, 2).value.ToString

If ((ex >= a) And b >= ex) Then
    For cCount = 2 To azr.Rows.Count
        Dim newitem As New ListViewItem()

        newitem.Text = azr.Cells(cCount, 1).value.ToString
        newitem.SubItems.Add(azr.Cells(cCount, 2).value.ToString)
        newitem.SubItems.Add(azr.Cells(cCount, 3).value.ToString)
        newitem.SubItems.Add(azr.Cells(cCount, 4).value.ToString)
        newitem.SubItems.Add(azr.Cells(cCount, 5).value.ToString)

        LvRawExcel.Items.Add(newitem)

        count = count + 1
        LblCount.Text = count
    Next
End If

答案 4 :(得分:1)

我想它与from components.java指令有关,如guide所示。我有一个类似的设置,它的不同之处在于将该行添加到发布块中:

publications {
    mavenJar(MavenPublication) {
        artifactId 'rest-security'
        artifact jar
        from components.java
    }
}

答案 5 :(得分:0)

我正在使用maven-publish插件发布我的aar依赖项,实际上我无法使用maven任务。因此,我使用了mavenJava插件提供的maven-publish任务,并按如下方式使用它。

apply plugin 'maven-publish'

publications {
    mavenAar(MavenPublication) {
        from components.android
    }

    mavenJava(MavenPublication) {
        pom.withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            // Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
            configurations.api.allDependencies.each {
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', it.group)
                dependencyNode.appendNode('artifactId', it.name)
                dependencyNode.appendNode('version', it.version)
            }
        }
    }
}

我希望它对寻求使用aar插件发布pommaven-publish文件的人有所帮助。

答案 6 :(得分:0)

现在不建议使用编译,我们必须使用实现。

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