Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 {
    defdepsNode= asNode().appendNode('dependencies')

    configurations.compile.allDependencies.each {  dep ->
        defdepNode= 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.

Solution 2:

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

Post a Comment for "How To Import Dependecies From Build.gradle To Pom.xml"