Skip to content

Instantly share code, notes, and snippets.

@KaustubhPatange
Last active February 5, 2021 05:04
Show Gist options
  • Save KaustubhPatange/187fed923e89704f8afb1f2dc0ac5188 to your computer and use it in GitHub Desktop.
Save KaustubhPatange/187fed923e89704f8afb1f2dc0ac5188 to your computer and use it in GitHub Desktop.
A Gradle script to automatically publish Android library to maven or sonatype provided your central sync is enabled.
/*
* Copyright 2020 Kaustubh Patange
*
* 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
*
* https://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.
*/
/**
Automatic publishing on MavenCentral/Sonatype with nexus staging provided
your maven-central sync is enabled.
Setup Instructions
==================
- Add following properties to your local.properties file.
--------------------------------------------------------
signing.keyId= // gpg key id
signing.password= // gpg key password
signing.secretKeyRingFile= // gpg file path
ossrhUsername= // sonatype username
ossrhPassword= // sonatype password
--------------------------------------------------------
- Add following to top level build.gradle file.
----------------------------------------------
apply plugin: 'io.codearte.nexus-staging'
classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.22.0"
----------------------------------------------
- Save this file into your project's root folder as "publish.gradle".
- To include an Android library, add following snippet to your's
library build.gradle file, modify it according to your need.
----------------------------------------------------------
apply plugin: 'maven-publish' // add this at top
// place this all at the end of build.gradle
ext {
PUBLISH_GROUP_ID = 'io.github.kaustubhpatange'
PUBLISH_ARTIFACT_ID = 'hvlog'
PUBLISH_VERSION = '1.0'
PUBLISH_DESCRIPTION = 'A simple light-weight logger for Android'
PUBLISH_URL = 'https://github.com/KaustubhPatange/HVLog'
LICENSE_NAME = 'The Apache License, Version 2.0'
LICENSE_URL = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
DEVELOPER_ID = 'kp2016'
DEVELOPER_NAME = 'Kaustubh Patange'
DEVELOPER_EMAIL = 'developerkp16@gmail.com'
SCM_CONNECTION = 'scm:git:github.com/KaustubhPatange/HVLog.git'
SCM_DEVELOPER_CONNECTION = 'scm:git:ssh://github.com/KaustubhPatange/HVLog.git'
SCM_URL = 'https://github.com/KaustubhPatange/HVLog/tree/master'
NEXUS_STAGING_ID = '12345abcd' // dummy replace with your own.
}
apply from: "${rootProject.projectDir}/publish.gradle"
----------------------------------------------------------
- To publish library,
-------------------------------------------------------
./gradlew module-name:publishReleasePublicationToSonatypeRepository
./gradlew closeRepository
./gradlew releaseRepository
-------------------------------------------------------
*/
apply plugin: 'maven-publish'
apply plugin: 'signing'
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.javaCompileProvider.get().classpath
}
}
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
archiveClassifier.set('javadoc')
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
println "Found secret props file, loading props"
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
println "No props file, loading env vars"
ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
}
signing {
sign publishing.publications
}
nexusStaging {
packageGroup = PUBLISH_GROUP_ID
stagingProfileId = NEXUS_STAGING_ID
username = ossrhUsername
password = ossrhPassword
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
artifact androidSourcesJar
groupId = PUBLISH_GROUP_ID
artifactId = PUBLISH_ARTIFACT_ID
version = PUBLISH_VERSION
pom {
name = PUBLISH_ARTIFACT_ID
description = PUBLISH_DESCRIPTION
url = PUBLISH_URL
licenses {
license {
name = LICENSE_NAME
url = LICENSE_URL
}
}
developers {
developer {
id = DEVELOPER_ID
name = DEVELOPER_NAME
email = DEVELOPER_EMAIL
}
}
// Version control info, if you're using GitHub, follow the format as seen here
scm {
connection = SCM_CONNECTION
developerConnection = SCM_DEVELOPER_CONNECTION
url = SCM_URL
}
}
}
}
repositories {
// The repository to publish to, Sonatype/MavenCentral
maven {
// This is an arbitrary name, you may also use "mavencentral" or
// any other name that's descriptive for you
name = "sonatype"
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
// You only need this if you want to publish snapshots, otherwise just set the URL
// to the release repo directly
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
// The username and password we've fetched earlier
credentials {
username ossrhUsername
password ossrhPassword
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment