Gradle maven-publish plugin produces pom with no dependencies
Asked Answered
M

1

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.

Moy answered 7/3, 2016 at 11:46 Comment(0)
P
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.

Pomology answered 7/3, 2016 at 20:15 Comment(7)
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.Moy
I had the exact same problem and describe a solution in this blogpost: jeroenmols.com/blog/2015/08/13/artifactory2 (see dependencies section)Thekla
Managed to solve the Android dependencies issue by adding pom.withXml {} to my build.gradle. This answer was a big help: https://mcmap.net/q/908692/-gradle-not-including-dependencies-in-published-pom-xmlMoy
Or try this plugin and use components.androidLenardlenci
Thanks for the link bvarga. Does that plugin take care of adding dependency exclusions in the generated pom file?Moy
bvarga, I just tried it and yes it does! Thanks for bringing this plugin to my attention. It works like a charm!Moy
Update: The digital.wup.android-maven-publish Gradle plugin has been deprecated since the maven-publish Gradle plugin is now supported by the Android Gradle plugin. See here for a short guide on how to use the maven-publish to publish an Android application or Android library.Moy

© 2022 - 2024 — McMap. All rights reserved.