休闲游戏

如何发布jar包到新版maven中央仓库(2025年1月最新保姆级教程)

如何发布jar包到新版maven中央仓库(2025年1月最新保姆级教程)

前言

作者博客: www.lstar.icu

开源地址 Gitee 地址: https://gitee.com/lxwise Github 地址: https://github.com/lxwise

在官方文档中提到:

从 2024 年 3 月 12 日起,所有注册都将通过中央门户进行。有关旧版注册的信息,请参阅相关文档。有关在 Portal Early Access 期间切换到通过旧版 OSSRH 发布方法发布的支持,请参阅以下部分。

1.准备账户

1.1 注册账号

1.1.1 进入中央仓库官网

官网地址

1.1.2 点击Sign In

点击右上角Sign In,选择Continue with GitHub,一定要选择这个选项,这样后面会省很多事情

注册完成样子

1.2 新建名称空间

依次点击Publish ----->进入后选择Namespace

因为这里博主直接用的Github账号登录的,所以直接就给我创建好了一个Namespace名称空间,如果你的没有,请点击Add Namespace自己创建一个。

如果你的代码存在Gitee上,你就需要把命名空间设置为io.gitee.yourname。这里的yourname是你的gitee名称,例如博主的是https://gitee.com/lxwise所以我的命名空间是:io.gitee.lxwise

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

这里 一定要按照这个格式来创建,否则发布时会无法校验通过!

1.3 验证名称空间

如果你是Github账号登录的,请忽略这一步。

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

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

1.4 创建发布账密

新版本抛弃了原来固定的username和password,选择token密钥认证,这个username和password用来配置你的本地maven并push你的jar包到中央仓库里面去的凭证只有在创建成功的时候才会显示一次,以后都不会显示了,所以一定要保存好。

1.4.1 点击右上角的view account

1.4.2 点击Generate User Token

1.4.3 点击ok

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

${server}

Iasdasoas

gadsd1xcsd+yA/3AR/jasasfasdqwqsadasdga

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

1.4.4 设置setting

打开你本地maven的setting配置文件,找到servers模块,把上面这串粘贴到servers模块里面的。

ossrh

Iasdasoas

gadsd1xcsd+yA/3AR/jasasfasdqwqsadasdga

到这里账号相关的就完成了。

2.准备密钥工具

2.1 下载GPG工具

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

下载地址: https://gnupg.org/download/index.html

下拉找到你电脑对应的版本,作者这里是window系统就选择下载的windows版本的。

下载完成后,无脑下一步安装就行,但要记住自己的安装位置后面会用到。

2.2 生成密钥

去你自己的安装路径的bin路径下,进入cmd命令行

2.2.1 生成密钥

输入:

gpg --gen-key

提示输入名字和邮箱名称输入你命名空间的名称即可。输入后,再输入O,回车。然后会弹出要输入密码,这里会弹出一个让你输入密钥并二次确认的窗口,记住你的密码,后面发布的时候会用。(密码是为了保护私钥用的)

D:\develop\GnuPG\bin> gpg --gen-key

gpg (GnuPG) 2.4.7; 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: lxwise

Email address: lxstart@126.com

You selected this USER-ID:

"lxwise "

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\ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA.rev'

public and secret key created and signed.

pub ed25519 2025-01-10 [SC] [expires: 2028-01-10]

ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA

uid lxwise

sub cv25519 2025-01-10 [E] [expires: 2028-01-10]

2.2.2 发布秘钥

注意:下面的密钥请替换为你自己的!!!

上面 ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA 就是你的密钥id,

gpg --keyserver keyserver.ubuntu.com --send-keys ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA

这里发布到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

keyserver.ubuntu.com

keys.openpgp.org

pgp.mit.edu

2.2.3 验证密钥

等几分钟,验证公钥是否发布成功:

gpg --keyserver keyserver.ubuntu.com --recv-keys ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA

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

D:\develop\GnuPG\bin>gpg --keyserver keyserver.ubuntu.com --recv-keys ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA

gpg: key 38D7F32BA88EEB01: "io.github.lxwise " not changed

gpg: Total number processed: 1

gpg: unchanged: 1

D:\develop\GnuPG\bin>

备注:

忘记了公钥,也可以通过gpg --list-keys查看当前所有的公钥

2.3 密钥导入导出

作为程序员肯定有在不同的电脑上操作时候,这里我们可以直接用现成的密钥。这就需要用导出导入功能。

2.3.1 导出

导出公钥,命令格式:

gpg -a -o public-file.key --export keyId : 导出公钥keyId 到 文件 public-file.key中;

-a 为 --armor 的简写,表示密钥以ASCII的形式输出,默认以二进制的形式输出; -o 为 --output 的简写,指定写入的文件;

例如:

gpg -a -o public-file.key --export ACB3D93GFSCD5F128512A11F4ADBA3851FA12FA

导出私钥(导出私钥需要输入之前设置的保护私钥密码),命令格式:

gpg -a -o private-file.key --export-secret-keys 6C1D0E2XXXXXXXX7CE3EC423

2.3.2 导入

导入公钥,命令格式:

gpg --import public-file.key

导入私钥(导入私钥需要输入保护私钥的密码),命令格式:

gpg --import private-file.key

2.3.3 使用图形化界面

我们在安装GPG工具时会带有一个kleopatra工具,提供了签名、导入、导出 、证书列表等等一系列的功能,不想敲命令的也可以直接用图形化界面操作,效果是一样的。

3.发布jar包

3.1 修改项目pom.xml文件

在你的项目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.github.lxwise

fx-updater

1.0.1

jar

Automatic update launcher for JavaFX applications.

fx-updater

https://github.com/lxwise/fx-updater

The Apache Software License, Version 2.0

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

ossrh

lxwise

lstart980@gmail.com

Project Manager

Developer

https://github.com/lxwise/fx-updater.git

scm:git:ssh://git@github.com:lxwise/fx-updater.git

https://github.com/lxwise/fx-updater

21

21

UTF-8

23.0.1

org.openjfx

javafx-controls

${javafx.version}

org.openjfx

javafx-base

${javafx.version}

org.openjfx

javafx-fxml

${javafx.version}

org.openjfx

javafx-web

${javafx.version}

com.google.code.gson

gson

2.10

org.apache.maven.plugins

maven-resources-plugin

3.2.0

UTF-8

src/main/resources

false

org.sonatype.central

central-publishing-maven-plugin

0.4.0

true

ossrh

true

org.apache.maven.plugins

maven-source-plugin

2.2.1

attach-sources

jar-no-fork

org.apache.maven.plugins

maven-javadoc-plugin

3.1.1

UTF-8

UTF-8

UTF-8

-Xdoclint:none

attach-javadocs

jar

org.apache.maven.plugins

maven-gpg-plugin

1.6

D:\develop\GnuPG\bin\gpg.exe

lxwise

sign-artifacts

verify

sign

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

3.2 打包并上传到中央仓库

执行下面的指令:

mvn clean deploy

如果使用的是Idea等工具可以直接按下面操作

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

打包成功

打包失败

如果有失败请查看控制台的详细日志解决,如果是Publish失败,可以在中央仓库的Publish–>Deployments查看是否有相关错误信息。

3.3 发布jar包

打包成功之后Deployments模块里面也有对应的步骤,刚刚我们在idea控制台看到的id是:8f266d74-d332-4517-83a7-d4a36c4dbeb6也就对应这个页面里面的Deployment ID

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

3.4 使用jar包

还是进入官网地址,搜索你自己的jar,即可获取使用了,这里要注意名称空间是不是你自己的。

4.注意事项

有过gpg秘钥的时候请先导出,并把原来的删除,否则会一直验证失败,这里作者踩了半天的坑一直签名失败,血一般的教训啊。

publishingServerId 里面的值要对应setting文件里面的id

ossrh

ossrh

Iasdasoas

gadsd1xcsd+yA/3AR/jasasfasdqwqsadasdga

使用gpg生成秘钥的时候名称一定要填命名空间里面的名称,否则打包会一直验证失败。

版本号不要用快照SNAPSHOT,否则最后发布时一直会失败。

总结

第一次发布时,上边所有步骤都要做。以后再发布时,只操作第3步即可!