Aleksandr
Aleksandr

Reputation: 4936

How to import dependecies from build.gradle to pom.xml

I use maven-publish plugin for deploying android library(.aar).

My library has another dependencies, which are also .aar

How can I import all dependencies from build.gradle, dependencies section:

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

To generated pom.xml dependencies section:

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

I found some explanation how to do similar things:

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

So, I understood, that I should use pom.withXml, import all dependecies from project.configurations.compile.allDependencies with scope compile, and put it into asNode().dependencies

But I'm not familiar with it, and I think I doing something wrong. Here is my current code:

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

Upvotes: 3

Views: 7146

Answers (2)

codeAvax
codeAvax

Reputation: 91

You can update the dependencies with the dependencies of another project or library in the maven publish task. This has to be done before jar generation task. In this way, the changes will be reflected in the pom generation and no need for pom xml manipulation. getDependencies is the method that you extract those dependencies and you should implement it ;)

This is the snippet in the publish task

        artifactId = POM_ARTIFACT_ID
        groupId = GROUP
        version = VERSION_NAME

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

        from components.java

Upvotes: 1

RaGe
RaGe

Reputation: 23647

To publish a .aar library with the dependencies listed correctly in pom.xml, it might be easier to use this plugin, rather than assemble the dependencies section yourself using the maven-publish plugin.

To apply plugin:

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

and run task install to push library to local .m2 repo.

You can override the repo being published to like this:

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

You can of course, continue to use the maven-publish plugin if you so prefer. In your code, you're looking for depsNode which does not already exist. You likely need to create a new Node for depsNode and add it to the pom first. Or just use 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
    }
}

The caveat here is that, you still need to handle exclusions correctly. Also, if you have variants in your project, and have different compile configurations such as androidCompile or somethingElseCompile, you need to handle those correctly as well.

Upvotes: 4

Related Questions