Skip to content

Commit b9c7c3e

Browse files
Add publishing to sonatype repo.
1 parent 9dc663d commit b9c7c3e

File tree

4 files changed

+117
-57
lines changed

4 files changed

+117
-57
lines changed
Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,31 @@
1-
# This workflow uses actions that are not certified by GitHub.
2-
# They are provided by a third-party and are governed by
3-
# separate terms of service, privacy policy, and support
4-
# documentation.
5-
# This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created
6-
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle
1+
#
2+
# Publishes the package to Sonatype - the Maven Central artifact broker
3+
#
74

8-
name: Gradle Package
5+
name: Publish
96

107
on:
118
release:
12-
types: [created]
9+
types: [ created ]
1310

1411
jobs:
15-
build:
16-
12+
publish:
1713
runs-on: ubuntu-latest
18-
permissions:
19-
contents: read
20-
packages: write
21-
2214
steps:
23-
- uses: actions/checkout@v4
24-
- name: Set up JDK 17
25-
uses: actions/setup-java@v4
26-
with:
27-
java-version: '17'
28-
distribution: 'temurin'
29-
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
30-
settings-path: ${{ github.workspace }} # location for the settings.xml file
31-
32-
- name: Build with Gradle
33-
uses: gradle/gradle-build-action@v3
34-
with:
35-
arguments: -Pversion=${{ github.event.release.tag_name }} build
36-
37-
# The USERNAME and TOKEN need to correspond to the credentials environment variables used in
38-
# the publishing section of your build.gradle
39-
- name: Publish to GitHub Packages
40-
uses: gradle/gradle-build-action@v3
41-
with:
42-
arguments: -Pversion=${{ github.event.release.tag_name }} publish
43-
env:
44-
USERNAME: ${{ github.actor }}
45-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
15+
- uses: actions/checkout@v4
16+
- name: Set up JDK
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: '17'
20+
distribution: 'temurin'
21+
- name: Set Release Version as an environment variable
22+
run: echo "PUBLISH_VERSION=$(echo ${{ github.event.release.tag_name }})" >> $GITHUB_ENV
23+
- name: Publish package
24+
uses: gradle/gradle-build-action@v3
25+
with:
26+
arguments: -Pversion=${{ env.PUBLISH_VERSION }} publishToSonatype closeAndReleaseSonatypeStagingRepository
27+
env:
28+
ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.SONATYPE_USERNAME }}
29+
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.SONATYPE_PASSWORD }}
30+
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_SIGNING_KEY }}
31+
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GPG_SIGNING_PASSWORD }}

build.gradle.kts

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ buildscript {
77

88
plugins {
99
alias(libs.plugins.kotlin.jvm)
10-
// `maven-publish`
10+
`java-library`
11+
`maven-publish`
12+
signing
13+
alias(libs.plugins.nexusPublishPlugin)
1114
}
1215

1316
val jvmTargetVersion: String by project
@@ -31,7 +34,6 @@ dependencies {
3134

3235
tasks {
3336
java {
34-
withSourcesJar()
3537
toolchain { languageVersion.set(JavaLanguageVersion.of(jvmTargetVersion)) }
3638
}
3739

@@ -40,20 +42,82 @@ tasks {
4042
}
4143
}
4244

43-
//publishing {
44-
// repositories {
45-
// maven {
46-
// name = "GitHubPackages"
47-
// url = uri("https://maven.pkg.github.com/target/native_memory_allocator")
48-
// credentials {
49-
// username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
50-
// password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
51-
// }
52-
// }
53-
// }
54-
// publications {
55-
// register<MavenPublication>("gpr") {
56-
// from(components["java"])
57-
// }
58-
// }
59-
//}
45+
java {
46+
withJavadocJar()
47+
withSourcesJar()
48+
}
49+
50+
publishing {
51+
repositories {
52+
maven {
53+
credentials(PasswordCredentials::class)
54+
name =
55+
"sonatype" // correlates with the environment variable set in the github action release.yml publish job
56+
val releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
57+
val snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
58+
setUrl(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
59+
}
60+
}
61+
62+
publications {
63+
val projectTitle: String by project
64+
val projectDescription: String by project
65+
val projectUrl: String by project
66+
val projectScm: String by project
67+
68+
create<MavenPublication>("mavenJava") {
69+
artifactId = rootProject.name
70+
from(components["java"])
71+
versionMapping {
72+
usage("java-api") {
73+
fromResolutionOf("runtimeClasspath")
74+
}
75+
usage("java-runtime") {
76+
fromResolutionResult()
77+
}
78+
}
79+
pom {
80+
name.set(projectTitle)
81+
description.set(projectDescription)
82+
url.set(projectUrl)
83+
licenses {
84+
license {
85+
name.set("Apache 2.0")
86+
url.set("http://www.apache.org/licenses/LICENSE-2.0")
87+
}
88+
}
89+
developers {
90+
developer {
91+
id.set("ossteam")
92+
name.set("OSS Office")
93+
email.set("[email protected]")
94+
}
95+
}
96+
scm {
97+
url.set(projectScm)
98+
}
99+
}
100+
}
101+
}
102+
103+
}
104+
105+
signing {
106+
val signingKey: String? by project
107+
val signingPassword: String? by project
108+
if (signingKey.isNullOrBlank() || signingPassword.isNullOrBlank()) {
109+
isRequired = false
110+
} else {
111+
useInMemoryPgpKeys(signingKey, signingPassword)
112+
sign(publishing.publications)
113+
}
114+
}
115+
116+
nexusPublishing {
117+
repositories {
118+
sonatype {
119+
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
120+
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
121+
}
122+
}
123+
}

gradle.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
jvmTargetVersion = 17
1+
jvmTargetVersion = 17
2+
3+
# Properties used by the publishing plugin
4+
projectTitle=Native Memory Allocator
5+
projectDescription=O(1) malloc for off-heap storage in Kotlin, and a NativeMemoryMap based on Caffeine.
6+
projectUrl=https://github.com/target/native_memory_allocator
7+
projectScm=https://github.com/target/native_memory_allocator

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ kotlin-logging = "7.0.7"
77
micrometer-core = "1.15.2"
88
objenesis = "3.4"
99

10+
# publishing dependencies
11+
nexusPublishPlugin = "2.0.0"
12+
1013
# examples dependencies
1114
flatbuffers = "25.2.10"
1215
kotlin-coroutines = "1.10.2"
@@ -50,5 +53,6 @@ testing = ["guava-testlib", "jupiter-api", "jupiter-engine", "jupiter-params", "
5053

5154
[plugins]
5255
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
56+
nexusPublishPlugin = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexusPublishPlugin" }
5357
shadow = { id = "com.gradleup.shadow", version.ref = "shadowjar" }
5458
jmh = { id = "me.champeau.jmh", version.ref = "jmh-gradle" }

0 commit comments

Comments
 (0)