Maven Central 上传(发布)JAR 包流程 - 详解
2026-01-14 12:03 tlnshuju 阅读(114) 评论(0) 收藏 举报将一个 Java 工程的 JAR 包发布到 Maven Central,可以让其他开发者只用 mvn dependency 就能引用你的库。这是许多开源组件的标准流程,但会涉及签名、账号、工件验证等步骤。
本文将以简洁且可复用的方式,带你从 0 到发布成功。
一、准备工作
1. 注册账号
https://central.sonatype.com右上角的“登录”。
支持通过 Google 或 GitHub 进行社交账号登录。也可以自定义用户名和密码。如果您选择使用社交账号登录,Sonatype 将获得与您的社交账号关联的电子邮件地址的访问权限。
2. 申请 Maven Central Namespace(GroupId)
在发布前必须申请你的 groupId,比如:
io.github.myusername
io.gitlab.myusername
io.gitee.myusername
io.bitbucket.myusername
com.company.project
# 域名
cn.ha
⚠️ 注意:Maven Central 不允许你使用未授权的 Namespace,如果你没有对应域名的所有权,会被拒绝。
最推荐的方式是使用 GitHub:
io.github.
Sonatype 不会拒绝 GitHub namespace,非常简单。
https://central.sonatype.com/publishing/namespaces 点击 Register New Namespace。
二、在本地生成 GPG 密钥
1. 安装 GnuPG
从 https://www.gnupg.org/download/下载 GnuPG 二进制文件。
mac 可以通过 Homebrew 安装 GnuPG
-- 安装
brew install gnupg
-- 显示 GnuPG 的版本信息
gpg --version
2.生成密钥对
Maven Central 要求所有 artifacts 必须签名。
gpg --full-generate-key
推荐配置:
Type: RSA & RSA
Key size: 4096
Expire: 0(永不过期)
查看 key:
gpg --list-keys
然后上传公钥到 keyserver(必须):
gpg --keyserver keyserver.ubuntu.com --send-keys <你的KEY_ID>
三、在项目中配置 Maven 发布所需内容
1. pom 文件
Maven Central 有要求:
✔ 必须包含 source JAR
✔ 必须包含 javadoc JAR(可用 empty-javadoc)
✔ 必须 GPG 签名
✔ 必须有完整的 POM 信息(license、开发者、scm 地址等)
以下为的 pom.xml 示例(可直接复制):
4.0.0
io.github.tom-hu
my-library
1.0.0
jar
my-library
A simple Java utility library
https://github.com/tom-hu/my-library
Apache License 2.0
https://www.apache.org/licenses/LICENSE-2.0.txt
tomhu
Tom Hu
https://github.com/tom-hu
scm:git:git://github.com/tom-hu/my-library.git
scm:git:ssh://github.com:tom-hu/my-library.git
https://github.com/tom-hu/my-library
org.apache.maven.plugins
maven-source-plugin
3.3.0
attach-sources
jar
org.apache.maven.plugins
maven-javadoc-plugin
3.6.2
attach-javadocs
jar
org.apache.maven.plugins
maven-gpg-plugin
3.1.0
sign-artifacts
verify
sign
gpg.passphrase
org.sonatype.central
central-publishing-maven-plugin
0.9.0
true
central
2. settings.xml
central
token
tokenx
gpg.passphrase
你的GPG密码
aliyunmaven
*
https://maven.aliyun.com/repository/public
release
release
四、执行发布命令
1. 发布:
mvn clean deploy -P release
等待 Deployments published。
可 https://central.sonatype.com/ 搜索查看。
浙公网安备 33010602011771号