上传本地项目至MAVEN中央库全教程

这段时间整理了下平时自己的一些工具类,也为了以后避免重复造轮子,想着干脆封装成一个项目传到MAVEN中央库去,方便以后使用。借鉴了网上一些前辈写的教程,折腾了接近两天终于完活了。

第一步,新建一个gitee项目

如果有自己的域名,可以直接跳过这一步,没有就用gitee或者github的域名,我这边用的gitee上的仓库。

  • 仓库的权限需要设置为公开
    在这里插入图片描述

第二步,sonatype

网站链接:https://issues.sonatype.org/secure/Dashboard.jspa

1、注册登录进去
在这里插入图片描述

2、新建一个项目
在这里插入图片描述

3、创建一个问题

  • 项目:Community Support
  • 问题类型:New Project
  • 概要:随便填
  • Group Id:填自己的域名,或者gitee/github的域名:com.gitee.个人域名
  • Project URL:填第一步中在gitee创建的仓库的地址:https://gitee.com/域名/仓库名
  • SCM url:项目的HTTPS:https://gitee.com/域名/仓库名.git
    在这里插入图片描述

3、提交以后会有个专员跟进这个问题,注意以下这个问题的状态

4、如果使用gitee的方式,专员会让你验证这个仓库是否属于你,具体的操作是:让你新建一个以他给出的名字命令的仓库,并且设为公开

在这里插入图片描述

5、不久,就会收到回复
在这里插入图片描述
6、至此,sonatype这边的事情就结束了

第三步,下载安装Gpg

1、去官网下载并安装,这边采用控制台的方式
控制台版本: https://www.gnupg.org/download/
可视化版本: https://www.gpg4win.org/

2、安装完成后,在控制台执行gpg2 --gen-key开始创建秘钥,之后会让你输入用户名、邮箱信息,"O"确认

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

Real name: 用户名
Email address: 邮箱
You selected this USER-ID:
    "用户名<邮箱>"

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

3、密钥创建的过程中会让你输入一个密码,这个密码务必记住
在这里插入图片描述

4、密钥创建成功

gpg: key ***************marked as ultimately trusted
gpg: directory '/Users/will/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/Users/will/.gnupg/openpgp-revocs.d/********************************.rev'
public and secret key created and signed.

pub   rsa3072 2021-11-10 [SC] [expires: 2023-11-10]
      ***************************密钥*****************************
uid                      *******<**************>
sub   rsa3072 2021-11-10 [E] [expires: 2023-11-10]

5、上传密钥

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys *******************************************************

6、验证密钥

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys *******************************************************

7,至此,Gpg部分完成

第四步, 本地MAVEN配置

1、找到本地的maven的配置文件 settings.xml

--在servers标签下添加以下内容
<server>
        <id>oss</id>
        <username>sonatype中注册用户名(不是邮箱)</username>
        <password>密码</password>
</server>
--在profiles便签下添加以下内容
<profile>
      	<id>oss</id>
      	<activation>
        	<activeByDefault>true</activeByDefault>
      	</activation>
      	<properties>
        	<gpg.executable>gpg</gpg.executable>
        	<gpg.passphrase>在第三步gpg中输入的密码</gpg.passphrase>
      	</properties>
</profile>

2、idea的默认maven的路径是在安装的目录里面;如 D:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\plugins\maven\lib\maven3\conf

第五步,本地项目中的配置和操作

1、修改pom.xml文件

  • 主要就是添加url、licenses、scm、developers、distributionManagement等节点
  • 下面的几个插件时必须的,否则后期提交到中央库的时候,无法通过校验

需要注意的是,我们上传的远程仓库的地址是带s01前缀的,而比较旧的文章中不带s01前缀,这是因为旧的sonatype maven仓库已经资源满载了,所以官方新建了一个s01 maven仓库,现阶段(2023/9/12)在sonatype JIRA管理平台上注册的用户,只能上传这个新的s01仓库。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>第二步中的groupId</groupId>
    <artifactId>zpp-util-public</artifactId>
    <version>0.0.1.release</version>
    <name>zpp-util-public</name>
    <description>项目描述</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <projectUrl>https://gitee.com/XXXXXX/XXXXXX.git</projectUrl> //在第一步中创建的仓库地址
        <serverId>oss</serverId>
    </properties>
    <developers>
        <developer>
            <name>作者名</name>
            <email>邮箱</email>
            <url>${projectUrl}</url>
        </developer>
    </developers>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--以下部分内容不需要修改,直接复制咱贴即可-->
    <url>${projectUrl}</url>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo,manual</distribution>
        </license>
    </licenses>


    <scm>
        <!-- 采用projectUrl变量代替这个值,方便给重复利用这个配置,也就是上面的标签替换一下值就行 -->
        <connection>${projectUrl}</connection>
        <developerConnection>${projectUrl}</developerConnection>
        <url>${projectUrl}</url>
    </scm>


    <distributionManagement>
        <snapshotRepository>
            <!--这个id和settings.xml中servers.server.id要相同,因为上传jar需要登录才有权限-->
            <id>${serverId}</id>
            <name>OSS Snapshots Repository</name>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <!--这个id和settings.xml中servers.server.id要相同,因为上传jar需要登录才有权限-->
            <id>${serverId}</id>
            <name>OSS Staging Repository</name>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
    <build>
        <plugins>
            <!-- 编译插件,设置源码以及编译的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <!--打包源码的插件-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.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>3.3.0</version>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--公钥私钥插件,也就是上传需要进行验证用户名和密码过程中需要用到的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--部署插件-->
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.7</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>${serverId}</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>false</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2、打包并上传,直接使用idea右侧的打包工具
在这里插入图片描述
3、只要出现了build Sucess字样就说明上传暂存库成功了

第六步, 登录nexus Repository Manager

1、登录暂存库 https://s01.oss.sonatype.org/ ,账号密码和 sonatype 的一样

2、登录成功后,点击下图红框中的Staging Repositories
在这里插入图片描述

3、找到刚才的上传记录,选中并点击 close按钮
在这里插入图片描述

4、等待一段时间后,如果close报错,查看下方的 Activity 中的错误日志,一般就是必须的文件缺失,从而没有通过校验,去看看打包上传的时候有没有问题

5、如果close成功了就说明校验成功了,就可以正式发布到中央仓库去了
在这里插入图片描述

第七步,查看镜像

1、一般在4个小时左右到官方镜像库,到国内的镜像库时间就说不准了
2、https://central.sonatype.com/search

第八步,以后的更新

以后更新不需要这么麻烦了,只要用这个的groupId,修改下版本号,就可以打包提交了,然后重复Close/Release操作就行了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值