【问题标题】:How to import dependecies from build.gradle to pom.xml如何将依赖项从 build.gradle 导入 pom.xml
【发布时间】:2019-10-18 15:25:27
【问题描述】:

我使用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>

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

Optional Gradle dependencies for Maven libraries

How to change artifactory runtime scope to compile scope?

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,问题是生成的pom.xml 没有任何依赖关系。但我希望它具有 compile 范围的所有依赖项
  • @RaGe,当我构建 .aar 依赖项并将其包含在另一个应用程序中时,我需要包含我的库项目中的所有依赖项,因为我总是得到 ClassNotFoundException
  • 我上面的评论是针对一般的 gradle java 项目的——刚刚尝试过,compile 依赖项已正确写入 pom.xml 文件中。我想您的情况有所不同,因为它是一个 android 项目-环顾四周,我发现了一个专门用于在 android 插件中发布 .aar 文件的插件-在下面发布。

标签: maven gradle android-gradle-plugin pom.xml maven-publish


【解决方案1】:

要发布带有在 pom.xml 中正确列出的依赖项的 .aar 库,使用 this 插件可能更容易,而不是使用 maven-publish 插件自己组装依赖项部分。

要应用插件:

plugins {
  id "com.github.dcendents.android-maven" version "1.3"
}

并运行任务install 将库推送到本地.m2 repo。

您可以像这样覆盖正在发布的 repo:

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

如果您愿意,当然可以继续使用maven-publish 插件。在您的代码中,您正在寻找尚不存在的depsNode。您可能需要为 depsNode 创建一个新节点并首先将其添加到 pom 中。或者只是使用附加:

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,您也需要正确处理这些配置。

【讨论】:

  • 感谢您的回答。我今天稍后会尝试,如果它对我有帮助,我将不胜感激。
  • 我没有测试第一部分,因为第二部分适合我
  • 对于 gradle maven 插件版本 1.4.1,使用这个 configurations.releaseCompileClasspath.allDependencies.each 将所有依赖项写入 pom.xml跨度>
【解决方案2】:

您可以在 maven 发布任务中使用另一个项目或库的依赖项来更新依赖项。这必须在 jar 生成任务之前完成。这样,变化就会在 pom 生成中体现出来,不需要对 pom xml 进行操作。 getDependencies 是您提取这些依赖项的方法,您应该实现它;)

这是发布任务中的sn-p

        artifactId = POM_ARTIFACT_ID
        groupId = GROUP
        version = VERSION_NAME

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

        from components.java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2019-04-16
    • 1970-01-01
    • 2020-06-06
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多