How to import dependecies from build.gradle to pom.xml
Asked Answered
P

2

3

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"
        }
    }
}
Percent answered 27/12, 2015 at 20:53 Comment(4)
You use pom.withXml to modify the generated pom.xml, but you shouldn't have to do any custom config to include all runtime dependencies (which by default also includes compile dependencies.) What is the problem you're running into?Ashur
@RaGe, problem is that generated pom.xml has no any dependencies. But I want it to have all dependencies with compile scopePercent
@RaGe, when I built my .aar dependency and include it in another app, I need include all dependecies from my library project, because I always get ClassNotFoundExceptionPercent
My comment above is for a gradle java project in general - just tried it and the compile dependencies are written properly in the pom.xml file. I suppose your situation is different because it is an android project - looking around I found a plugin specifically for publishing .aar files in an android plugin - posting below.Ashur
A
4

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.

Ashur answered 29/12, 2015 at 15:25 Comment(3)
Thank you for your answer. I will try it today later and I'll appreciate it if it will help me.Percent
I didn't test first part, because second part works for mePercent
For gradle maven plugin version 1.4.1, use this configurations.releaseCompileClasspath.allDependencies.each to write all dependencies in pom.xmlProudfoot
X
1

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
Xanthin answered 18/10, 2019 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.