Skip to content

Commit

Permalink
chore: publish the gradle module metadata
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jnizet committed Dec 30, 2020
1 parent 813cba5 commit 764125f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.gradle
/build/
build/
!gradle/wrapper/gradle-wrapper.jar

### STS ###
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -59,7 +59,7 @@ Add this to your dependencies:
<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
```
Expand Down
67 changes: 40 additions & 27 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.ninjasquad.gradle.MavenSyncTask

plugins {
val kotlinVersion = "1.4.10"

`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 {
Expand All @@ -36,6 +37,11 @@ val sharedManifest = Action<Manifest> {
)
}

val bintrayUser = "ninjasquad"
val bintrayRepo = "maven"
val bintrayPackage = project.name
val bintrayKey = project.findProperty("bintray.key")?.toString() ?: ""

tasks {
withType(KotlinCompile::class.java) {
kotlinOptions {
Expand All @@ -51,6 +57,27 @@ tasks {
withType<Jar> {
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 {
Expand Down Expand Up @@ -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"])
}
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.squareup.okhttp3:okhttp:4.8.1")
implementation(gradleApi())
}
66 changes: 66 additions & 0 deletions buildSrc/src/main/kotlin/com/ninjasquad/gradle/MavenSyncTask.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 764125f

Please sign in to comment.