1. 前置步骤
自 2024 年 3 月 12 日起,所有注册和发布都将通过 Central Portal 进行。
- Central Portal 帐户
- 注册命名空间
- 可用于对工件进行签名的 GPG 密钥
1. 创建 GPG 密钥对
2. 分发公钥
更多教程参考本文: 如何发布jar包到maven中央仓库(2024年3月最新版保姆级教程)
如果遇到命名空间无法创建请参阅这篇文章: 记一次从 Legacy OSSRH 到 Central Portal 进行迁移
2. 配置插件
截至到当前为止 Gradle 并没有任何官方插件用于发布 Jar 到Maven。故而这里选择一款开源插件用来帮助我们来实现
2.1 com.vanniktech.maven.publish
Gradle 插件可创建一项publish任务,自动将所有 Java、Kotlin 或 Android 库上传到任何 Maven 实例。此插件基于Chris Banes 的初始实现 ,并进行了增强以添加 Kotlin 支持并跟上最新变化。
该插件内置了许多插件因此可以直接使用大部分功能
引入插件
plugins {id("com.vanniktech.maven.publish") version "0.30.0"
}
2.2 配置插件
这里直接复制我的配置即可,所有的变量都是可以改变的。
val artifactId = "HeadlessChrome"
val groupId = "io.github.zimoyin"
val versionId = "1.0"
val descriptions = "Headless Chrome"val authorName = "zimoyin"
val developerId= authorNameval gitRepoName = artifactId
val gitUri = "github.com/${authorName}"
val emails = "tianxuanzimo@qq.com"val license = "The Apache License, Version 2.0"
val licenseUrl = "https://www.apache.org/licenses/LICENSE-2.0.txt"description = descriptions
group = groupId
version = versionIdmavenPublishing {publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)signAllPublications()if (!project.hasProperty("mavenCentralUsername")) {throw IllegalArgumentException("mavenCentralUsername is not set")} else if (!project.hasProperty("mavenCentralPassword")) {throw IllegalArgumentException("mavenCentralPassword is not set")} else if (!project.hasProperty("signing.keyId")) {throw IllegalArgumentException("signing.keyId is not set")} else if (!project.hasProperty("signing.password")) {throw IllegalArgumentException("signing.password is not set")}coordinates(groupId, artifactId, versionId)pom {name.set(artifactId)description.set(descriptions)inceptionYear.set("2025")url.set("https://$gitUri/$gitRepoName/")licenses {license {name.set(license)url.set(licenseUrl)distribution.set(licenseUrl)}}developers {developer {id.set(developerId)name.set(authorName)email.set(emails)url.set("https://$gitUri")}}scm {url.set(gitRepoName)connection.set("scm:git:git://$gitUri/$gitRepoName.git")developerConnection.set("scm:git:ssh://git@$gitUri/$gitRepoName.git")}}
}
2.3 配置密钥
在项目根目录下 build.gradle.kts
旁边有个 gradle.properties
我们需要在里面配置密钥,但是注意不要将gradle.properties
进行版本托管,否则可能会泄露
kotlin.code.style=officialmavenCentralUsername=...
mavenCentralPassword=...
signing.keyId=5F82D2CD
signing.password=...
signing.secretKeyRingFile=private.bin
依次解释这个几个配置
- mavenCentralUsername:在 central 中通过
Generate User Token
生成的令牌中的name
字段 - mavenCentralPassword:在 central 中通过
Generate User Token
生成的令牌中的password
字段
如果你通过 Generate User Token
生成后就会的到这个配置,这个配置的内容就是你所需要的 name,pasdword。
如果你不知道我在说什么请重新查阅阶段一的详细教程,
是另外一个博主的文章: 如何发布jar包到maven中央仓库(2024年3月最新版保姆级教程)
或者你在 central 网站中找到 你的头像(主页)—> View Account —> Generate User Token
<server><id>...</id><username>...</username><password>...</password>
</server>
- signing.keyId : 是你公钥ID的
后八位
- signing.password: 密码,不多说
- signing.secretKeyRingFile:私钥二进制文件
关于如何生成私钥二进制文件,请使用以下命令
gpg --export-secret-keys -o private.bin [公钥ID后八位]
2.4 发布打包
IDEA 里面找到 publishing
–>publishAllPublicationsToMavenCentralRepository
,运行它即可