用Gradle去发布第一个依赖库
背景
关于在看仓库时,发现一个叫作 Toon 的项目,十分有意思,于是想写一个 kotlin 的实现版本的。
实现
略。。。看仓库:https://github.com/ShiYioo/java-toon
发布
1. 创建 Sonatype 账号
访问地址:https://central.sonatype.com
注册过程略。
TIP
如果为 github 账号,可以直接用 github 登录,并会得到一个 GitHub 的命名空间,非常方便。
2. 生成用户访问令牌
访问地址:https://central.sonatype.com/usertoken
生成一个用户访问令牌,保存好这个令牌,后续会用到,记住 username 和 password。
3. 生成 GPG 密钥对
使用 GPG 生成一个密钥对,用于对发布的包进行签名。
安装 gpg
brew install gpg
生成密钥对
gpg --full-generate-key
之后他会问你几个问题:
| 问题 | 选择 |
|---|---|
| Please select what kind of key you want | (1) RSA and RSA(默认) |
| What keysize do you want? | 4096 |
| Key is valid for? | 0(永不过期) |
| Real name | 你的名字或昵称 |
| Email address | 你的邮箱 |
| Comment | 可以留空 |
DANGER
⚠️ 重要:最后,它会要求你输入一个密码 (Passphrase),请务必牢记这个密码,发布时需要用到。
查看密钥
gpg --list-secret-keys
输出类似如下:
pub rsa4096 2025-11-02 [SC]
3AxxxxxxCBF4xxxxxxxxxxxxxxxxxxxxx
uid ShiYi (null) <xxx@example.com>
sub rsa4096 2025-11-02 [E]
请复制这一整长串字符,然后只保留最后面的 16 个字符。
INFO
如果您的完整指纹是 3A61CBF40102030405060708DEADBEEFCAFE0123,那么您的密钥 ID 就是 DEADBEEFCAFE0123。
这个就是你的 signing.keyId,请把它记录下来。
上传到公钥服务器上
gpg --keyserver keyserver.ubuntu.com --send-keys [你的密钥ID]
更新 gradle.properties 文件
在你的用户目录下(例如 ~/.gradle/gradle.properties)创建或编辑 gradle.properties 文件,添加以下内容:
ossrhUsername=你的Sonatype用户名
ossrhPassword=你的Sonatype密码或Token
signing.keyId=你的密钥ID
signing.password=你在生成密钥对时设置的密码
4. 配置 Gradle
在 build.gradle.kts 中添加如下配置:
plugins {
kotlin("jvm") version "2.2.20"
`java-library`
`maven-publish`
signing
}
publishing {
publications.register<MavenPublication>("maven") {
groupId = "io.github.shiyioo"
artifactId = "java-toon"
version = project.version.toString()
from(components["java"])
pom {
name = "java-toon"
description = "A Kotlin library for toon development"
url = "https://github.com/ShiYioo/java-toon"
developers {
developer {
id = "shiyi"
name = "ShiYi"
email = "141152739+ShiYioo@users.noreply.github.com"
}
}
licenses {
license {
name = "MIT License"
url = "https://opensource.org/licenses/MIT"
}
}
scm {
connection = "scm:git:https://github.com/ShiYioo/java-toon.git"
developerConnection = "scm:git:https://github.com/ShiYioo/java-toon.git"
url = "https://github.com/ShiYioo/java-toon"
}
}
}
// 配置本地发布目录,用于生成 bundle
repositories {
maven {
name = "LocalRepo"
url = uri(layout.buildDirectory.dir("repo"))
}
}
}
java {
withJavadocJar()
withSourcesJar()
}
// 配置签名任务
signing {
// 优先从环境变量读取,其次从 gradle.properties 读取
val signingKeyId = System.getenv("SIGNING_KEY_ID")
?: findProperty("signing.keyId") as String?
val signingPassword = System.getenv("SIGNING_PASSWORD")
?: findProperty("signing.password") as String?
// 只有在具备签名凭据时才进行签名
if (!signingKeyId.isNullOrBlank() && !signingPassword.isNullOrBlank()) {
// 使用 useGpgCmd() 让 Gradle 使用系统的 gpg 命令
useGpgCmd()
sign(publishing.publications["maven"])
} else {
// 未配置签名凭据时,跳过签名(用于本地开发)
isRequired = false
}
}
// 创建 bundle 文件的任务
tasks.register<Zip>("createBundle") {
group = "publishing"
description = "创建用于手动上传到 Maven Central 的 bundle 文件"
dependsOn("publishMavenPublicationToLocalRepoRepository")
archiveFileName.set("${project.name}-${project.version}-bundle.zip")
destinationDirectory.set(layout.buildDirectory.dir("bundle"))
from(layout.buildDirectory.dir("repo")) {
include("**/*")
}
doLast {
println("Bundle 文件已创建: ${archiveFile.get().asFile.absolutePath}")
println("请访问 https://central.sonatype.com/publishing 上传此文件")
}
}
5. 发布
用 gradle 去执行 createBundle 命令,会发现 build/bundle 目录下生成了一个 zip 文件,这个文件就是我们要上传到 Maven Central 的 bundle 文件。
| 步骤 | 操作 |
|---|---|
| 1 | 访问 https://central.sonatype.com/publishing |
| 2 | 登录后,选择 "Upload" 选项卡,上传刚才生成的 zip 文件 |
| 3 | 等待审核通过,通常需要几分钟到几天时间 |
| 4 | 审核通过后,访问 https://search.maven.org/ |
TIP
搜索你的 artifactId,例如 java-toon。如果能找到你的库,说明发布成功!🎉
