From a9eac90c380497bcaae67f9bb6304186526f3a84 Mon Sep 17 00:00:00 2001 From: "Artem Zinnatullin :slowpoke" Date: Thu, 13 Apr 2017 22:16:13 +0300 Subject: [PATCH] Setup automatic publishing to Bintray (jcenter). (#9) --- build.gradle | 35 +++++++++++++++++- ci/build.sh | 27 +++++++++----- swarmer/build.gradle | 88 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 139 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 774f6bd..79adf58 100644 --- a/build.gradle +++ b/build.gradle @@ -11,8 +11,9 @@ buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin" - classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0' + classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0' classpath "org.junit.platform:junit-platform-gradle-plugin:$versions.junitPlatform" + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' } } @@ -23,3 +24,35 @@ allprojects { apply plugin: 'com.github.ben-manes.versions' } + +def gitTag() { + def tag = 'git tag --list --points-at HEAD'.execute((List) null, rootProject.projectDir).text.trim() + + if (tag.split(System.lineSeparator()).length > 1) { + throw new IllegalStateException("gitTag is accessed but commit has multiple tags: $tag") + } + + return tag +} + +def projectVersion() { + def tag = gitTag() + + if (tag.startsWith('v')) { + return tag.substring(1) + } else if (tag.isEmpty()) { + return "development" + } + + return tag +} + +def validateTagAndVersion() { + if (gitTag().isEmpty()) { + throw new IllegalStateException('Publishing is not allowed because current commit has no tag') + } + + if (projectVersion().isEmpty()) { + throw new IllegalStateException('Publishing is not allowed because current projectVersion is empty') + } +} diff --git a/ci/build.sh b/ci/build.sh index 2d82724..401cddc 100644 --- a/ci/build.sh +++ b/ci/build.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -xe +set -e # You can run it from any directory. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" @@ -11,23 +11,32 @@ pushd "$PROJECT_DIR" USER_ID=`id -u $USER` BUILD_COMMAND="set -xe && " -BUILD_COMMAND+="apt-get update && apt-get -y install sudo && " -BUILD_COMMAND+="groupadd --gid $USER_ID build_user && " -BUILD_COMMAND+="useradd --shell /bin/bash --uid $USER_ID --gid $USER_ID --create-home build_user && " -BUILD_COMMAND+="sudo --set-home --preserve-env -u build_user " +BUILD_COMMAND+="apt-get update && apt-get --assume-yes install git && " + +if [ "$USER_ID" == "0" ]; then + echo "Warning: running as r00t." +else + BUILD_COMMAND+="apt-get --assume-yes install sudo && " + BUILD_COMMAND+="groupadd --gid $USER_ID build_user && " + BUILD_COMMAND+="useradd --shell /bin/bash --uid $USER_ID --gid $USER_ID --create-home build_user && " + BUILD_COMMAND+="sudo --set-home --preserve-env -u build_user " +fi BUILD_COMMAND+="/opt/project/gradlew " -BUILD_COMMAND+="--no-daemon --info " +BUILD_COMMAND+="--no-daemon --info --stacktrace " BUILD_COMMAND+="clean build " -if [ "$PUBLISH" = "true" ]; then - BUILD_COMMAND+="artifactoryPublish " +if [ "$PUBLISH" == "true" ]; then + BUILD_COMMAND+="bintrayUpload " fi BUILD_COMMAND+="--project-dir /opt/project" docker run \ --v `"pwd"`:/opt/project \ +--env BINTRAY_USER="$BINTRAY_USER" \ +--env BINTRAY_API_KEY="$BINTRAY_API_KEY" \ +--env BINTRAY_GPG_PASSPHRASE="$BINTRAY_GPG_PASSPHRASE" \ +--volume `"pwd"`:/opt/project \ openjdk:8u121-jdk \ bash -c "$BUILD_COMMAND" diff --git a/swarmer/build.gradle b/swarmer/build.gradle index 6e32b46..bf405d0 100644 --- a/swarmer/build.gradle +++ b/swarmer/build.gradle @@ -1,6 +1,9 @@ apply plugin: 'kotlin' apply plugin: 'application' apply plugin: 'org.junit.platform.gradle.plugin' +apply plugin: 'maven' +apply plugin: 'maven-publish' +apply plugin: 'com.jfrog.bintray' mainClassName = 'com.gojuno.swarmer.MainKt' @@ -19,7 +22,7 @@ dependencies { } jar { - // Build "fat" jar. + // Build jar with dependencies. from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) { exclude 'META-INF/*.SF' exclude 'META-INF/*.DSA' @@ -40,3 +43,86 @@ junitPlatform { } } } + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +task validatePublishing { + doLast { + validateTagAndVersion() + } +} + +bintrayUpload.dependsOn validatePublishing + +def pomConfig = { + licenses { + license { + name 'The Apache Software License, Version 2.0' + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' + distribution 'repo' + } + } + developers { + developer { + id 'gojuno' + name 'Juno Inc.' + email 'opensource@gojuno.com' + } + } +} + +publishing { + publications { + SwarmerPublication(MavenPublication) { + from components.java + + artifact sourcesJar + artifact javadocJar + + groupId 'com.gojuno.swarmer' + artifactId 'swarmer' + version projectVersion() + + pom.withXml { + def root = asNode() + root.appendNode('description', 'Tool to create and start multiple Android Emulators in parallel.') + root.appendNode('name', 'Swarmer') + root.appendNode('url', 'https://github.com/gojuno/swarmer') + root.children().last() + pomConfig + } + } + } +} + +bintray { + user = System.getenv('BINTRAY_USER') + key = System.getenv('BINTRAY_API_KEY') + publish = true + + pkg { + repo = 'maven' + name = 'swarmer' + licenses = ['Apache-2.0'] + vcsUrl = 'https://github.com/gojuno/swarmer.git' + issueTrackerUrl = 'https://github.com/gojuno/swarmer/issues' + publications = ['SwarmerPublication'] + + version { + name = projectVersion() + vcsTag = gitTag() + + gpg { + sign = true + passphrase = System.getenv('BINTRAY_GPG_PASSPHRASE') + } + } + } +}