如何将dependecies从build.gradle导入到pom.xml

时间:2015-12-27 20:53:35

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

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

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

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的所有dependecies,并将其放入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"
        }
    }
}

2 个答案:

答案 0 :(得分:4)

要在pom.xml中正确列出依赖项列出的function TItemContainer<T>.ItemClass: TItemClass; begin Result := T; end; 库,使用this插件可能更容易,而不是使用maven-publish插件自行组装依赖项部分。< / p>

申请插件:

.aar

并运行任务plugins { id "com.github.dcendents.android-maven" version "1.3" } 以将库推送到本地.m2 repo。

您可以覆盖发布的repo,如下所示:

install

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

depsNode

这里需要注意的是,您仍然需要正确处理排除。此外,如果项目中有变体,并且具有不同的编译配置,例如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,则还需要正确处理这些变体。

答案 1 :(得分: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