10

I have a project which has a SharedCode (Java) module and secondly an Android (Android library) module which depends on the SharedCode module. I've previously been using the maven plugin in my build.gradle files and I've been using the uploadArchives task of that plugin to publish the artifacts from my two modules. This has worked and produced pom files which reflect the dependencies in my build.gradle files.

I thought I'd replace the old maven plugin with the new maven-publish plugin. However, I see that the pom files produced by the maven-publish plugin contain no dependencies. Is this by design, is this a bug in the plugin or am I using the plugin incorrectly?

The build.gradle file in my SharedCode module is as follows:

apply plugin: 'java'
apply plugin: 'maven-publish'

group = "${projectGroupId}"
version = "${projectVersionName}"

dependencies {
    compile 'com.google.guava:guava:18.0'
}

publishing {
    publications {
        SharedCode(MavenPublication) {
            groupId "${projectGroupId}"
            artifactId 'SharedCode'
            version "${projectVersionName}"
            artifact("$buildDir/libs/SharedCode-${projectVersionName}.jar")
        }
    }
}

The build.gradle file in my Android module is as follows:

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

group = "${projectGroupId}"
version = "${projectVersionName}"

android {
    // android stuff here...
}

dependencies {
    compile project(':SharedCode')
}

publishing {
    publications {
        Android(MavenPublication) {
            groupId "${projectGroupId}"
            artifactId 'Android'
            version "${projectVersionName}"
            artifact "$buildDir/outputs/aar/Android-release.aar"
        }
    }
}

The pom file created from the SharedCode module is as follows:

<?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>com.mycompany.sdk</groupId>
    <artifactId>SharedCode</artifactId>
    <version>0.0.2</version>

</project>

The pom file created from the Android module is as follows:

<?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>com.mycompany.sdk</groupId>
    <artifactId>Android</artifactId>
    <version>0.0.2</version>
    <packaging>aar</packaging>

</project>

Note the absence of the dependencies in the pom files.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147

1 Answers1

9

Use from components.java instead of the artifact... line in your publications section for the java project. That should produce the dependencies in the pom automatically.

The android project isn't recognized as a standard java project, which makes it a bit trickier. You can create your own dependencies section using pom.withXml {} and then iterating through your dependency list. Alternatively, there is this gradle plugin that does it for yout.

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • Awesome! Replacing the `artifact...` line for `from components.java` in the `SharedCode` module fixes the problem for the `SharedCode` module. Just need to find a solution now for the `Android` module. Will follow your leads to see if that leads to a solution. Will get back to you. – Adil Hussain Mar 08 '16 at 11:04
  • 1
    I had the exact same problem and describe a solution in this blogpost: http://jeroenmols.com/blog/2015/08/13/artifactory2/ (see dependencies section) – Jeroen Mols Mar 08 '16 at 15:15
  • Managed to solve the Android dependencies issue by adding `pom.withXml {}` to my `build.gradle`. This answer was a big help: http://stackoverflow.com/a/24764713/1071320 – Adil Hussain Mar 08 '16 at 15:15
  • 4
    Or try this [plugin](https://github.com/wupdigital/android-maven-publish) and use components.android – bvarga Aug 04 '17 at 19:44
  • Thanks for the link bvarga. Does that plugin take care of adding dependency exclusions in the generated pom file? – Adil Hussain Nov 08 '17 at 11:10
  • bvarga, I just tried it and yes it does! Thanks for bringing this plugin to my attention. It works like a charm! – Adil Hussain Nov 08 '17 at 11:25
  • **Update:** The [digital.wup.android-maven-publish](https://github.com/wupdigital/android-maven-publish) Gradle plugin has been deprecated since the [maven-publish](https://docs.gradle.org/current/userguide/publishing_maven.html) Gradle plugin is now supported by the Android Gradle plugin. See [here](https://developer.android.com/studio/build/maven-publish-plugin) for a short guide on how to use the `maven-publish` to publish an Android application or Android library. – Adil Hussain Apr 23 '21 at 12:44