自定义组件上传到maven中央仓库2024实测可用最详细版

自 2024 年 3 月 12 日起,官方调整了发布的方式,所有发布都必须通过中央门户,以往老方式可能不适用,以下记录2024新版上传发布方式

注册sonatype账号

Maven中央仓库并不支持直接发布jar包,sonatype是其指定的第三方仓库之一,发布到此的项目会被定时同步到中央仓库
官方教程地址:https://central.sonatype.org/register/central-portal/

1.访问网址:https://central.sonatype.com/,并点击右上角访问登陆界面

在这里插入图片描述

2.注册sonatype账号

(1)自行创建账号方式【创建好账号后点击新建命名空间,建好后会提示绑定自己的域名-这里的域名需要反向填写如下图,
并解析提供的DNS记录值。此方法适合想用自己域名定义groupId并乐于折腾的,新手建议参考方法(2)】

在这里插入图片描述

在这里插入图片描述

(2)gitee/github账号直接登录【适合新手,直接用git的域名方便。github的用户名如下图】

在这里插入图片描述

根据gitee/github新建namespace秒通过【格式为io.github.用户名】

在这里插入图片描述

3.获取User Token并配置本地maven

在这里插入图片描述
在这里插入图片描述

(1)点击确定后会生成如下信息,将信息复制到本地maven-config-settings.xml的<servers>标签中。用于上传jar包时验证身份使用。

在这里插入图片描述
在这里插入图片描述

4.安装GPG并生成密钥【官网说明上传时必须使用GPG签名,需要下载软件获取签名】

(1)官网地址:https://gnupg.org/download/index.html
(2)进入网站后下拉选择windows版本下载

在这里插入图片描述
在这里插入图片描述

(3)点击0捐助白嫖下载,想捐助的也可以

在这里插入图片描述

(4)无脑下一步安装即可
(5)安装完成后会有两个文件夹,cmd进入GnuPG的bin目录,输入`gpg --version`验证是否安装成功

在这里插入图片描述

在这里插入图片描述

(6)生成密钥:命令行输入gpg --gen-key,按提示依次输入RealName、email,确认无误输入O之后会提示输入密码。
此密码需要记住,上传仓库时需要使用

在这里插入图片描述

输入密码后,中间这串就是生成的密钥。如果关闭cmd窗口则可以使用gpg --list-keys命令查询已生成的密钥。

在这里插入图片描述

上述生成密匙也可点击桌面图标进入图形化程序操作,获取密匙id即可

在这里插入图片描述

(7)发布密匙验证,cmd输入gpg --keyserver keyserver.ubuntu.com --send-keys 密匙ID。
或者在图形化界面选择一个证书点击右键选择`在服务器上发布`
中央服务器当前支持的 GPG 密钥服务器有(如果遇到报错换着来试试):

keyserver.ubuntu.com
keys.openpgp.org
pgp.mit.edu

在这里插入图片描述

(8)发布完成后,将密匙配置到本地maven的settings.xml的<profiles>标签中,并添加<activeProfiles>标签
 </profiles>
   <profile>
        <id>gpg</id>
        <properties>
        	<!--这里为gpg安装目录>
            <gpg.executable>D:\gpg4\GnuPG\bin</gpg.executable>
            <gpg.passphrase>{证书密码}</gpg.passphrase>
        </properties>
    </profile>
  </profiles>

在这里插入图片描述

注:<profile>标签中的<id>标签值需要和<activeProfile>标签值对应

5.修改要发布的pom.xml文件【这里默认已写好了要上传的代码】

<?xml version="1.0" encoding="UTF-8"?>
<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不能自定义,需要与sonatype网站建好的namespace名称对应,有多个namespace无所谓,选择一个即可
	,否则上传失败一直报not allow groupId -->
    <groupId>io.github.955-955</groupId>
    <artifactId>xxxx-spring-boot-starter</artifactId>
    <version>1.1-beta</version>
    <packaging>jar</packaging>
    <name>名称</name>
    <description>描述</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version>
    </parent>
    <licenses>
        <license>
            <name>Apache License 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <!--  填写自己的git地址  -->
    <scm>
        <connection>scm:git:git://github.com:955-955</connection>
        <developerConnection>scm:git:git@github.com:955-955</developerConnection>
        <url>https://github.com/955-955</url>
        <tag>HEAD</tag>
    </scm>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--  所需依赖  -->
    </dependencies>
    	<!--  以下插件必须有  -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- Source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.1.0</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludeResources>true</excludeResources>
                    <useDefaultExcludes>true</useDefaultExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.6.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- GPG -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <!-- 改成你自己的路径 -->
                    <executable>D:\gpg4\GnuPG\bin\gpg.exe</executable>
                </configuration>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <!-- 这里的serverId是之前在本地settings.xml中配置的<server><id>标签值(上述三(1)步骤) -->
                    <publishingServerId>xxxxx</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

6.点击deploy后输入密码即可自动上传,上传成功后会在网站显示是否有错误,有错误修改,没错误则点击publish发布等待提示published,大约10分钟后会发布成功可以查询到,阿里镜像和中央仓库会稍后自动同步。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

完成后即可引入坐标使用

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值