Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Reworkiing publish script to support maven central
Browse files Browse the repository at this point in the history
  • Loading branch information
tunjid committed Apr 16, 2021
1 parent aaf413e commit ff6914a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 128 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/github_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,5 @@ jobs:
java-version: 1.8
- name: Github Maven Publish
env:
BINTRAY_USER: ${{ secrets.bintray_user }}
BINTRAY_API_KEY: ${{ secrets.bintray_key }}
PUBLISH_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_USER_NAME: ${{ secrets.GITHUB_ACTOR }}
PUBLISH_VERSION_SUFFIX: ${{ secrets.PUBLISH_VERSION_SUFFIX }}
PUBLISH_URL: ${{ secrets.PUBLISH_URL }}
PUBLISH_REPO_NAME: GithubMaven
run: bash ./gradlew incrementalPublish -PversionSuffix="$PUBLISH_VERSION_SUFFIX" -PpublishRepoName="$PUBLISH_REPO_NAME" -PpublishUrl="$PUBLISH_URL" -PpublishUserName="$PUBLISH_USER_NAME" -PpublishPassword="$PUBLISH_PASSWORD" -PbintrayUser="$BINTRAY_USER" -PbintrayApiKey="$BINTRAY_API_KEY" --stacktrace
PUBLISH_INFO_JSON: ${{ secrets.publish_info_json }}
run: bash ./gradlew incrementalPublish -PpublishInfoJson="$PUBLISH_INFO_JSON" --stacktrace
60 changes: 29 additions & 31 deletions androidLib.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply from: "${project.rootDir}/version.gradle"

android {
Expand Down Expand Up @@ -45,28 +45,6 @@ android {
}
}

bintray {
user = parent.ext.bintrayUser
key = parent.ext.bintrayApiKey

// jFrog plugin must be declared for this line to work
configurations = ['archives']
publications = ['lib']
// Package info for BinTray
pkg {
repo = 'maven'
name = 'android-extensions'
userOrg = user
licenses = ['MIT']
vcsUrl = "${parent.ext.publishUrl}.git"
version {
name = project.version
desc = 'An Android library to bootstrap an Android application.'
vcsTag = 'vx1.0.0-alpha01'
}
}
}

afterEvaluate {
publishing {
publications {
Expand All @@ -83,18 +61,34 @@ afterEvaluate {
classifier = 'sources'
extension = 'jar'
}

pom {
name = project.getName()
description = "${name} Android extensions"
url = 'https://github.com/tunjid/Android-Extensions'
licenses {
license {
name = 'MIT License'
url = 'https://github.com/tunjid/tunjid/Android-Extensions/develop/license.txt'
}
}
developers {
developer {
id = 'tunjid'
name = 'Adetunji Dahunsi'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:github.com/tunjid/Android-Extensions.git'
developerConnection = 'scm:git:ssh://github.com/tunjid/Android-Extensions.git'
url = 'https://github.com/tunjid/Android-Extensions/tree/main'
}
}
}
}
}
repositories {
maven {
name = parent.ext.publishRepoName
url = parent.ext.publishUrl
credentials {
username = parent.ext.publishUserName
password = parent.ext.publishPassword
}
}
parent.ext.publishRepositories.each { repo ->
def props = (repo as ConfigObject).toProperties()
maven {
Expand All @@ -108,6 +102,10 @@ afterEvaluate {
}
}
}

signing {
sign publishing.publications
}
}

// Gradle task to generate sources after building a release aar
Expand Down
102 changes: 44 additions & 58 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ buildscript {

// Artifactory publishing params
libProps = new Properties()
bintrayProps = new Properties()
signingProps = new Properties()
localProps = new Properties()

libProps.load(new FileInputStream(file("libraryVersion.properties")))
Expand All @@ -67,42 +67,30 @@ buildscript {
// "credentials": "username": "user", "password": "hi"
// }]

publishRepositories = []
def localPropsFile = file("local.properties")

if (localPropsFile.exists()) {
localProps.load(new FileInputStream(localPropsFile))

def slurper = new JsonSlurper()
def repos = localProps['repositories']
if (repos != null) publishRepositories = slurper.parseText(repos)
}

def bintrayFile = file("bintray.properties")

if (bintrayFile.exists()) {
bintrayProps.load(new FileInputStream(bintrayFile))
}

def pullProperty = { String name, Properties prop ->
return project.findProperty(name) ?: prop[name] ?: ""
}

publishUrl = pullProperty('publishUrl', localProps)
publishRepoName = pullProperty('publishRepoName', localProps)
publishUserName = pullProperty('publishUserName', localProps)
publishPassword = pullProperty('publishPassword', localProps)
versionSuffix = pullProperty('versionSuffix', localProps)
def publishInfoFile = file("publishInfo.json")
def publishInfoProperty = project.findProperty('publishInfoJson')
publishInfo = publishInfoFile.exists()
? new JsonSlurper().parse(publishInfoFile)
: reposProperty
? new JsonSlurper().parseText(publishInfoProperty)
: [:]

versionSuffix = publishInfo['versionSuffix'] ?: ''
publishRepositories = publishInfo['repositories'] ?: []

bintrayUser = pullProperty('bintrayUser', bintrayProps)
bintrayApiKey = pullProperty('bintrayApiKey', bintrayProps)
signingKeyId = pullProperty('signingKeyId', signingProps)
signingPassword = pullProperty('signingPassword', signingProps)
}
}

// Plugin used to upload authenticated files to BinTray through Gradle
plugins {
id "com.jfrog.bintray" version "1.8.5"
}
//plugins {
// id 'signing'
//}

allprojects {
repositories {
Expand All @@ -118,20 +106,18 @@ task clean(type: Delete) {

task incrementalPublish {
def libProps = project.ext.libProps
def artifactExists = { String mavenRepo, String name, String version, boolean addAuth ->
def artifactExists = { params ->
// githubUrl, moduleName, version, true
def group = libProps['groupId'].replace(".", "/")
def url = "${mavenRepo}/${group}/${name}/${version}/${name}-${version}.pom"
def url = "${params['url']}/${group}/${params['moduleName']}/${params['version']}/${params['moduleName']}-${params['version']}.pom"

try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection()
if (addAuth) {
String encoded = Base64.getEncoder().encodeToString(("${project.ext.publishUserName}:${project.ext.publishPassword}").getBytes("UTF-8"))
connection.setRequestProperty("Authorization", "Basic " + encoded)
}
connection.setRequestProperty("Authorization", params['auth'])
connection.connect()

def exists = connection.getResponseCode() == HttpURLConnection.HTTP_OK
println "pom for ${name} exists on ${url}: ${exists}"
println "pom for ${params['moduleName']} exists on $url}: ${exists}"

return exists
}
Expand All @@ -145,29 +131,29 @@ task incrementalPublish {
libProps.stringPropertyNames()
.findAll { name -> name.contains('version') }
.each { name ->
def moduleName = name - '_version'
def version = libProps[name] + project.ext.versionSuffix
def githubUrl = "${project.ext.publishUrl}"
def jCenterUrl = "https://jcenter.bintray.com"

def existsOnGitHub = artifactExists(githubUrl, moduleName, version, true)
def existsOnJcenter = artifactExists(jCenterUrl, moduleName, version, false)
def hasJcenterCreds = project.ext.bintrayUser && !project.ext.bintrayUser.empty && project.ext.bintrayApiKey && !project.ext.bintrayApiKey.empty

def publishGithub = existsOnGitHub != null && !existsOnGitHub
def publishJcenter = existsOnJcenter != null && !existsOnJcenter && hasJcenterCreds

if (publishJcenter || publishJcenter) {
dependsOn ":${moduleName}:assemble"
}

if (publishGithub) {
dependsOn ":${moduleName}:publishLibPublicationTo${project.ext.publishRepoName}Repository"
}

if (publishJcenter) {
println "Adding task ${moduleName} to bintray publish"
dependsOn ":${moduleName}:bintrayUpload"
project.ext.publishRepositories.each { repo ->
def moduleName = name - '_version'
def versionSuffix = repo["versionSuffix"] ?: ""

def username = repo['credentials']['username']
def password = repo['credentials']['password']
def encoded = Base64.getEncoder().encodeToString(("${username}:${password}").getBytes("UTF-8"))
def auth = "Basic ${encoded}"

def params = [
url : repo['url'],
moduleName: moduleName,
version : libProps[name] + (versionSuffix.isEmpty() ? "" : "-${versionSuffix}"),
auth : auth
]
def existsOnGitHub = artifactExists(params)
def publishGithub = existsOnGitHub != null && !existsOnGitHub

if (publishGithub) {
println "Adding task ${moduleName} to publish on ${repo['name']}"
dependsOn ":${moduleName}:assemble"
dependsOn ":${moduleName}:publishLibPublicationTo${repo['name']}Repository"
}
}
}
}
Expand Down
55 changes: 24 additions & 31 deletions kotlinLib.gradle
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
apply plugin: 'kotlin'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply from: "${project.rootDir}/version.gradle"

bintray {
user = parent.ext.bintrayUser
key = parent.ext.bintrayApiKey

// jFrog plugin must be declared for this line to work
configurations = ['archives']
publications = ['lib']
// Package info for BinTray
pkg {
repo = 'maven'
name = 'android-extensions'
userOrg = user
licenses = ['MIT']
vcsUrl = 'https://github.com/tunjid/android-bootstrap.git'
version {
name = project.version
desc = 'An suite of libraries to bootstrap an Android application.'
vcsTag = 'vx1.0.0-alpha01'
}
}
}

publishing {
publications {
lib(MavenPublication) {
Expand All @@ -38,18 +15,34 @@ publishing {
afterEvaluate {
artifact sourcesJar
artifact javadocJar

pom {
name = project.getName()
description = "${name} Android extensions"
url = 'https://github.com/tunjid/Android-Extensions'
licenses {
license {
name = 'MIT License'
url = 'https://github.com/tunjid/tunjid/Android-Extensions/develop/license.txt'
}
}
developers {
developer {
id = 'tunjid'
name = 'Adetunji Dahunsi'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:github.com/tunjid/Android-Extensions.git'
developerConnection = 'scm:git:ssh://github.com/tunjid/Android-Extensions.git'
url = 'https://github.com/tunjid/Android-Extensions/tree/main'
}
}
}
}
}
repositories {
maven {
name = parent.ext.publishRepoName
url = parent.ext.publishUrl
credentials {
username = parent.ext.publishUserName
password = parent.ext.publishPassword
}
}
parent.ext.publishRepositories.each { repo ->
def props = (repo as ConfigObject).toProperties()
maven {
Expand Down
21 changes: 21 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Adetunji Dahunsi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit ff6914a

Please sign in to comment.