From e85070fbdf20233111442d5ef3a1048de1893919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Mon, 1 Jan 2024 18:27:37 +0100 Subject: [PATCH] Add maven publishing configuration --- .github/workflows/publish.yml | 32 ++++++++ build.gradle.kts | 141 ++++++++++++++++++++++++++-------- buildscript-gradle.lockfile | 2 + 3 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..5f258cf --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,32 @@ +name: publish + +on: + workflow_dispatch: + +concurrency: + group: "publish" + cancel-in-progress: true + +jobs: + publish: + environment: MavenRelease + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: 17 + distribution: temurin + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: Publish + uses: gradle/gradle-build-action@v2 + env: + ORG_GRADLE_PROJECT_ossrhUser: ${{ secrets.OSSRH_USER }} + ORG_GRADLE_PROJECT_ossrhPassword: ${{ secrets.OSSRH_PASSWORD }} + ORG_GRADLE_PROJECT_stagingProfile: ${{ secrets.STAGING_PROFILE_ID }} + ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_KEY }} + ORG_GRADLE_PROJECT_signingKeyId: ${{ secrets.GPG_KEYID }} + with: + arguments: "publishToSonatype closeAndReleaseStagingRepository" \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 3a00c26..a7b8762 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,10 @@ buildscript { plugins { `maven-publish` + signing + kotlin("jvm") version "1.+" + id("io.github.gradle-nexus.publish-plugin") version "1.+" id("io.gitlab.arturbosch.detekt") version "1.+" id("org.jetbrains.dokka") version "1.+" } @@ -95,7 +98,6 @@ dependencies { - //////////////////////// // Task Configuration // //////////////////////// @@ -124,35 +126,6 @@ tasks { -//////////////// -// Publishing // -//////////////// - -publishing.publications { - register("Release") { - from(components["java"]) - groupId = project.group as String - artifactId = project.name - version = project.version as String - - artifact(javadocJar) - artifact(sourcesJar) - - // Uses the resolved version instead of the 1.+ wildcards - // See https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:resolved_dependencies - versionMapping { - usage("java-api") { - fromResolutionOf("runtimeClasspath") - } - usage("java-runtime") { - fromResolutionResult() - } - } - } -} - - - ///////////// // Linting // ///////////// @@ -193,3 +166,111 @@ tasks.getByName("dokkaHtml", DokkaTask::class) { } } } + + + +//////////////// +// Publishing // +//////////////// + + +fun getProjectProperty(name: String) = project.properties[name] as? String + + +// Generate pom file for maven central + +fun generatePom(): MavenPom.() -> Unit { + return { + packaging = "jar" + name.set(project.name) + description.set("Collection of useful Kotlin extensions for JDA") + url.set("https://github.com/MinnDevelopment/jda-ktx") + scm { + url.set("https://github.com/MinnDevelopment/jda-ktx") + connection.set("scm:git:git://github.com/MinnDevelopment/jda-ktx") + developerConnection.set("scm:git:ssh:git@github.com:MinnDevelopment/jda-ktx") + } + licenses { + license { + name.set("The Apache Software License, Version 2.0") + url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + distribution.set("repo") + } + } + developers { + developer { + id.set("Minn") + name.set("Florian Spieß") + email.set("business@minn.dev") + } + } + } +} + + +publishing.publications { + register("Release") { + from(components["java"]) + artifactId = project.name + groupId = project.group as String + version = project.version as String + + artifact(javadocJar) + artifact(sourcesJar) + + pom.apply(generatePom()) + + // Uses the resolved version instead of the 1.+ wildcards + // See https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:resolved_dependencies + versionMapping { + usage("java-api") { + fromResolutionOf("runtimeClasspath") + } + usage("java-runtime") { + fromResolutionResult() + } + } + } +} + +val signingKey: String? by project + +if (signingKey != null) { + signing { + useInMemoryPgpKeys(signingKey, null) + sign(*publishing.publications.toTypedArray()) + } +} + +val ossrhUser: String? by project +val ossrhPassword: String? by project +val stagingProfile: String? by project + +val enablePublishing = ossrhUser != null && ossrhPassword != null && stagingProfile != null + +if (enablePublishing) { + nexusPublishing { + repositories.sonatype { + username.set(ossrhUser) + password.set(ossrhPassword) + stagingProfileId.set(stagingProfile) + } + } +} + +val build by tasks +val clean by tasks + +build.mustRunAfter(clean) + +val rebuild = tasks.register("rebuild") { + group = "build" + dependsOn(clean, build) +} + +// Only enable publishing task for properly configured projects +val publishingTasks = tasks.withType { + enabled = ossrhUser?.isNotEmpty() == true || name.contains("local", true) + mustRunAfter(rebuild) + dependsOn(rebuild) +} diff --git a/buildscript-gradle.lockfile b/buildscript-gradle.lockfile index ae4a3ff..8d7fb81 100644 --- a/buildscript-gradle.lockfile +++ b/buildscript-gradle.lockfile @@ -9,6 +9,8 @@ com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.12.7=classpath com.fasterxml.jackson.module:jackson-module-kotlin:2.12.7=classpath com.fasterxml.jackson:jackson-bom:2.12.7=classpath com.fasterxml.woodstox:woodstox-core:6.2.4=classpath +io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin:1.1.0=classpath +io.github.gradle-nexus:publish-plugin:1.1.0=classpath io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.4=classpath io.gitlab.arturbosch.detekt:io.gitlab.arturbosch.detekt.gradle.plugin:1.23.4=classpath jakarta.activation:jakarta.activation-api:1.2.1=classpath