用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: 可以留空。
⚠️ 重要:最后,它会要求你输入一个密码 (Passphrase),请务必牢记这个密码,发布时需要用到。
查看密钥
gpg --list-secret-keys
输出类似如下:
pub rsa4096 2025-11-02 [SC]
3AxxxxxxCBF4xxxxxxxxxxxxxxxxxxxxx
uid ShiYi (null) 3401111804@136.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文件
访问 https://central.sonatype.com/publishing
登录后,选择 "Upload" 选项卡,上传刚才生成的zip文件
上传完成后,等待审核通过,通常需要几分钟到几天时间
审核通过后,访问 https://search.maven.org/
搜索你的artifactId,例如 "java-toon"
如果能找到你的库,说明发布成功!
