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

在官方的文档中提到:

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

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

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

账号准备

第一步,注册账号

https://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 文件里面

<server>
	<id>${server}</id>
	<username>rRdszgHD</username>
	<password>wqKmkox2x2y2+yA/3ER/TYwX0eQoBOJYZSVLcnegAVgi</password>
</server>

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

在这里插入图片描述

GPG准备

第一步,下载GPG

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

https://gnupg.org/download/index.html

在这里插入图片描述

第二步,生成秘钥

在这里插入图片描述

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

生成秘钥

输入gpg --gen-key

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


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 <masiyi163163@163.com>"

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 <masiyi163163@163.com>
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 <masiyi163163@163.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

发布jar包

第一步,编辑pom文件

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <groupId>io.gitee.wangfugui-ma</groupId>
    <artifactId>aliyun-oss-spring-boot-starter</artifactId>
    <version>0.0.1</version>
    <name>aliyun-oss-spring-boot-starter</name>
    <description>aliyun-oss-spring-boot-starter</description>
    <url>https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter</url>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>masiyi</id>
            <name>masiyi</name>
            <email>masiyi163163@163.com</email>
            <roles>
                <role>Project Manager</role>
                <role>Architect</role>
            </roles>
        </developer>
    </developers>


    <scm>
        <connection>https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter.git</connection>
        <developerConnection>scm:git:ssh://git@gitee.com:wangfugui-ma/aliyun-oss-spring-boot-starter.git
        </developerConnection>
        <url>https://gitee.com/wangfugui-ma/aliyun-oss-spring-boot-starter</url>
    </scm>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!--   central发布插件    -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>masiyi</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
            <!--   source源码插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--   javadoc插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</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>1.5</version>
                <configuration>
                    <executable>E:\dev\gpg2\GnuPG\bin\gpg.exe</executable>
                    <keyname>wangfugui-ma</keyname>
                </configuration>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

除了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次失败写出来的教程,希望大家不吝啬手中的三连!!!

在这里插入图片描述

在这里插入图片描述

  • 37
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
上传jar包Maven 仓库需要执行以下步骤: 1. 在本地 Maven 仓库中安装 jar 包 在命令行中执行以下命令: ``` mvn install:install-file -Dfile=path/to/your.jar -DgroupId=your.group.id -DartifactId=your-artifact-id -Dversion=your-version -Dpackaging=jar ``` 其中,`path/to/your.jar` 是你要上传的 jar 包路径,`your.group.id` 、`your-artifact-id` 和 `your-version` 分别代表你的 Maven 项目的 Group ID、Artifact ID 和版本号。 2. 配置 Maven 项目的 pom.xml 文件 在 Maven 项目的 `pom.xml` 文件中添加以下配置: ``` <repositories> <repository> <id>your-repo-id</id> <url>https://your.repo.url</url> </repository> </repositories> <dependencies> <dependency> <groupId>your.group.id</groupId> <artifactId>your-artifact-id</artifactId> <version>your-version</version> </dependency> </dependencies> <distributionManagement> <repository> <id>your-repo-id</id> <url>https://your.repo.url</url> </repository> </distributionManagement> ``` 其中,`your-repo-id` 和 `https://your.repo.url` 分别代表你的 Maven 仓库的 ID 和 URL。 3. 配置 Maven 的 settings.xml 文件 在 Maven 的 `settings.xml` 文件中添加以下配置: ``` <servers> <server> <id>your-repo-id</id> <username>your-username</username> <password>your-password</password> </server> </servers> ``` 其中,`your-username` 和 `your-password` 分别代表你的 Maven 仓库的用户名和密码。 4. 执行 Maven 的 deploy 命令 在命令行中执行以下命令: ``` mvn deploy ``` 这将会把你的 jar 包上传到 Maven 仓库中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

掉头发的王富贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值