首页 文章

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

提问于
浏览
39

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

3 回答

  • 70

    通过让脚本直接使用 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)
        }
    }
    

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

  • 26

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

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

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

相关问题