Skip to content

Instantly share code, notes, and snippets.

@rubensousa
Last active January 18, 2024 08:48
Show Gist options
  • Save rubensousa/44c51b845678c7a0fefbbf238d16ed11 to your computer and use it in GitHub Desktop.
Save rubensousa/44c51b845678c7a0fefbbf238d16ed11 to your computer and use it in GitHub Desktop.
A helper plugin for publishing Android libraries to GitHub Packages
/*
* Copyright 2013 Chris Banes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* BSD 3-Clause License
*
* Copyright (c) 2019, Sky Italia
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright 2020 Rúben Sousa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Adapted from: https://github.com/chrisbanes/gradle-mvn-push
* and: https://github.com/sky-uk/gradle-maven-plugin/blob/master/artifact-pom-manager.gradle
*
* This script requires the following variables:
*
* --- LIBRARY INFORMATION ---
*
* LIBRARY_VERSION=1.0.0-rc01
* LIBRARY_GROUP=com.library.package
* LIBRARY_ARTIFACT=library
* LIBRARY_PUBLISH_SOURCES=true // default is true
* LIBRARY_PUBLISH_DOCS=true // default is true
*
* Gradle path for installation will be: com.library.package:library
*
* --- GITHUB PACKAGES INFORMATION ---
*
* GITHUB_OWNER=githubuser
* GITHUB_REPOSITORY=Library
*
* Maven repository url will be: https://maven.pkg.github.com/githubuser/Library
*
* --- POM INFORMATION ---
*
* POM_PACKAGING=aar
* POM_NAME=Library
* POM_DESCRIPTION=A sample library that's published to github packages
* POM_URL=https://github.com/githubuser/Library
* POM_SCM_URL=https://github.com/githubuser/githubuser
* POM_SCM_CONNECTION=scm:git@github.com:githubuser/githubuser.git
* POM_SCM_DEV_CONNECTION=scm:git@github.com:githubuser/githubuser.git
* POM_DEVELOPER_ID=githubuser
* POM_DEVELOPER_NAME=Github User
* POM_LICENCE_NAME=The Apache Software License, Version 2.0
* POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
* POM_LICENCE_DIST=repo
*/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.10.1"
}
}
apply plugin: 'maven-publish'
def isDokkaAvailable = project.tasks.findByName('dokka')
// Check if the project has dokka setup before trying to apply the plugin
// If dokka isn't setup, javadocs won't be generated for kotlin files
if (isDokkaAvailable) {
apply plugin: 'org.jetbrains.dokka'
}
afterEvaluate { project ->
def publishSources = shouldPublishSources()
def publishDocs = shouldPublishDocs()
if (publishDocs) {
// If dokka is setup, generate the javadocs for kotlin files
if (isDokkaAvailable) {
task dokkaJavadoc(type: dokka.class) {
outputFormat = 'javadoc'
outputDirectory = "$buildDir/javadoc"
}
task dokkaJavadocsJar(type: Jar, dependsOn: dokkaJavadoc) {
getArchiveClassifier().set('javadoc')
from dokkaJavadoc.outputDirectory
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.source
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
excludes = ['**/*.kt']
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
getArchiveClassifier().set('javadoc')
from androidJavadocs.destinationDir
}
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
if (JavaVersion.current().isJava9Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addBooleanOption('html5', true)
}
}
}
android.libraryVariants.all { variant ->
tasks.androidJavadocs.doFirst {
classpath += files(variant.javaCompileProvider.get().classpath.files
.join(File.pathSeparator))
}
}
if (isDokkaAvailable) {
artifacts {
archives dokkaJavadocsJar
}
} else {
artifacts {
archives androidJavadocsJar
}
}
}
if (publishSources) {
task androidSourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.source
}
artifacts {
archives androidSourcesJar
}
}
publishing {
repositories {
maven {
url = getRepositoryUrl()
credentials {
username = GITHUB_USER
password = GITHUB_TOKEN
}
}
}
publications {
release(MavenPublication) {
from components.release
groupId LIBRARY_GROUP
version LIBRARY_VERSION
artifactId LIBRARY_ARTIFACT
if (publishSources) {
artifact androidSourcesJar
}
if (publishDocs) {
if (isDokkaAvailable) {
artifact dokkaJavadocsJar
} else {
artifact androidJavadocsJar
}
}
configurePom(pom)
}
}
}
}
def shouldPublishSources() {
if (hasProperty("LIBRARY_PUBLISH_SOURCES")) {
return LIBRARY_PUBLISH_SOURCES == "true"
} else {
return true
}
}
def shouldPublishDocs() {
if (hasProperty("LIBRARY_PUBLISH_DOCS")) {
return LIBRARY_PUBLISH_DOCS == "true"
} else {
return true
}
}
def getRepositoryUrl() {
return "https://maven.pkg.github.com/" + GITHUB_OWNER + "/" + GITHUB_REPOSITORY
}
def configurePom(pom) {
pom.name = POM_NAME
pom.packaging = POM_PACKAGING
pom.description = POM_DESCRIPTION
pom.url = POM_URL
pom.scm {
url = POM_SCM_URL
connection = POM_SCM_CONNECTION
developerConnection = POM_SCM_DEV_CONNECTION
}
if (hasProperty('POM_LICENSE_NAME')) {
pom.licenses {
license {
name = POM_LICENCE_NAME
url = POM_LICENCE_URL
distribution = POM_LICENCE_DIST
}
}
}
pom.developers {
developer {
id = POM_DEVELOPER_ID
name = POM_DEVELOPER_NAME
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment