-
-
Save murijr/4aac7f10adde8fc9fdff5aae2a560b87 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' | |
task androidJavadocs(type: Javadoc) { | |
source = android.sourceSets.main.java.srcDirs | |
classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) | |
android.libraryVariants.all { variant -> | |
if (variant.name == 'release') { | |
owner.classpath += variant.javaCompile.classpath | |
} | |
} | |
exclude '**/R.html', '**/R.*.html', '**/index.html' | |
} | |
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { | |
classifier = 'javadoc' | |
from androidJavadocs.destinationDir | |
} | |
task androidSourcesJar(type: Jar) { | |
classifier = 'sources' | |
from android.sourceSets.main.java.srcDirs | |
} | |
publishing { | |
publications { | |
maven(MavenPublication) { | |
//groupId 'cz.example' | |
//artifactId 'custom-artifact' | |
//version = android.defaultConfig.versionName | |
artifact bundleReleaseAar | |
artifact androidJavadocsJar | |
artifact androidSourcesJar | |
pom.withXml { | |
final dependenciesNode = asNode().appendNode('dependencies') | |
ext.addDependency = { Dependency dep, String scope -> | |
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified") | |
return // ignore invalid dependencies | |
final dependencyNode = dependenciesNode.appendNode('dependency') | |
dependencyNode.appendNode('groupId', dep.group) | |
dependencyNode.appendNode('artifactId', dep.name) | |
dependencyNode.appendNode('version', dep.version) | |
dependencyNode.appendNode('scope', scope) | |
if (!dep.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 (!dep.properties.excludeRules.empty) { | |
// Otherwise add specified exclude rules | |
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion') | |
dep.properties.excludeRules.each { ExcludeRule rule -> | |
exclusionNode.appendNode('groupId', rule.group ?: '*') | |
exclusionNode.appendNode('artifactId', rule.module ?: '*') | |
} | |
} | |
} | |
// List all "compile" dependencies (for old Gradle) | |
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") } | |
// List all "api" dependencies (for new Gradle) as "compile" dependencies | |
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") } | |
// List all "implementation" dependencies (for new Gradle) as "runtime" dependencies | |
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment