发布Jar到Maven中央仓库--Maven版
摘要
- 2024-12-11,老用户发布的权限已失效,登录https://central.sonatype.com后提示可以将原来的命名空间迁移过来,但笔者按提示迁移报错,于是邮件联系官方,官方回复说有两个选择,1是恢复https://oss.sonatype.org上的发布权限,2是官方的技术人员在后台将命名空间迁移到新的central上来,以后要求我只能使用新的方式发布,笔者选择了后者。
- https://oss.sonatype.org已经不再支持新用户注册,新的注册地址为https://central.sonatype.com,所以本文内容已经过时,但是老用户不受影响,依旧可以继续通过本文方式进行发布,唯一区别就是在配置文件settings.xml中添加server节点配置用户认证时不再支持用户名和密码的认证方式,而是要使用User Token的方式。最新的方式请参看 发布Jar到Maven中央仓库--Maven版(最新方式)
- 通过本文,你将知道如何将Maven构建的项目发布到Maven中央仓库
- Gradle构建方式请看 发布Jar到Maven中央仓库--Gradle版
一、将项目推送到远程仓库,如 Github或者Gitee
二、注册 Sonatype 账户 – 就是一个JIRA
进入 https://issues.sonatype.org/secure/Dashboard.jspa 注册一个账号,邮箱要真实。
三、登录 Sonatype 创建工单–每部署一个新项目就创建一个工单
- 
登录后点击 新建创建工单,如果没有显示新建菜单,可以直接请求https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
| 1 | Summary:工单摘要,随便给个名称 | 
- 
注意: Group Id:最好填写你拥有的域名,比如我的域名是hanqunfeng.com,这里就填写com.hanqunfeng,这样后面验证domain时会方便一些。我第一次就是不知道这个事情,所以随便填的hanqf.org。
- 
创建好工单后会收到两封邮件: - 
第一封,通知你创建工单成功。 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19hanqunfeng created OSSRH-62247: 
 ----------------------------------
 Summary: reactive-redis-cache-annotation
 Key: OSSRH-62247
 URL: https://issues.sonatype.org/browse/OSSRH-62247
 Project: Community Support - Open Source Project Repository Hosting
 Issue Type: New Project
 Reporter: hanqunfeng
 Assignee: Joel Orlina
 reactive-redis-cache-annotation-spring-boot-starter
 --
 This message was sent by Atlassian Jira
 (v8.5.7#805007)
- 
第二封邮件,要求你验证 domain,两封邮件间隔大约5分钟。同时你在工单的注释中也能看到系统回复的同样的信息。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[ https://issues.sonatype.org/browse/OSSRH-62247?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] 
 Central OSSRH updated OSSRH-62247:
 ----------------------------------
 Status: Waiting for Response (was: Open)
 Do you own the domain hanqf.org? If so, please verify ownership via one of the following methods:
 * Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-62247 (Fastest)
 * Setup a redirect to your Github page (if it does not already exist)
 If you do not own this domain, please read:
 http://central.sonatype.org/pages/choosing-your-coordinates.html
 You may also choose a groupId that reflects your project hosting, in this case, something like io.github.hanqunfeng or com.github.hanqunfeng
 > reactive-redis-cache-annotation
 > -------------------------------
 >
 > Key: OSSRH-62247
 > URL: https://issues.sonatype.org/browse/OSSRH-62247
 > Project: Community Support - Open Source Project Repository Hosting
 > Issue Type: New Project
 > Reporter: hanqunfeng
 > Assignee: Joel Orlina
 > Priority: Major
 >
 > reactive-redis-cache-annotation-spring-boot-starter
 
- 
- 
看邮件说明,如果是自己的域名,添加一条 TXT record是最快速的验证方式,刚好我有一个域名,于是修改了工单的Group Id,修改工单时需要填写注释,随便填就行。
- 
然后去设置了 DNS,一条TXT记录,key是工单号,我这里就是OSSRH-62247,值是工单地址,这里就是https://issues.sonatype.org/browse/OSSRH-62247,创建好后,在工单中添加一条注释
| 1 | I have change the group Id to com.hanqunfeng,and I own the domain hanqunfeng.com,and I have added a dns record TXT. | 
其它验证方式我并未尝试。
- 
过一会你就会收到回复邮件,通知你验证完成,可以发布了,并且给出了发布地址。 - 注意这里是先发布到https://oss.sonatype.org,而不是maven中央仓库的地址。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[ https://issues.sonatype.org/browse/OSSRH-62247?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] 
 Central OSSRH resolved OSSRH-62247.
 -----------------------------------
 Resolution: Fixed
 com.hanqunfeng has been prepared, now user(s) hanqf can:
 * Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
 * Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
 * Release staged artifacts into repository 'Releases'
 please comment on this ticket when you promoted your first release, thanks
 > reactive-redis-cache-annotation
 > -------------------------------
 >
 > Key: OSSRH-62247
 > URL: https://issues.sonatype.org/browse/OSSRH-62247
 > Project: Community Support - Open Source Project Repository Hosting
 > Issue Type: New Project
 > Reporter: hanqunfeng
 > Assignee: Joel Orlina
 > Priority: Major
 >
 > reactive-redis-cache-annotation-spring-boot-starter
 --
 This message was sent by Atlassian Jira
 (v8.5.7#805007)
 
- 注意这里是先发布到
四、发布
- 
1.准备签名 - 可以使用工具创建密钥对
 需要下载一个签名工具,我是mac电脑,下载的是https://gpgtools.org。
 安装后点击新建,按照提示创建一个密钥对即可,注意高级选项里有个过期时间,默认是3年。创建好后会主动提示你是否将公钥发布到key server,点击Upload Public key即可。也可以在创建后的证书列表页面邮件选择证书–>Send Public Key To Key Server。
 导出证书时,勾选密码并设置密码就是私钥和公钥证书,不勾选密码就是公钥,看生成文件的名称就可以,公开就是公钥,私密就是私钥,格式都是 asc,其实就是字符串,可以用记事本打开查看。如果windows系统,可以下载https://www.gpg4win.org/ ,使用方式差不多,最后点击“将公钥上传的目录服务”。 公钥发布到 key server后要稍微等一会,大约10分钟吧,因为key server有多个,同步需要一些时间。
 记住你创建密钥对时的密码,发布项目时要使用。- 
也可以使用命令行创建密钥对,版本 [gpg (GnuPG/MacGPG2) 2.2.24]1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22# 创建密钥对,按提示输入用户名称和邮箱地址 
 gpg --generate-key
 # 列出密钥,hanqunfeng就是创建密钥对是的用户名,此处也可以使用邮箱
 # 结果中第二行一长串的后8位就是keyId,比如:30FF8D58,gradle构建时会用到
 gpg --list-keys hanqunfeng
 # 也可以直接通过id查询
 gpg --list-keys 30FF8D58
 # 上传公钥到server key,默认上传到hkps://keys.openpgp.org,但是提示上传失败
 # 看到网上的示例可以通过--keyserver指定上传的服务器地址,但是我这个版本[gpg (GnuPG/MacGPG2) 2.2.24]没有这个参数
 # 使用 https://gpgtools.org 上传公钥就会成功
 gpg --send-keys 30FF8D58
 # 查看指纹
 gpg --fingerprint 30FF8D58
 # 删除私钥,这里也可以使用用户名称或者邮箱,如果唯一的话
 gpg --delete-secret-keys 30FF8D58
 # 删除公钥
 gpg --delete-keys 30FF8D58
 
- 可以使用工具创建密钥对
- 
2.settings.xml 
 配置maven的settings.xml文件,设置一个server,里面添加Sonatype的账号和密码。
 注意当前已经不再支持帐号密码的认证方式,需要配置为 User Token
登录
https://oss.sonatype.org–>点击右上角的用户名称—>Profile–>下拉选择User Token–>Generate Token
| 1 | <settings> | 
- 
3.pom.xml,重点是后面几个plugin 
 这里重点说一下nexus-staging-maven-plugin这个插件,该插件会将我们发布到https://oss.sonatype.org的jar自动发布到maven中央仓库,如果没有这个插件,我们需要登录https://oss.sonatype.org,然后手工点击Staging Repositories,找到你发布的包(GroupId开头的),然后点击close,再点击release。有说这种方式容易失败(我没有测试),推荐使用插件自动发布。
| 1 | 
 | 
- 
4.执行命令 
| 1 | mvn clean package # 完成打包和测试 | 
4.1 执行过程中会提示你输入创建密钥对时的密码,如果不想人工参与,也可以使用如下方式(参考:http://maven.apache.org/plugins/maven-gpg-plugin/usage.html)
- 
a.在执行命令时指定密码: 1 mvn clean deploy -Dgpg.passphrase=thephrase 
- 
b.setting.xml中创建一个server 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10<settings> 
 [...]
 <servers>
 [...]
 <server>
 <id>gpg.passphrase</id>
 <passphrase>clear or encrypted text</passphrase>
 </server>
 </servers>
 </settings>
- 
5.执行成功后即说明发布到 maven中央仓库成功了,过一会就会收到邮件
| 1 | [ https://issues.sonatype.org/browse/OSSRH-62247?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] | 
这封邮件的大意是提示你,大约10分钟后你就可以将依赖添加到项目中进行下载了,不过要通过https://search.maven.org(现已重定向到https://central.sonatype.com)检索到需要等待两个小时,毕竟更新索引也是需要时间的。
另外,如果你确认发布是成功的,记得要回到工单添加个注释,如
| 1 | I've released Releases successfully.Thank you! | 
- 
6.更新部署 
 修改pom.xml中的版本号,重新执行mvn clean deploy即可。此时不会再收到sonatype发的邮件,依旧等待大约10分钟后你就可以将依赖添加到项目中进行下载了,同样https://search.maven.org(现已重定向到https://central.sonatype.com)检索到需要等待两个小时。
 
 
 
 
 
 
 
 
 
