当前位置: 首页 > ops >正文

打造自己的开源组件:如何将 Starter 发布到 Maven Central?

欢迎来到我的博客,代码的世界里,每一行都是一个故事


在这里插入图片描述

🎏:你只管努力,剩下的交给时间

🏠 :小破站

打造自己的开源组件:如何将 Starter 发布到 Maven Central?

    • 一、准备工作
    • 注册 OSSRH 账号并创建项目空间
    • 安装配置GPG
    • 生成发布令牌
    • 配置发布

本文将详细介绍如何将你开发的 Java Starter 组件发布到 Maven Central,涵盖从项目准备、GPG 签名、Sonatype 注册、Nexus 配置、到最终发布的完整流程。通过一个真实项目为例,带你快速掌握整个发布链路,避免踩坑,助你轻松构建自己的开源组件生态!

开发自己的 Starter 很爽,但你有没有想过让全球的 Java 开发者都能 引入它?这就需要你把它发布到 Maven Central

然而,第一次发布总是伴随着各种“仪式感”:GPG 密钥、Sonatype 审核、签名校验失败、POM 配置报错……是不是已经劝退了不少人?

别担心,这篇文章将用最通俗易懂的方式,手把手带你把自己的 Java 组件优雅地发布到 Maven Central

一、准备工作

  • ✅ 拥有一个已经发布成功的 Java 项目(以我的acowbo-excel-starter 为例)
  • ✅ 注册 Sonatype OSSRH
  • ✅ 安装并配置 GPG(用于代码签名)
  • ✅ 生成发布令牌
  • ✅ 拥有 GitHub 仓库(开源项目推荐)

注册 OSSRH 账号并创建项目空间

大家可以在这个网址进行注册https://central.sonatype.com/,我是直接使用的谷歌邮箱,当然还支持github。注册登录之后进行命名空间的创建。命名建议直接使用github的,也就是io.github.你的名称,然后会出现验证命名空间,如下图:
image-20250520102416745

验证成功提醒
image-20250520102503216

至此就完成了第二步操作🔚

安装配置GPG

这一步是必须的,而且建议使用脚本来配置,控制台我试了一下,交互不是那么好。

  1. 创建一个txt文件

    Key-Type: RSA
    Key-Length: 2048
    Name-Real: acowbo
    Name-Email: abcd@gmail.com
    Expire-Date: 0
    Passphrase: 123456
    %commit
    

    这里只需要注意名称、邮箱、以及密码。别的按照我的填写即可!

  2. 执行命令gpg --batch --gen-key key-config.txt

    image-20250521100943706

  3. 执行gpg --list-keys获取到key id,下一步会用到

    image-20250521101615894

  4. 上传到公钥服务器

    gpg --keyserver hkp://keyserver.ubuntu.com --send-keys 上一步的key id
    gpg --keyserver hkp://keys.openpgp.org --send-keys 上一步的key id
    

生成发布令牌

地址为:https://central.sonatype.com/account

image-20250521102025446

保存这个令牌,后续会用到

配置发布

首先需要根据你刚刚获取到的用户令牌放到你使用的maven的setting.xml中,如下

<settings><servers><server><id>central</id><username><!-- your token username --></username><password><!-- your token password --></password></server></servers>
</settings>

上面提到的都完成后就需要配置pom文件了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>io.github.acowbo</groupId><artifactId>acowbo-excel-starter</artifactId><version>1.0.0</version><packaging>jar</packaging><name>acowbo-excel-starter</name><description>Excel import/export starter for Spring Boot</description><url>https://github.com/acowbo/acowbo-excel-starter</url><!-- 许可证信息 --><licenses><license><name>The Apache License, Version 2.0</name><url>http://www.apache.org/licenses/LICENSE-2.0.txt</url></license></licenses><!-- 开发者信息 --><developers><developer><name>acowbo</name><email>todoitbo@gmail.com</email><organization>acowbo</organization><organizationUrl>https://github.com/acowbo</organizationUrl></developer></developers><!-- SCM 信息 --><scm><connection>scm:git:git://github.com/acowbo/acowbo-excel-starter.git</connection><developerConnection>scm:git:ssh://github.com:acowbo/acowbo-excel-starter.git</developerConnection><url>https://github.com/acowbo/acowbo-excel-starter/tree/master</url></scm><build><plugins><!-- 生成源码JAR --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.2.1</version><executions><execution><id>attach-sources</id><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin><!-- 生成JavaDoc JAR --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>3.3.1</version><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><!-- GPG签名 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-gpg-plugin</artifactId><version>1.5</version><configuration><passphrase>${env.GPG_PASSPHRASE}</passphrase></configuration><executions><execution><id>sign-artifacts</id><phase>verify</phase><goals><goal>sign</goal></goals></execution></executions></plugin><!-- Central发布插件 --><plugin><groupId>org.sonatype.central</groupId><artifactId>central-publishing-maven-plugin</artifactId><version>0.7.0</version><extensions>true</extensions><configuration><publishingServerId>central</publishingServerId><!-- <tokenAuth>true</tokenAuth> &lt;!&ndash; 如果使用token认证 &ndash;&gt; --></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins></build>
</project> 

具体的配置要求大家也可以看这个文档:https://central.sonatype.org/publish/requirements/#provide-file-checksums

然后执行一个脚本

#!/bin/bash
# deploy.sh# 设置GPG密码为环境变量
export GPG_TTY=$(tty)
export GPG_PASSPHRASE="你刚刚配置的gpg密码"# 执行Maven命令
mvn clean deploy --settings /Users/xiaobo/webSoft/apache-maven-3.8.1/conf/settings_to_private_mvn.xml# 清理环境变量
unset GPG_PASSPHRASE

⚠️:这里说明一下为什么我执行settings,因为我发现没有走我在idea中设置的。也建议大家这种做,保险一点

发布成功页面

http://www.xdnf.cn/news/9190.html

相关文章:

  • 人工智能100问☞第34问:什么是语音识别与合成?
  • xilinx 7系列底层可配置逻辑块CLB资源简介
  • js 实现多并发任务处理
  • AI时代的弯道超车之第二十一章:AI会颠覆哪些行业?
  • 什么是MCP技术,跟http技术有什么区别
  • Excel 统计某个字符串在指定区域出现的次数
  • 低空经济管理系统设计方案
  • Spring Boot 3.4.6 中文文档上线
  • 深入理解 JDK、JRE 和 JVM 的区别
  • CellularPro 1.8.6.1 | 提升网络速度,抢到更多基站的速度
  • Netty创新架构突破链接数瓶颈技术,如何应用于新能源汽车智慧充电桩?
  • Redis 容器启动失败Fatal error loading the DB, check server logs. Exiting.的解决方法
  • 使用 ssld 提取CMS 签名并重签名
  • 在PyTorch中,有了y = x + y,为什么还需要y += x,有什么好处呢?
  • 九级融智台阶的要素协同跃迁框架
  • 6个月Python学习计划 Day 6 - 综合实战:学生信息管理系统
  • ai写歌平台:AnKo开启音乐创作的智能时代!
  • java类加载器
  • 树莓派超全系列教程文档--(50)如何查找树莓派的IP地址
  • 计算机组成与体系结构:硬盘驱动器(Hard Disk Drives)
  • OpenGL Chan视频学习-9 Index Buffers inOpenGL
  • STM32F407VET6学习笔记6:定时器TIM2的配置使用
  • MPLS实验复现
  • 70页精品PPT | 休闲食品行业数据分析平台建设方案快消BI大数据解决方案BI方案
  • [ Qt ] | 常用控件(三):
  • AR眼镜+AI视频盒子+视频监控联网平台:消防救援的智能革命
  • 板凳-------Mysql cookbook学习 (七)
  • 钠离子电池循环寿命突破万次
  • [网页五子棋][用户模块]客户端开发(登录功能和注册功能)
  • 【C语言】排序方法