如何从build.gradle导入依赖项到pom.xml

3
我使用 maven-publish 插件来部署 Android 库(.aar)。
我的库有其他依赖项,这些依赖项也是 .aar
如何从 build.gradledependencies 部分导入所有依赖项?
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile ('com.android.support:recyclerview-v7:22.2.1'){
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment-support-v4:0.10.0'

    //Http communication, websockets, etc.
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'

    //Fonts
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'

    //Unit tests
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'

    //Other
    compile ('org.apache.commons:commons-lang3:3.4'){
        exclude group: 'org.apache.httpcomponents'
    }

    //Reactive programmnig
    compile 'io.reactivex:rxjava:1.0.13'
    compile 'io.reactivex:rxandroid:0.25.0'

    compile 'com.github.bumptech.glide:glide:3.6.1'
}

生成pom.xmldependencies部分:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.sdk</groupId>
<artifactId>SDK</artifactId>
<version>0.0.1</version>
<packaging>aar</packaging>

<dependencies>
   ...
</dependencies>

</project>

我找到了一些类似事情的解释:

使用Maven库的可选Gradle依赖项

如何将artifactory运行时范围更改为编译范围?

https://issues.gradle.org/browse/GRADLE-1749

http://www.scriptscoop.net/t/6ac07fb41846/how-to-maven-publish-a-gradle-project-jar-with-provided-scope.html

因此,我理解应该使用pom.withXml,从project.configurations.compile.allDependencies导入所有依赖项,并将其范围设置为compile,然后将其放入asNode().dependencies中。

但是我不熟悉它,我认为我做错了什么。这是我的当前代码:

publishing {
    publications {
        maven(MavenPublication) {
            artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar"
            artifactId = POM_ARTIFACT_ID
            groupId = GROUP
            version = VERSION_NAME

            // Task androidSourcesJar is provided by gradle-mvn-push.gradle
            //artifact androidSourcesJar {
            //    classifier "sources"
            //}

            pom.withXml {
                def depsNode = asNode().dependencies.'*'
                project.configurations.compile.allDependencies.each { dep ->
                    if(dep.name != null && dep.group != null && dep.version != null) {
                        def depNode = new Node(null, 'dependency')

                        def groupIdNode = new Node(depNode, 'groupId', dep.getGroup())
                        def artifactIdNode = new Node(depNode, 'artifactId', dep.getName())
                        def versionNode = new Node(depNode, 'version', dep.getVersion())


                        depsNode.add(depNode)
                        println depsNode
                    }
                }
            }
        }
    }

    repositories {
        maven {
            credentials {
                username System.getenv('NEXUS_USER_NAME')
                password System.getenv('NEXUS_PASSWORD')
            }
            url "http://nexus-repo"
        }
    }
}

您可以使用 pom.withXml 修改生成的 pom.xml 文件,但是您不应该需要进行任何自定义配置来包括所有运行时依赖项(默认情况下也包括编译依赖项)。您遇到的问题是什么? - RaGe
@RaGe,问题是生成的pom.xml没有任何依赖项。但我希望它具有所有带有“compile”范围的依赖项。 - Aleksandr
@RaGe,当我构建我的.aar依赖项并将其包含在另一个应用程序中时,我需要包含来自我的库项目的所有依赖项,因为我总是会收到ClassNotFoundException - Aleksandr
我上面的评论是针对Gradle Java项目的一般情况 - 我刚试过了,compile依赖项在pom.xml文件中正确编写。我想你的情况可能不同,因为这是一个Android项目 - 我找到了一个专门用于发布.aar文件的Android插件 - 下面发布。 - RaGe
2个回答

4
为了发布一个带有正确列在 pom.xml 中的依赖项的 .aar 库,你可以使用 这个 插件,而不是使用 maven-publish 插件自己组装依赖项部分。
要应用插件:
plugins {
  id "com.github.dcendents.android-maven" version "1.3"
}

运行任务 install 将库推送到本地的 .m2 存储库。

您可以像这样覆盖要发布的存储库:

install {
    repositories {
        mavenDeployer {
            repository(...)
        }
    }
}

当然,如果您愿意,仍然可以继续使用maven-publish插件。在您的代码中,您正在寻找尚不存在的depsNode。您可能需要先创建一个新的节点并将其添加到pom中,或者只需使用append即可。
pom.withXml {
    def depsNode  = asNode().appendNode('dependencies')

    configurations.compile.allDependencies.each {  dep ->
        def depNode  = depsNode.appendNode('dependency')
        depNode.appendNode('groupId', dep.group)
        depNode.appendNode('artifactId', dep.name)
        depNode.appendNode('version', dep.version)
        //optional add scope
        //optional add transitive exclusions
    }
}

这里需要注意的是,您仍然需要正确处理排除项。另外,如果您的项目中存在变体,并且具有不同的编译配置,例如androidCompilesomethingElseCompile,您也需要正确处理它们。


谢谢您的回答。我今天稍后会尝试一下,如果它能帮助我,我将不胜感激。 - Aleksandr
我没有测试第一部分,因为第二部分对我有效。 - Aleksandr
对于Gradle Maven插件版本1.4.1,请使用以下代码configurations.releaseCompileClasspath.allDependencies.each将所有依赖项写入pom.xml - Hassan Jamil

1
您可以在Maven发布任务中使用另一个项目或库的依赖项更新依赖项。这必须在生成JAR任务之前完成。通过这种方式,更改将反映在POM生成中,无需进行POM XML操作。getDependencies是提取这些依赖项的方法,您应该实现它 ;)
这是发布任务中的片段。
        artifactId = POM_ARTIFACT_ID
        groupId = GROUP
        version = VERSION_NAME

        project.configurations.implementation.withDependencies { dependencies ->            
             dependencies.addAll(getDependencies(project)) 
        }

        from components.java

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接