在官方的文档中提到:

从 2024 年 3 月 12 日起,所有注册都将通过中央门户网站进行。有关传统注册的信息,请参阅相关文档。有关在门户网站早期访问期间通过传统的 OSSRH 发布方式进行发布的支持,请参阅以下部分。

所以 2024 年 3 月 12 日之后,【拥抱开源】发布自己的项目到maven中央仓库博主原来写的教程就不适用了,所以这次博主根据他的官方文档出了一篇新教程,踩了很多的坑,最后三天终于把他弄出来了,这里我会把我遇到的坑给大家整理一下,避免大家踩坑。

如果你没看过原来的那篇文章没问题的【拥抱开源】发布自己的项目到maven中央仓库,现在我们开始从0开始,到最后jar包成功发布在maven中央仓库上。

账号准备

第一步,注册账号

central.sonatype.com/

依次输入username,邮箱地址,和密码。

注册完了是这样的:

第二步,新建命名空间

根据上面的教程点击创建命名空间,这里面可以看博主已经在之前创建了两个了。如果你的代码存在Gitee上,你就需要把命名空间设置为io.gitee.myusername。这里的myusername是你的gitee地址,例如博主的是https://gitee.com/wangfugui-ma所以我的命名空间是:io.gitee.wangfugui-ma

其他的仓库可以按照下面的要求来创建: GitHub io.github.myusername GitLab io.gitlab.myusername Gitee io.gitee.myusername Bitbucket io.bitbucket.myusername

第三步,验证命名空间

创建之后需要验证命名空间来依次证明这个命名空间是你独有的,需要在对应的地址创建一个开源的仓库,名称就是你点击verify namespace的名称,例如下面这样:

点击verify namespace

创建好了之后点击confirm按钮即可进行下一步。

第四步,创建push的账号和密码

这一步抛弃了原来固定的username和password,选择了一个随机的username和password,这个username和password用来push你的jar包到中央仓库里面去,所以一定要保存好,以后都不会显示了,只有在创建成功的时候才会显示一次。

点击右上角的view account

点击Generate User Token

点击ok

最终会生成一个这样的数据,大家保存好

设置setting

把他粘贴到maven的setting.xml 文件里面

csharp

代码解读

复制代码

${server} rRdszgHD wqKmkox2x2y2+yA/3ER/TYwX0eQoBOJYZSVLcnegAVgi

这个${server}可以写成你自定义的id,待会会用到,记住。

GPG准备

第一步,下载GPG

GPG 用于创建asc文件用于验证你的文件的正确性和安全性,我们直接去官网下载:

gnupg.org/download/in…

第二步,生成秘钥

去图中的路径的bin路径下,进入cmd命令行

生成秘钥

输入gpg --gen-key

依次输入名称,邮箱地址,名称输入你命名空间的名称

bash

代码解读

E:\dev\gpg2\GnuPG\bin>gpg --gen-key

gpg (GnuPG) 2.4.5; Copyright (C) 2024 g10 Code GmbH

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Note: Use "gpg --full-generate-key" for a full featured key generation dialog.

GnuPG needs to construct a user ID to identify your key.

Real name: wangfugui-ma

Email address: masiyi163163@163.com

You selected this USER-ID:

"wangfugui-ma "

Change (N)ame, (E)mail, or (O)kay/(Q)uit? o

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

gpg: revocation certificate stored as 'C:\\Users\\admin\\AppData\\Roaming\\gnupg\\openpgp-revocs.d\\4CB3D9314CD5F1277582A11F4ADBA3851D627E38.rev'

public and secret key created and signed.

pub ed25519 2024-03-17 [SC] [expires: 2027-03-17]

4CB3D9314CD5F1277582A11F4ADBA3851D627E38

uid wangfugui-ma

sub cv25519 2024-03-17 [E] [expires: 2027-03-17]

这里会弹出一个让你输入密钥并二次确认的窗口,记住你的密码,后面发布的时候会用。

发布秘钥

上面 4CB3D9314CD5F1277582A11F4ADBA3851D627E38 就是你的密钥id

gpg --keyserver keyserver.ubuntu.com --send-keys 4CB3D9314CD5F1277582A11F4ADBA3851D627E38

这里发布到keyserver.ubuntu.com服务器上,这样中央仓库也有你的密钥,所以它才能验证你的身份

As SKS Keyserver Network is being deprecated we recommend the use an specific GPG keyserver. Current GPG Keyservers supported by Central Servers are: keyserver.ubuntu.com keys.openpgp.org pgp.mit.edu

如果发布失败开源用上面的其他两个地址

验证秘钥

验证秘钥是否发布成功:

gpg --keyserver keyserver.ubuntu.com --recv-keys 4CB3D9314CD5F1277582A11F4ADBA3851D627E38

出现下面内容则说明发布成功

E:\dev\gpg2\GnuPG\bin>gpg --keyserver keyserver.ubuntu.com --recv-keys 4CB3D9314CD5F1277582A11F4ADBA3851D627E38

gpg: key 4ADBA3851D627E38: "wangfugui-ma " not changed

gpg: Total number processed: 1

gpg: unchanged: 1

发布jar包

第一步,编辑pom文件

在你的项目pom文件中加入以下的内容:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

io.gitee.wangfugui-ma

aliyun-oss-spring-boot-starter

0.0.1

aliyun-oss-spring-boot-starter

aliyun-oss-spring-boot-starter

https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter

The Apache Software License, Version 2.0

http://www.apache.org/licenses/LICENSE-2.0.txt

masiyi

masiyi

masiyi163163@163.com

Project Manager

Architect

https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter.git

scm:git:ssh://git@gitee.com:wangfugui-ma/aliyun-oss-spring-boot-starter.git

https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter

UTF-8

org.springframework.boot

spring-boot-autoconfigure

2.2.9.RELEASE

org.slf4j

slf4j-api

2.0.4

com.aliyun.oss

aliyun-sdk-oss

3.17.2

org.projectlombok

lombok

1.18.30

provided

org.sonatype.central

central-publishing-maven-plugin

0.4.0

true

masiyi

true

org.apache.maven.plugins

maven-source-plugin

2.2.1

attach-sources

jar-no-fork

org.apache.maven.plugins

maven-javadoc-plugin

2.9.1

attach-javadocs

jar

org.apache.maven.plugins

maven-gpg-plugin

1.5

E:\dev\gpg2\GnuPG\bin\gpg.exe

wangfugui-ma这里必须得修改

sign-artifacts

verify

sign

除了dependencies里面的内容是根据你的项目里面的实际情况写,其他的都必须写,否则会上传失败。

第二步,打包上传

mvn 先clean 后deploy

这个时候会让你输入你在开头设置的密码,输入成功开始打包上传。

打包成功

第三步,发布jar包

打包成功之后Deployments模块里面也有对应的步骤,刚刚我们在idea控制台看到的id是:50706b21-6fd9-4017-9b43-31e8d05b8566也就对应这个页面里面的Deployment ID

这个时候我们点击Publish意思就是将我们的jar包发布了

第四步,搜索我们的jar包

可以看到成功搜索到了我们发布的jar包

注意事项

publishingServerId 里面的值要对应setting文件里面的id之前有gpg秘钥的时候请先导出,如何把原来的删除,否则会一直验证失败使用mvn clean deploy 命令的时候请指定setting文件地址,如果在idea打包则不需要Generate User Token 如果重新生成,之前的就会用不了,会报401错误使用gpg生成秘钥的时候名称一定要填命名空间里面的名称,否则打包会一直验证失败一定要用我发出来的pom文件里面的结构,否则会打包失败

三天验证,打包快100次失败写出来的教程,希望大家不吝啬手中的三连!!!

作者:掉头发的王富贵 链接:https://juejin.cn/post/7347207466818289703 来源:稀土掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。