Gradle not including dependencies in published pom.xml
Asked Answered
M

7

57

I have a Gradle project I'm using the maven-publisher plugin to install my android library to maven local and a maven repo.

That works, but the generated pom.xml does not include any dependency information. Is there a workaround to include that information, or am I forced to go back to the maven plugin and do all the manual configuration that requires?


Researching I realized that I'm not telling the publication where the dependencies are, I'm only specifying the output/artifact, so I need a way to link this MavenPublication to the dependencies, but I have not yet found how to do that in the documentation.

------------------------------------------------------------
Gradle 1.10
------------------------------------------------------------

Build time:   2013-12-17 09:28:15 UTC
Build number: none
Revision:     36ced393628875ff15575fa03d16c1349ffe8bb6

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:          2.2.0
JVM:          1.7.0_60 (Oracle Corporation 24.60-b09)
OS:           Mac OS X 10.9.2 x86_64

Relevant build.gradle sections

//...
apply plugin: 'android-library'
apply plugin: 'robolectric'
apply plugin: 'maven-publish'

//...
repositories {
     mavenLocal()
     maven  {
         name "myNexus"
         url myNexusUrl
     }
     mavenCentral()
}

//...
android.libraryVariants
publishing {
    publications {
        sdk(MavenPublication) {
            artifactId 'my-android-sdk'
            artifact "${project.buildDir}/outputs/aar/${project.name}-${project.version}.aar"
        }
    }
    repositories {
        maven  {
            name "myNexus"
            url myNexusUrl
            credentials {
                username myNexusUsername
                password myNexusPassword
            }
        }
    }
}

Generated pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example.android</groupId>
  <artifactId>my-android-sdk</artifactId>
  <version>gradle-SNAPSHOT</version>
  <packaging>aar</packaging>
</project>
Milord answered 14/7, 2014 at 18:44 Comment(1)
'maven-publisher plugin': do you mean the maven-publish plugin ?Helgahelge
M
87

I was able to work around this by having the script add the dependencies to the pom directly using pom.withXml.

//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')

    //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
    configurations.compile.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}

This works for my project, it may have unforeseen consequences in others.

Milord answered 15/7, 2014 at 17:41 Comment(14)
+1 fantastic. Added this inside my *(MavenPublication) block and all my dependencies are now in my aar's pom. Many thxPersson
I just tried this and it caused an exception for duplicate 'dependencies' tags when parsing the generated tree. It seems the dependencies were being generated all along but still aren't being added to the published file for some reason. It sounds like I (and possibly others) could be suffering from the same problem but with a different root cause.Renn
I needed to add a check for it.group, it.name not being null or unspecified so that it skips the default dependency on lib directory in android studio. Without it gradle generates an invalid POM file and crashes.Papagena
Do you have dependencies on another android modules? For me it seems they are not added to <dependencies>Chaparro
Better to check if (it instanceof ExternalModuleDependency) before adding it to the pom file (similar to what @adwiv suggested but seems cleaner to me)Monia
Suppose that the aar has other aar dependencies. Then, how to publish them? I tried the same way. The app is built, but getting crash (NoClassDefFoundError). After I included those inner dependencies in the app, it is running without any crash. Kindly let me know if there is any other proper way to do it.Hormuz
@c. Ross Can you provide one file that uses this snippet, when I am trying to use this getting a build error asNode() not recognised :(Crossrefer
You can replace "compile" with "implementation" if you’ve switched to the 4.1 gradle version/3.0.1 cradle plugin.Phenanthrene
"compile" doesn't work for me today. What does work is "releaseCompileClasspath". E.g. to iterate over the dependencies use "configurations.releaseCompileClasspath.allDependencies.each"Acidimetry
@Hormuz according to this blog post "Note that while you can similarly add other repositories in this way to the pom.xml, Gradle itself won’t look for repositories in that file. Therefore if you use libraries from 3rd party repositories, you still need to add those repositories to the build.gradle of your app."Bohun
Nice solution! How would it work for dependencies like the following? implementation project ":myModule" In my case, the version is marked as unspecified in the resulting pom.xml. Do you know of any mechanism for getting the version number in these situations?Slapup
Sorry @Slapup I haven't touched Gradle in two years, and I don't recall that type.Milord
Still relevant today. The plugin should get an update to handle cases like this more naturally.Everyway
You need to add <scope> </scope> tag too.Preoccupancy
S
30

I'm upgraded C.Ross solution. This example will generate pom.xml with dependecies from compile configuration and also with special build type dependecies, for example if you use different dependencies for release or debug version (debugCompile and releaseCompile). And also it adding exlusions

publishing {
    publications {
        // Create different publications for every build types (debug and release)
        android.buildTypes.all { variant ->
            // Dynamically creating publications name
            "${variant.name}Aar"(MavenPublication) {

                def manifest = new XmlSlurper().parse(project.android.sourceSets.main.manifest.srcFile);
                def libVersion = manifest['@android:versionName'].text()
                def artifactName = project.getName()

                // Artifact properties
                groupId GROUP_ID
                version = libVersion
                artifactId variant.name == 'debug' ? artifactName + '-dev' : artifactName

                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")

                pom.withXml {
                    //Creating additional node for dependencies
                    def dependenciesNode = asNode().appendNode('dependencies')

                    //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile)
                    def configurationNames = ["${variant.name}Compile", 'compile']

                    configurationNames.each { configurationName ->
                        configurations[configurationName].allDependencies.each {
                            if (it.group != null && it.name != null) {
                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                dependencyNode.appendNode('groupId', it.group)
                                dependencyNode.appendNode('artifactId', it.name)
                                dependencyNode.appendNode('version', it.version)

                                //If there are any exclusions in dependency
                                if (it.excludeRules.size() > 0) {
                                    def exclusionsNode = dependencyNode.appendNode('exclusions')
                                    it.excludeRules.each { rule ->
                                        def exclusionNode = exclusionsNode.appendNode('exclusion')
                                        exclusionNode.appendNode('groupId', rule.group)
                                        exclusionNode.appendNode('artifactId', rule.module)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Seibold answered 8/9, 2015 at 7:35 Comment(3)
Thanks for the snippet! I suspect I'll have to manage the variants myself (will update this thread if I have something working)Veilleux
Comparing it to the previous pom that was created for the uploadArchives task it appears to be missing the scope attribute. Is there a way to easily include that to your code above?Armadillo
scope in pom.xml should be the same as configurationName. There is line def configurationNames = ["${variant.name}Compile", 'compile'] debugCompile and releaseCompile shoulb be skipped, but configuration name compile is that you need in scope attribute. To make pom with dependensies of all scopes you need to add all of them (runtime, etc.) in that array. And in loop just check if it's not debugCompile or releaseCompile append node scope. exclusionNode.appendNode('scope', configurationName)Seibold
B
9

With gradle 3 implemention was introduced. Replace compile with implementation. Use this instead.

pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')
    configurations.implementation.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
}
Betweenwhiles answered 20/3, 2019 at 12:27 Comment(0)
H
8

Kotlin DSL version of the accepted answer:

    create<MavenPublication>("maven") {
        groupId = "com.example"
        artifactId = "sdk"
        version = Versions.sdkVersionName
        artifact("$buildDir/outputs/aar/Example-release.aar")
        pom.withXml {
            val dependenciesNode = asNode().appendNode("dependencies")
            val configurationNames = arrayOf("implementation", "api")
            configurationNames.forEach { configurationName ->
                configurations[configurationName].allDependencies.forEach {
                    if (it.group != null) {
                        val dependencyNode = dependenciesNode.appendNode("dependency")
                        dependencyNode.appendNode("groupId", it.group)
                        dependencyNode.appendNode("artifactId", it.name)
                        dependencyNode.appendNode("version", it.version)
                    }
                }
            }
        }
    }
Heliostat answered 16/9, 2019 at 12:42 Comment(1)
this answer will be popular very soon, for surePileup
I
3

I guess it has something to do with the from components.java directive, as seen in the guide. I had a similar setup and it made the difference to add the line into the publication block:

publications {
    mavenJar(MavenPublication) {
        artifactId 'rest-security'
        artifact jar
        from components.java
    }
}
Intro answered 10/4, 2015 at 6:29 Comment(6)
this works when using .jar output but not for aar afaik. Apparently its broken for aar at present (gradle plugin 1.2.2)Persson
@Persson ran into same problem.. no dependencies for aar and a need to remove md5/sha files because of overwritten pomHydrometer
Directive still missing in version 1.5.0 of the Android Plugin for Gradle. If you add it to your Android project's build.gradle file, you'll see this error message when trying to parse the file: "Could not find property 'java' on SoftwareComponentInternal set."Tilghman
version 2.1.0 also doesn't work for aars. Is there a work around?Armadillo
This saved my life!Sabina
If multiple jars are included, then you must declare the classifier on each of them to avoid the error github.com/gradle/gradle/issues/1624#issuecomment-330821182. I declared the classifier for the main jar as default and I got both the sources and the main jar published!Sabina
D
0

I was using the maven-publish plugin for publishing my aar dependency and actually I could not use the maven task in my case. So I used the mavenJava task provided by the maven-publish plugin and used that as follows.

apply plugin 'maven-publish'

publications {
    mavenAar(MavenPublication) {
        from components.android
    }

    mavenJava(MavenPublication) {
        pom.withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            // Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
            configurations.api.allDependencies.each {
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', it.group)
                dependencyNode.appendNode('artifactId', it.name)
                dependencyNode.appendNode('version', it.version)
            }
        }
    }
}

I hope that it helps someone who is looking for help on how to publish the aar along with pom file using the maven-publish plugin.

Dimple answered 31/10, 2019 at 16:58 Comment(0)
O
0

now that compile is deprecated we have to use implementation.

pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
    configurations.implementation.allDependencies.each {
    def dependencyNode = dependenciesNode.appendNode('dependency')
    dependencyNode.appendNode('groupId', it.group)
    dependencyNode.appendNode('artifactId', it.name)
    dependencyNode.appendNode('version', it.version)
}
Oppression answered 21/11, 2019 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.