Skip to content

Instantly share code, notes, and snippets.

@rubensousa
Last active June 5, 2022 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubensousa/fe14c48d0597b4189ab4c6e4be173924 to your computer and use it in GitHub Desktop.
Save rubensousa/fe14c48d0597b4189ab4c6e4be173924 to your computer and use it in GitHub Desktop.
Publish to maven central
/*
* 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
*
* --- 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 {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.6.10"
}
}
apply plugin: 'maven-publish'
apply plugin: 'signing'
def isDokkaAvailable = project.tasks.findByName('dokkaJavadoc')
// 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'
}
ext["signing.keyId"] = getSecretKeyId()
ext["signing.password"] = getSecretKeyPassword()
ext["signing.secretKeyRingFile"] = getSecretKeyRingFilePath()
String getSecretKeyPassword() {
if (hasProperty("ossrhSecretKeyPassword")) {
return ossrhSecretKeyPassword
} else {
return ""
}
}
String getSecretKeyId() {
if (hasProperty("ossrhSecretKeyId")) {
return ossrhSecretKeyId
} else {
return ""
}
}
String getSecretKeyRingFilePath() {
if (hasProperty("ossrhSecretKeyRingFile")) {
return ossrhSecretKeyRingFile
} else {
return ""
}
}
String getSonatypeUsername() {
if (hasProperty("ossrhUsername")) {
return ossrhUsername
} else {
return ""
}
}
String getSonatypePassword() {
if (hasProperty("ossrhPassword")) {
return ossrhPassword
} else {
return ""
}
}
afterEvaluate { project ->
def publishSources = shouldPublishSources()
def publishDocs = shouldPublishDocs()
if (publishDocs) {
// If dokka is setup, generate the javadocs for kotlin files
if (isDokkaAvailable) {
tasks.dokkaJavadoc.configure {
outputDirectory.set(file("$buildDir/javadoc"))
}
}
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')
if (isDokkaAvailable) {
from dokkaJavadoc.outputDirectory
} else {
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))
}
}
artifacts {
archives androidJavadocsJar
}
}
if (publishSources) {
task androidSourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.source
}
artifacts {
archives androidSourcesJar
}
}
publishing {
publications {
release(MavenPublication) {
from components.release
groupId LIBRARY_GROUP
version LIBRARY_VERSION
artifactId LIBRARY_ARTIFACT
if (publishSources) {
artifact androidSourcesJar
}
if (publishDocs) {
artifact androidJavadocsJar
}
configurePom(pom)
}
}
repositories {
maven {
name = "sonatype"
url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username getSonatypeUsername()
password getSonatypePassword()
}
}
}
}
}
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 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
}
pom.licenses {
license {
name = "The Apache Software License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
pom.developers {
developer {
id = POM_DEVELOPER_ID
name = POM_DEVELOPER_NAME
}
}
}
group LIBRARY_GROUP
version LIBRARY_VERSION
signing {
sign publishing.publications
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment