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

打造自己的开源组件:如何将 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中设置的。也建议大家这种做,保险一点

发布成功页面

相关文章:

  • 经典查找算法合集(上)
  • 59、【OS】【Nuttx】编码规范解读(七)
  • 【MQTT】TLS证书双向验证
  • ROS2 robot控制学习(一)
  • Java 并发编程通关秘籍——08死锁
  • STL-Library-Containers
  • Acrobat Reader 无法在 Windows 11及10 中打开的5种修复方法
  • 岛津Sonialvision X-ray X射线高压发生器控制台
  • RuoYi前后端分离框架集成Jasypt实现配置信息加密
  • C语言创意编程:用趣味实例玩转基础语法(2)
  • Redis工作原理解析
  • 学者观察 | Web3.0的技术革新与挑战——北京理工大学教授沈蒙
  • BERT和GPT语言模型的核心差异
  • How API Gateways handle raw TCP packets
  • 【深度学习-Day 18】从SGD到Adam:深度学习优化器进阶指南与实战选择
  • CentOS 7.0重置root密码
  • 【Marp】自定义主题 - box01
  • 【第1章 基础知识】1.2 Canvas 的绘图环境
  • iPaaS集成平台如何赋能智能体搭建
  • 【递归、搜索与回溯算法】专题一 递归
  • 什么网站做招聘效果好/时事热点新闻
  • 网站开发需要的软件有哪些/win10优化大师是官方的吗
  • 临猗做网站/宁波seo优化排名
  • 做网站都需要年服务费吗/优化seo设置
  • 福州金山网站建设/seo平台优化服务
  • 之梦一个系统做多个网站/惠州seo整站优化