Skip to content

Instantly share code, notes, and snippets.

@comigor
Created November 14, 2018 15:33
Show Gist options
  • Save comigor/1ee0712d439f9c16510a592a2a217210 to your computer and use it in GitHub Desktop.
Save comigor/1ee0712d439f9c16510a592a2a217210 to your computer and use it in GitHub Desktop.
Generate a Pomfile to an Android library (AAR)

Maven Publish Pomfile Generator

To be used in conjunction of maven-publish to correctly generate an AAR Pomfile.

Why?

For some reason, I can't get maven-publish to generate a Pomfile when I'm exporting an Android library (AAR). This plugin generates the Pomfile by looking at library's classpath and exclusions.

Usage

Just import this file somewhere on your project and on the library build.gradle:

apply plugin: "maven-publish"
apply from: "$rootDir/scripts/maven-publish-pomfile-generation.gradle"

publishing {
    repositories {
        mavenLocal()
    }

    publications {
        release(MavenPublication) {
            groupId "me.borges"
            artifactId "my_library"
            version "1.0.0"

            artifact "$buildDir/outputs/aar/my_library-release.aar"

            pom.withXml pomfileManualGenerator
        }
    }
}

Then you can run:

./gradlew publishToMavenLocal
ext.pomfileManualGenerator = {
def root = asNode()
def dependenciesNode = root.appendNode('dependencies')
configurations.releaseRuntimeElements.allDependencies
.findAll {
it.name != null && it.name != 'unspecified' && it.version != 'unspecified' && !ext.pomFileBlacklist.contains(it.name)
}.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'compile')
if (it.properties.excludeRules?.size() > 0) {
def exclusionsNode = dependencyNode.appendNode('exclusions')
it.properties.excludeRules.each { exclusion ->
def exclusionNode = exclusionsNode.appendNode('exclusion')
exclusion.group != null && exclusionNode.appendNode('groupId', exclusion.group)
exclusionNode.appendNode('artifactId', exclusion.module != null ? exclusion.module : '*')
}
}
}
root.children().last()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment