Forked from Robyer/maven-publish-helper-usage.gradle
Last active
November 26, 2019 13:54
-
-
Save hameno/5d9b6208b8eb0af855e29c0218d38433 to your computer and use it in GitHub Desktop.
Gradle script for publishing Android library to local Maven repository using maven-publish plugin. With dependencies (also handling transitive: false and custom exclude rules), including sources and javadoc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'maven-publish' | |
project.afterEvaluate { | |
def androidExtension = project.extensions.findByName("android") | |
if (androidExtension.hasProperty('libraryVariants')) { | |
androidExtension.libraryVariants.all { final variant -> | |
task("${variant.name}Javadoc", type: Javadoc) { | |
description "Generates Javadoc for ${variant.name}." | |
failOnError = false | |
source = variant.javaCompile.source | |
classpath = files(variant.javaCompile.classpath.files, project.android.getBootClasspath()) | |
exclude '**/BuildConfig.java' | |
exclude '**/R.java' | |
} | |
} | |
} | |
task androidJavadocsJar(type: Jar, dependsOn: 'releaseJavadoc') { | |
classifier = 'javadoc' | |
from { | |
releaseJavadoc.destinationDir | |
} | |
} | |
task androidSourcesJar(type: Jar) { | |
classifier = 'sources' | |
from androidExtension.sourceSets.main.java.srcDirs | |
} | |
publishing.publications { | |
release(MavenPublication) { | |
groupId project.group | |
artifactId project.name | |
version project.version | |
artifact bundleRelease | |
artifact androidJavadocsJar | |
artifact androidSourcesJar | |
pom.withXml { | |
// List all compile dependencies and write to POM | |
final Node dependenciesNode = asNode().appendNode('dependencies') | |
final compileConfiguration = configurations.getByName('compile') | |
compileConfiguration.allDependencies.each { final Dependency dependency -> | |
final def noGroup = dependency.group == null | |
final def noVersion = dependency.version == null | |
final def noName = dependency.name == null | |
final def invalidName = dependency.name == "unspecified" | |
final def invalidDependency = noGroup || noVersion || noName || invalidName | |
if (invalidDependency) { | |
// ignore invalid dependencies | |
return | |
} | |
final def dependencyFiles = compileConfiguration.files(dependency) | |
final def firstFile = dependencyFiles.first() | |
final def firstFileName = firstFile.name | |
final def firstFileNameExtension = firstFileName.substring(firstFileName.lastIndexOf(".") + 1) | |
final dependencyNode = dependenciesNode.appendNode('dependency') | |
dependencyNode.appendNode('groupId', dependency.group) | |
dependencyNode.appendNode('artifactId', dependency.name) | |
dependencyNode.appendNode('version', dependency.version) | |
dependencyNode.appendNode('type', firstFileNameExtension) | |
if (!dependency.transitive) { | |
// If this dependency is transitive, we should force exclude all its dependencies them from the POM | |
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion') | |
exclusionNode.appendNode('groupId', '*') | |
exclusionNode.appendNode('artifactId', '*') | |
} else if (!dependency.properties.excludeRules.empty) { | |
// Otherwise add specified exclude rules | |
final exclusionsNode = dependencyNode.appendNode('exclusions') | |
dependency.properties.excludeRules.each { final ExcludeRule rule -> | |
final exclusionNode = exclusionsNode.appendNode('exclusion') | |
exclusionNode.appendNode('groupId', rule.group ?: '*') | |
exclusionNode.appendNode('artifactId', rule.module ?: '*') | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
publishing { | |
repositories { | |
maven { | |
url "$buildDir/repo" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment