通过使用gitee发布项目到Maven中央仓库最新教程
序言
传统的OSSRH出版服务将于2025年6月30日结束,要迁移到Central Portal Publisher Service,以前很多教程都即将失效,所以写个新的记录一下。
先决条件
- 在central.sonatype.com有一个账号,如果还没创建,请先去创建。
- 在Gitee有一个账号,如果还没创建,请先去创建。
创建命令空间(namespace)
在central.sonatype.com登录后,点击头像,选择View Namespaces,进入命名空间
点击右上角Register New Namespace按钮,输入io.gitee.xxxx,这里的xxxx需要去替换为Gitee个人资料下的个人空间地址。
输入完命名空间后,点击submit,此时会来到待验证的状态
复制其中的verification key,去Gitee创建一个以这个key为名称的、公开的仓库后,回到此处,点击Verify Namespace即可。这个仓库在验证完成后可以删除。
验证完成后如下图所示
创建并配置token给Maven
在central.sonatype.com登录后,点击头像,选择View Account
来到如下图所示页面,点击Generate User Token,创建一个User Token
进入Maven安装目录下的conf文件夹,打开settings.xml文件,添加如下所示的server标签,请将token-username
与token-password
替换为token生成的对应内容。
注意:如果你要自定义一个其他的id,在后续的pom.xml的配置中,publishingServerId这个标签的内容要与server标签中的id一致。
maven中的settings.xml
<settings><servers><server><id>ossrh</id><username>token-username</username><password>token-password</password></server></servers>
</settings>
使用GPG生成密钥
前往GPG4win官方下载页面下载gpg
下载完成后,双击运行gpg4win-4.4.1.exe,然后一直点击下一步,在安装目录这里需要注意一下,如果不想装到C盘,就自己修改一个
安装完成后,会有一个Kleopatra的应用,运行它
点击右上角,文件->新建OpenGPG密钥对,记得勾选使用密码保护
点击OK后,会弹出输入密码的提示,输入完成后,继续点击OK
接下来右键新创建的证书,点击“在服务器上发布”
点击导出证书
导出完成后,在右键点击这个证书,选择细节,复制其中的指纹
这个指纹在下节配置Pom.xml时还会用到
然后前往https://keyserver.ubuntu.com/,通过这个证书指纹查询证书是否发布成功。如果搜索得到内容,就发布成功了。
配置项目pom.xml
<build><plugins><!-- 添加 source 和 javadoc 插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.3.1</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>3.11.2</version><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.2.7</version><executions><execution><id>sign-artifacts</id><phase>verify</phase><goals><goal>sign</goal></goals><configuration><keyname><!-- 这里就是你的证书指纹 --></keyname></configuration></execution></executions></plugin><plugin><groupId>org.sonatype.central</groupId><artifactId>central-publishing-maven-plugin</artifactId><version>0.8.0</version><extensions>true</extensions><configuration><!--与maven配置中的id保持一致--><publishingServerId>ossrh</publishingServerId><checksums>required</checksums></configuration></plugin></plugins></build>
测试
# 输入maven命令部署
mvn clean deploy
# 出现一下BUILD SUCCESS就代表成功了
.....
[INFO] Deployment 0fadc095-8f3d-4555-8413-adbe9cf1e28b has been validated. To finish publishing visit https://central.sonatype.com/publishing/deployments
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.590 s
[INFO] Finished at: 2025-06-23T15:16:13+08:00
[INFO] ------------------------------------------------------------------------
参考
- https://central.sonatype.org/publish/publish-portal-maven/
- https://central.sonatype.org/publish/generate-portal-token/