Gradle 发布Jar到 Maven Central

1. 前置步骤

自 2024 年 3 月 12 日起,所有注册和发布都将通过 Central Portal 进行。

  1. Central Portal 帐户
  2. 注册命名空间
  3. 可用于对工件进行签名的 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 支持并跟上最新变化。

该插件内置了许多插件因此可以直接使用大部分功能

引入插件

kotlin 复制代码
plugins {
    id("com.vanniktech.maven.publish") version "0.30.0"
}

2.2 配置插件

这里直接复制我的配置即可,所有的变量都是可以改变的。

kotlin 复制代码
val artifactId = "HeadlessChrome"
val groupId = "io.github.zimoyin"
val versionId = "1.0"
val descriptions = "Headless Chrome"

val authorName = "zimoyin"
val developerId= authorName

val 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 = versionId

mavenPublishing {
    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 复制代码
kotlin.code.style=official

mavenCentralUsername=...
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

xml 复制代码
<server>
   <id>...</id>
   <username>...</username>
   <password>...</password>
</server>
  • signing.keyId : 是你公钥ID的后八位
  • signing.password: 密码,不多说
  • signing.secretKeyRingFile:私钥二进制文件
    关于如何生成私钥二进制文件,请使用以下命令
sh 复制代码
gpg --export-secret-keys -o private.bin [公钥ID后八位]

2.4 发布打包

IDEA 里面找到 publishing-->publishAllPublicationsToMavenCentralRepository,运行它即可

相关推荐
皮皮林55113 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河13 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程16 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅18 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者18 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺19 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart20 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP21 小时前
MyBatis-mybatis入门与增删改查
java
孟陬1 天前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端