自定义组件上架Maven中央仓库核心步骤

本文最后更新于:December 2, 2026 pm

目录

账号准备

到Sonatype官网 https://central.sonatype.com 注册登录,可以直接使用GitHub账号进行授权登录。

namespaces创建

在右上角用户名 -> View Namespaces -> Register New Namespace,创建一个新的Namespace。

  • 有自有域名:如 www.tothefor.com 对应 com.tothefor
  • 无域名:GitHub 用户填 io.github.用户名;

这里我用GitHub账号授权以及使用自定义域名为例。

创建完成后,点击Verify Namespace,会要求让你去添加一条TXT的解析记录,添加完成后点击进行校验,成功后会显示 Verified。

访问Token生成

点击右上角用户名 -> View Account -> Generate User Token,复制生成的用户名和密码,后续用于 Maven 认证(仅显示一次,务必保存)。

GPG 密钥配置

在 GnuPG 官网 https://gnupg.org/download/index.html 根据系统选择对应版本(Windows
选 Gpg4win,Mac 选 Mac GPG)下载,免费下载的!!!。

生成密钥对

1
2
3
4
5
6
7
8
9
10
11
12
13
gpg --gen-key
# 然后按要求输入用户名和邮箱,这里和Sonatype 账号一致(也就是GitHub的用户名和邮箱)
# 然后输入O
O
# 会有一个弹窗进行输入,用于设置密钥密码并牢记
# 完成后,终端会输出类似如下内容:
public and secret key created and signed.

pub ed77777 2026-11-30 [SC] [expires: 2029-11-29]
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
uid 你的用户名 <你的邮箱>
sub cv77777 2026-11-30 [E] [expires: 2029-11-29]
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

其中,”pub” 行后的一串字符(如 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx),这是你的公钥
ID。接下来就是上传公钥至公共服务器,让中央仓库能验证签名合法性

1
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

执行后会有类似如下输出:

1
gpg: sending key yyyyyyyyyyyyyyyyy to hkp://keyserver.ubuntu.com:11371

然后验证 gpg 秘钥:

1
gpg --keyserver keyserver.ubuntu.com --recv-keys yyyyyyyyyyyyyyyyy(或者 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 都可以)

输出类似如下:

1
2
3
gpg: key yyyyyyyyyyyyyyyyy: "用户名 <邮箱>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1

另外一种方式直接到 https://keyserver.ubuntu.com/ 秘钥平台查询:输入上面的yyyyyyyyyyyyyyyyy,显示类似如下表示成功:

1
2
3
4
5
6
7
pub (4)aaaaaaaaaaaaaaaaaaaaaaaaa 2026-11-30T02:16:36Z

uid 用户名 <邮箱>
sig aaaa aaaaaaaaaaaaaaaa 2026-11-30T02:16:36Z 2029-11-29T02:16:36Z ____________________ [selfsig]

sub (4)bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 2026-11-30T02:16:36Z
sig bbbb bbbbbbbbbbbbbbb 2026-11-30T02:16:36Z ____________________ 2029-11-29T02:16:36Z []

配置Maven

打开 Maven 的 settings.xml 文件(通常在 conf 目录下),添加以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- Sonatype访问权限配置 -->
<servers>
<server>
<id>central</id>
<username>Sonatype生成的Token用户名</username>
<password>Sonatype生成的Token密码</password>
</server>
</servers>
<!-- GPG签名配置 -->
<profiles>
<profile>
<id>gpg</id>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.keyname>你的GPG绑定邮箱</gpg.keyname>
<gpg.passphrase>你的GPG密钥密码</gpg.passphrase>
<gpg.useagent>true</gpg.useagent>
</properties>
</profile>
</profiles>
  • 密钥密码:即最开始输入终端时手动输入的密码。

配置项目POM文件

修改待发布项目的 pom.xml,补充必要信息(直接复制替换占位符即可):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

<project>
<!-- 核心信息:GroupID需与验证通过的Namespace一致 -->
<groupId>io.github.你的用户名</groupId>
<artifactId>组件名称</artifactId>
<version>1.0.0.RELEASE</version>
<!-- 组件名称 -->
<name>组件名称</name>
<!-- 模块描述 -->
<description>模块描述</description>
<!-- 必须是正式版本,禁止SNAPSHOT -->
<url>你的代码仓库地址(如https://github.com/用户名/仓库名)</url>
<!-- 许可证信息(推荐Apache 2.0) -->
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<!-- 开发者信息 -->
<developers>
<developer>
<name>你的姓名</name>
<email>你的邮箱</email>
</developer>
</developers>
<!-- 代码仓库信息 -->
<scm>
<connection>scm:git:你的仓库克隆地址(如https://github.com/用户名/仓库名.git)</connection>
<developerConnection>scm:git:你的仓库SSH地址(如git@github.com:用户名/仓库名.git)</developerConnection>
<url>你的仓库网页地址</url>
</scm>
<!-- 必要插件配置 -->
<build>
<plugins>
<!-- 源码打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<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>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 中央仓库发布插件 -->
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.4.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
<tokenAuth>true</tokenAuth>
</configuration>
</plugin>
</plugins>
</build>
</project>

打包上传

  • 直接在IDEA中:Maven -> clean -> deploy 接口,过程会让输入之前设置的密钥密码。然后会自动校验POM文件中的信息是否完整,按照报错信息处理即可。
  • 等待 Sonatype 审核:登录 Sonatype 平台,在 Deployments 中可看到状态,然后手动Publish后会变成PUBLISHING(PUBLISHING
    为审核中),审核时间通常为几小时到 1 天,状态变为
    PUBLISHED 即发布成功。

使用

发布成功后,可通过 Maven 中央仓库搜索页 https://central.sonatype.com,输入 GroupID 或组件名称查询你的组件。其他开发者只需在 pom.xml 中添加以下依赖,即可直接使用。


本文作者: 墨水记忆
本文链接: https://tothefor.com/DragonOne/908f8b12.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!