From 764125f03bd77103e225aa5343cbdf79b91dfa2b Mon Sep 17 00:00:00 2001 From: jnizet Date: Wed, 30 Dec 2020 11:06:25 +0100 Subject: [PATCH] chore: publish the gradle module metadata - avoid using the gradle bintray plugin, which doesn't upload the module metadata - sign locally rather than with bintray - publish the signed artefacts using the native maven-publish plugin - use a custom task that uses the bintray API to sync with Maven Central fix #56 --- .gitignore | 2 +- README.md | 4 +- build.gradle.kts | 67 +++++++++++-------- buildSrc/build.gradle.kts | 12 ++++ .../com/ninjasquad/gradle/MavenSyncTask.kt | 66 ++++++++++++++++++ 5 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 buildSrc/build.gradle.kts create mode 100644 buildSrc/src/main/kotlin/com/ninjasquad/gradle/MavenSyncTask.kt diff --git a/.gitignore b/.gitignore index 9fb1382..acdc2e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .gradle -/build/ +build/ !gradle/wrapper/gradle-wrapper.jar ### STS ### diff --git a/README.md b/README.md index a2750e9..7d73f17 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ class GreetingControllerTest { Add this to your dependencies: ```kotlin -testImplementation("com.ninja-squad:springmockk:3.0.0") +testImplementation("com.ninja-squad:springmockk:3.0.1") ``` If you want to make sure Mockito (and the standard `MockBean` and `SpyBean` annotations) is not used, you can also exclude the mockito dependency: @@ -59,7 +59,7 @@ Add this to your dependencies: com.ninja-squad springmockk - 3.0.0 + 3.0.1 test ``` diff --git a/build.gradle.kts b/build.gradle.kts index 56adea4..2415ead 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,5 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import com.ninjasquad.gradle.MavenSyncTask plugins { val kotlinVersion = "1.4.10" @@ -6,16 +7,16 @@ plugins { `java-library` kotlin("jvm") version kotlinVersion `maven-publish` + signing id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion id("org.jetbrains.kotlin.plugin.noarg") version kotlinVersion id("org.springframework.boot") version "2.4.0" apply false id("io.spring.dependency-management") version "1.0.10.RELEASE" id("org.jetbrains.dokka") version "0.10.1" - id("com.jfrog.bintray") version "1.8.5" } group = "com.ninja-squad" -version = "3.0.0" +version = "3.0.1" description = "MockBean and SpyBean, but for MockK instead of Mockito" java { @@ -36,6 +37,11 @@ val sharedManifest = Action { ) } +val bintrayUser = "ninjasquad" +val bintrayRepo = "maven" +val bintrayPackage = project.name +val bintrayKey = project.findProperty("bintray.key")?.toString() ?: "" + tasks { withType(KotlinCompile::class.java) { kotlinOptions { @@ -51,6 +57,27 @@ tasks { withType { manifest(sharedManifest) } + + register("syncToMavenCentral", MavenSyncTask::class) { + mustRunAfter("publishMavenPublicationToBintrayRepository") + group = "publishing" + description = "Syncs to Maven Central" + + sonatypeUsername = project.findProperty("sonatypeUsername")?.toString() ?: "" + sonatypePassword = project.findProperty("sonatypePassword")?.toString() ?: "" + bintrayUsername = bintrayUser + bintrayPassword = bintrayKey + bintrayRepoName = bintrayRepo + bintrayPackageName = bintrayPackage + } + + register("publishAndSyncToMavenCentral", MavenSyncTask::class) { + group = "publishing" + description = "Publishes to Bintray, then syncs to Maven Central" + + dependsOn("publishMavenPublicationToBintrayRepository") + dependsOn("syncToMavenCentral") + } } dependencyManagement { @@ -114,34 +141,20 @@ publishing { } repositories { maven { + name = "build" url = uri("$buildDir/repo") } - } -} - -bintray { - user = "ninjasquad" - key = project.findProperty("bintray.key") as String? - setPublications("maven") - publish = true - pkg = PackageConfig().apply { - repo = "maven" - name = project.name - desc = project.description - websiteUrl = "https://github.com/Ninja-Squad/springmockk" - issueTrackerUrl = "https://github.com/Ninja-Squad/springmockk/issues" - vcsUrl = "https://github.com/Ninja-Squad/springmockk" - setLicenses("Apache-2.0") - version = VersionConfig().apply { - gpg = GpgConfig().apply { - sign = true - passphrase = project.findProperty("signing.password") as String? - } - mavenCentralSync = MavenCentralSyncConfig().apply { - sync = true - user = project.findProperty("sonatypeUsername") as String? - password = project.findProperty("sonatypePassword") as String? + maven { + name = "bintray" + url = uri("https://api.bintray.com/maven/$bintrayUser/$bintrayRepo/$bintrayPackage/;publish=1") + credentials { + username = bintrayUser + password = bintrayKey } } } } + +signing { + sign(publishing.publications["maven"]) +} diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 0000000..eec594b --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + `kotlin-dsl` +} + +repositories { + mavenCentral() +} + +dependencies { + implementation("com.squareup.okhttp3:okhttp:4.8.1") + implementation(gradleApi()) +} diff --git a/buildSrc/src/main/kotlin/com/ninjasquad/gradle/MavenSyncTask.kt b/buildSrc/src/main/kotlin/com/ninjasquad/gradle/MavenSyncTask.kt new file mode 100644 index 0000000..ab1d7b5 --- /dev/null +++ b/buildSrc/src/main/kotlin/com/ninjasquad/gradle/MavenSyncTask.kt @@ -0,0 +1,66 @@ +package com.ninjasquad.gradle + +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Credentials +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.TaskAction +import java.io.IOException +import java.time.Duration + +open class MavenSyncTask : DefaultTask() { + @Input + lateinit var sonatypeUsername: String + + @Input + lateinit var sonatypePassword: String + + @Input + lateinit var bintrayUsername: String + + @Input + lateinit var bintrayPassword: String + + @Input + lateinit var bintrayRepoName: String + + @Input + lateinit var bintrayPackageName: String + + @Input + var version = project.version.toString() + + @TaskAction + fun sync() { + println("synchronizing with Maven central...") + val client = OkHttpClient.Builder().readTimeout(Duration.ofMinutes(5L)).build() + val jsonBody = + // language=json + """ + { + "username": "$sonatypeUsername", + "password": "$sonatypePassword", + "close": "1" + } + """.trimIndent() + val request: Request = Request.Builder() + .header("Authorization", Credentials.basic(bintrayUsername, bintrayPassword)) + .url("https://api.bintray.com/maven_central_sync/$bintrayUsername/$bintrayRepoName/$bintrayPackageName/versions/$version") + .post(jsonBody.toRequestBody("application/json".toMediaType())) + .build() + try { + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) { + throw GradleException("Maven sync failed with status " + response.code + " and body " + response.body?.string()) + } + println(response.body?.string()) + } + } catch (e: IOException) { + throw GradleException("Maven sync failed", e) + } + } +}