本文最后更新于:December 2, 2026 pm
目录 账号准备 到Sonatype官网 https://central.sonatype.com 注册登录,可以直接使用GitHub账号进行授权登录。
namespaces创建 在右上角用户名 -> View Namespaces -> Register New Namespace,创建一个新的Namespace。
这里我用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)下载,免费下载的!!!。
生成密钥对 gpg --gen-key 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。接下来就是上传公钥至公共服务器,让中央仓库能验证签名合法性
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
执行后会有类似如下输出:
gpg: sending key yyyyyyyyyyyyyyyyy to hkp://keyserver.ubuntu.com:11371
然后验证 gpg 秘钥:
gpg --keyserver keyserver.ubuntu.com --recv-keys yyyyyyyyyyyyyyyyy(或者 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 都可以)
输出类似如下:
gpg: key yyyyyyyyyyyyyyyyy: "用户名 <邮箱>" not changed gpg: Total number processed: 1 gpg: unchanged: 1
另外一种方式直接到 https://keyserver.ubuntu.com/ 秘钥平台查询:输入上面的yyyyyyyyyyyyyyyyy,显示类似如下表示成功:
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 <servers > <server > <id > central</id > <username > Sonatype生成的Token用户名</username > <password > Sonatype生成的Token密码</password > </server > </servers > <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 > io.github.你的用户名</groupId > <artifactId > 组件名称</artifactId > <version > 1.0.0.RELEASE</version > <name > 组件名称</name > <description > 模块描述</description > <url > 你的代码仓库地址(如https://github.com/用户名/仓库名)</url > <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 > <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 > <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 中添加以下依赖,即可直接使用。