Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maven publishing configuration #46

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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"
141 changes: 111 additions & 30 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.+"
}
Expand Down Expand Up @@ -95,7 +98,6 @@ dependencies {




////////////////////////
// Task Configuration //
////////////////////////
Expand Down Expand Up @@ -124,35 +126,6 @@ tasks {



////////////////
// Publishing //
////////////////

publishing.publications {
register<MavenPublication>("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 //
/////////////
Expand Down Expand Up @@ -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:[email protected]: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("[email protected]")
}
}
}
}


publishing.publications {
register<MavenPublication>("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<Task>("rebuild") {
group = "build"
dependsOn(clean, build)
}

// Only enable publishing task for properly configured projects
val publishingTasks = tasks.withType<PublishToMavenRepository> {
enabled = ossrhUser?.isNotEmpty() == true || name.contains("local", true)
mustRunAfter(rebuild)
dependsOn(rebuild)
}
2 changes: 2 additions & 0 deletions buildscript-gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down