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

2023-12-28

I use maven-publish用于部署 android 库的插件(.aar).

我的库还有另一个依赖项,它们也是.aar

如何导入所有依赖项build.gradle, dependencies部分:

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.xml dependencies部分:

<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 依赖项 https://stackoverflow.com/questions/25345804/optional-gradle-dependencies-for-maven-libraries

如何将工件运行时范围更改为编译范围? https://stackoverflow.com/questions/31069666/how-to-change-artifactory-runtime-scope-to-compile-scope

https://issues.gradle.org/browse/GRADLE-1749 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 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"
        }
    }
}

发布一个.aar在 pom.xml 中正确列出依赖项的库,可能会更容易使用this https://github.com/dcendents/android-maven-gradle-plugin插件,而不是使用 maven-publish 插件自己组装依赖项部分。

应用插件:

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

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

您可以覆盖正在发布的存储库,如下所示:

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

这里需要注意的是,您仍然需要正确处理排除。另外,如果您的项目中有变体,并且有不同的编译配置,例如androidCompile or somethingElseCompile,你也需要正确处理这些。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将依赖项从build.gradle导入到pom.xml 的相关文章

随机推荐