Skip to content

Instantly share code, notes, and snippets.

@gsavvid
Forked from Robyer/maven-publish-helper-usage.gradle
Last active November 2, 2022 12:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gsavvid/f7ffe00d846a50de7490c2ecbd7b4169 to your computer and use it in GitHub Desktop.
Save gsavvid/f7ffe00d846a50de7490c2ecbd7b4169 to your computer and use it in GitHub Desktop.
Gradle script for publishing Android Kotlin libraries to a Bintray repository using maven-publish plugin. With dependencies (also handling transitive: false and custom exclude rules), including sources and javadoc.
/**
* Publish Android Kotlin Library Helper
*
* This Gradle script is based on https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017.
* The difference is that:
* 1. It uses Dokka for generating Kotlin Javadoc.
* 2. It uses Jfrog's plugin for publishing to Bintray. If you don't want to publish to Bintray, simply remove all the Bintray-related code.
*
* NOTE: You need to add Dokka and Bintray to your classpath for the two plugins to work.
* Update the buildscript in your project's build.gradle with the following:
*
* buildscript {
* dependencies {
* classpath 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.18'
* classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
* }
* }
*
* @See https://github.com/Kotlin/dokka
* @See https://github.com/bintray/gradle-bintray-plugin
*
* @Author Georgios Savvidis
* @Version 1.0
* @Date 9.7.2019
*/
apply plugin: 'maven-publish'
apply plugin: 'org.jetbrains.dokka-android'
apply plugin: 'com.jfrog.bintray'
group = 'YOUR BINTRAY GROUP ID'
version = 'YOUR BINTRAY ARTIFACT VERSION'
// For more details on Bintray's configuration, see https://github.com/bintray/gradle-bintray-plugin.
bintray {
user = "YOUR BINTRAY USERNAME"
key = "YOUR BINTRAY API KEY"
publications = ['Production']
configurations = ['archives']
pkg {
repo = "YOUR BINTRAY REPO"
userOrg = "YOUR BINTRAY ORG"
name = "YOUR BINTRAY PACKAGE NAME"
dryRun = false
publish = true
override = false
publicDownloadNumbers = false
version {
gpg {
sign = true
passphrase = "YOUR BINTRAY GPG PASSPHRASE"
}
}
}
}
// For more details on Dokka's configuration, see https://github.com/Kotlin/dokka.
dokka {
// Minimal configuration.
outputFormat = 'javadoc'
outputDirectory = "$buildDir/javadoc"
// Use to include or exclude non public members.
includeNonPublic = false
// Do not output deprecated members. Applies globally, can be overridden by packageOptions.
skipDeprecated = false
// Emit warnings about not documented members. Applies globally, also can be overridden by packageOptions.
reportUndocumented = true
// Do not create index pages for empty packages
skipEmptyPackages = true
// Example of how to link to external documentation.
// The following links to AndroidX.
// Repeat for multiple links.
externalDocumentationLink {
// Root URL of the generated documentation to link with. The trailing slash is required!
url = new URL("https://developer.android.com/reference/")
// If package-list file located in non-standard location
packageListUrl = new URL("https://developer.android.com/reference/androidx/package-list")
}
}
task androidJavadocsJar(type: Jar, dependsOn: dokka) {
classifier = 'javadoc'
from dokka.outputDirectory
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
project.afterEvaluate {
publishing {
publications {
Production(MavenPublication) {
groupId "YOUR GROUP ID" // If you publish to Bintray, make sure that this is the same as the "group" field at the top of this file.
artifactId "YOUR ARTIFACT ID"
version = "YOUR ARTIFACT VERSION" // If you publish to Bintray, make sure that this is the same as the "version" field at the top of this file.
artifact bundleReleaseAar
artifact androidJavadocsJar
artifact androidSourcesJar
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { 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 not transitive, we should force exclude all its dependencies 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 exclusionsNode = dependencyNode.appendNode('exclusions')
dep.properties.excludeRules.each { rule ->
final exclusionNode = exclusionsNode.appendNode('exclusion')
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