1. 前置步骤自 2024 年 3 月 12 日起所有注册和发布都将通过 Central Portal 进行。Central Portal 帐户注册命名空间可用于对工件进行签名的 GPG 密钥1. 创建 GPG 密钥对2. 分发公钥3. 如果不下载 GPG 可以使用 git 的Git\usr\bin\gpg.exe更多教程参考本文: 如何发布jar包到maven中央仓库2024年3月最新版保姆级教程如果遇到命名空间无法创建请参阅这篇文章: 记一次从 Legacy OSSRH 到 Central Portal 进行迁移2. 配置插件截至到当前为止 Gradle 并没有任何官方插件用于发布 Jar 到Maven。故而这里选择一款开源插件用来帮助我们来实现2.1 com.vanniktech.maven.publishGradle 插件可创建一项publish任务自动将所有 Java、Kotlin 或 Android 库上传到任何 Maven 实例。此插件基于Chris Banes 的初始实现 并进行了增强以添加 Kotlin 支持并跟上最新变化。该插件内置了许多插件因此可以直接使用大部分功能引入插件plugins{id(com.vanniktech.maven.publish)version0.32.0}2.2 配置插件这里直接复制我的配置即可所有的变量都是可以改变的。valartifactIdHeadlessChromevalgroupIdio.github.zimoyinvalversionId1.0valdescriptionsHeadless ChromevalauthorNamezimoyinvaldeveloperIdauthorNamevalgitRepoNameartifactIdvalgitUrigithub.com/${authorName}valemailstianxuanzimoqq.comvallicenseThe Apache License, Version 2.0vallicenseUrlhttps://www.apache.org/licenses/LICENSE-2.0.txtdescriptiondescriptions groupgroupId versionversionId mavenPublishing{publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)signAllPublications()if(!project.hasProperty(mavenCentralUsername)){throwIllegalArgumentException(mavenCentralUsername is not set)}elseif(!project.hasProperty(mavenCentralPassword)){throwIllegalArgumentException(mavenCentralPassword is not set)}elseif(!project.hasProperty(signing.keyId)){throwIllegalArgumentException(signing.keyId is not set)}elseif(!project.hasProperty(signing.password)){throwIllegalArgumentException(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.styleofficial mavenCentralUsername...mavenCentralPassword...signing.keyId5F82D2CD signing.password...signing.secretKeyRingFileprivate.bin依次解释这个几个配置central 仓库配置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 Tokenserverid.../idusername.../usernamepassword.../password/serversigning.keyId 是你公钥ID的后八位signing.password: 密码不多说signing.secretKeyRingFile私钥二进制文件关于如何生成私钥二进制文件请使用以下命令gpg --export-secret-keys-oprivate.bin[公钥ID后八位]如果你需要更多的关于 gpg 的知识请查阅文章前面关于 gpg 的文章地址引用2.4 发布打包IDEA 里面找到publishing–publishAllPublicationsToMavenCentralRepository运行它即可3. android 发布包步骤同上但是你需要修改以下几个地方包括文件位置。Android 项目根gradle.properties中写入仓库与证书等信息Android LibraryModule 的build.gradle.kts中需要写入项目配置信息private.bin 文件需要在每个需要上传的Android LibraryModule 的根路径下4. 注意依赖传递在你需要上传到 maven 的项目中依赖使用了api或者implementation引入其他依赖是不同的。使用implementation引入的依赖无法被引用了该项目的人使用。详见