🎏:你只管努力,剩下的交给时间
🏠 :小破站
打造自己的开源组件:如何将 Starter 发布到 Maven Central?
本文将详细介绍如何将你开发的 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 账号并创建项目空间
验证成功提醒
至此就完成了第二步操作🔚
安装配置GPG
这一步是必须的,而且建议使用脚本来配置,控制台我试了一下,交互不是那么好。
-
创建一个txt文件
txtKey-Type: RSA Key-Length: 2048 Name-Real: acowbo Name-Email: [email protected] Expire-Date: 0 Passphrase: 123456 %commit
这里只需要注意名称、邮箱、以及密码。别的按照我的填写即可!
-
执行命令
gpg --batch --gen-key key-config.txt
-
执行
gpg --list-keys
获取到key id,下一步会用到 -
上传到公钥服务器
shgpg --keyserver hkp://keyserver.ubuntu.com --send-keys 上一步的key id gpg --keyserver hkp://keys.openpgp.org --send-keys 上一步的key id
生成发布令牌
地址为:https://central.sonatype.com/account

保存这个令牌,后续会用到
配置发布
首先需要根据你刚刚获取到的用户令牌放到你使用的maven的setting.xml中,如下
xml
<settings>
<servers>
<server>
<id>central</id>
<username><!-- your token username --></username>
<password><!-- your token password --></password>
</server>
</servers>
</settings>
上面提到的都完成后就需要配置pom文件了。
xml
<?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>[email protected]</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> <!– 如果使用token认证 –> -->
</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
然后执行一个脚本
sh
#!/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中设置的。也建议大家这种做,保险一点
