上传jar包到maven中央仓库

Published on 2023-10-09 13:14 in 分类: 博客 with 狂盗一枝梅
分类: 博客

上传jar包到中央仓库的好处太多了。。此处省略2000字,接下来进入正文

一、注册sonatype账号

官网:https://issues.sonatype.org

为了方便后续操作,注册完成以后,修改下首选语言为中文:右上角头像->Profile->Summary->Preferences,点击旁边的修改图标即可

image-20231009082520682

二、新建issue

样例issue:https://issues.sonatype.org/browse/OSSRH-95628

image-20231009085142498

特别需要注意的是groupId选项,我们都知道groupId非常重要,所以sonatype会验证你是否对该groupId拥有所有权,根据不同的情况,sonatype会有不同的验证方式,所以groupId的选择要慎重。

三、验证groupId

当我们新建完issue几分钟之后,sonatype机器人会在该issue下发一条评论要求验证groupId,之后会将该issue的状态更改为waiting状态,我们按照它的要求验证完成后,将issue状态更改为open,等待sonatype响应才能进行下一步。

image-20231009091740284

1、有自己的域名

需要证明自己对该域名有所有权,按照它的操作文档:https://central.sonatype.org/faq/how-to-set-txt-record/ ,不同的域名厂商,都有不同的操作文档,我的是阿里云的域名,操作文档在这里:https://www.alibabacloud.com/help/zh/alibaba-cloud-dns/latest/create-a-dns-record#h2-txt-5

完成操作之后,使用命令nslookup -type=TXT yourdomain.com 验证是否已经生效

image-20231009092423898

若已经生效,则将issue状态更改为"open",等待sonatype下一步提示即可。

2、没有自己的域名

没有自己的域名,国内用户大多数都使用gitee或者github,如果想借用github或者gitee的名头发布jar包,则需要更改groupId的名字为com.github.xxx或者io.gitee.xxx,具体要以sonatype的提示为准,其验证方式大概就是建一个指定名字的仓库,完成后将此issue的状态更改为open,然后等待sonatype的响应。

image-20231009093256411

四、验证s01.oss.sonatype.org

如果没有意外,验证groupId成功后,会有一条新的comment

image-20231009093608665

这段话的意思是,我们已经可以发布正式构件到s01.oss.sonatype.org了,发布指南:https://central.sonatype.org/publish/publish-guide/#deployment ;第一个构件发布成功以后,在这个issue下会有一条新的comment,如果没有看到新的comment,则看下文档:https://central.sonatype.org/publish/release/

s01.oss.sonatype.org 这个域名是新的域名,在收到这条comment的时候,我们的账号就已经在s01.oss.sonatype.org上开通了,账号密码就是注册 issues.sonatype.org 的账号密码。尝试登陆下,如果能成功,说明我们一切已经就绪,就等待上传jar包了

image-20231009094614278

五、安装gnupg

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

gnupg for windows官网:https://gpg4win.org/download.html (大多数的选择)

下载并安装好之后,桌面上会有这么个图标

image-20231009100226011

打开它,新建gnupg密钥对

image-20231009100521472

如果使用了密码句(passphrase),一定要记住这个密码。

生成后,右键,在服务器上发布,将公钥发布到服务器上,后续sonatype会根据该公钥验证我们发布的jar包签名是否正确。

image-20231009100907267

然后,在服务器上查找,如果能找到,说明公钥发布成功

image-20231009101150868

六、修改settings文件

新增如下配置项

	
<!-- Sonatype的用户名密码 -->
 <server>
     <id>ossrh</id>
     <username>userName</username>
     <password>password</password>
 </server>

......

  <profiles>
   <profile>
   <id>gpg</id>
		<properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>your passphrase</gpg.passphrase>
      </properties>
	<repositories>
		<repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <updatePolicy>always</updatePolicy>
                <enabled>true</enabled>
            </snapshots>
        </repository>
      </repositories>
    </profile> 
  </profiles>
  <activeProfiles>
    <activeProfile>gpg</activeProfile>
  </activeProfiles>

七、修改pom文件

新增如下元素,下面的元素几乎都是不可缺少的,按照下面的模板配置即可

<name>kdyzm-json-util</name>
<description>封装了jackson组件,修改spring-boot默认的序列化反序列化,同时暴露出相同规则的工具类</description>
<url>https://gitee.com/kdyzm/kdyzm-json-util</url>

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

<scm>
    <url>https://gitee.com/kdyzm/kdyzm-json-util.git</url>
</scm>

<developers>
    <developer>
        <name>kdyzm</name>
        <email>kdyzm@foxmail.com</email>
        <organization>kdyzm</organization>
        <organizationUrl>http://www.kdyzm.cn</organizationUrl>
    </developer>
</developers>

......

<distributionManagement>
    <snapshotRepository>
        <id>ossrh</id>
        <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
    <repository>
        <id>ossrh</id>
        <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    </repository>
</distributionManagement>

<profiles>
    <profile>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <id>release</id>
        <build>
            <plugins>
                <plugin>
                    <!-- 上传源码 -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.0.1</version>
                    <configuration>
                        <attach>true</attach>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>jar</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>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- nexus插件 -->
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.6.7</version>
                    <extensions>true</extensions>
                    <configuration>
                        <serverId>ossrh</serverId>
                        <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                        <autoReleaseAfterClose>false</autoReleaseAfterClose>
                    </configuration>
                </plugin>
                <!-- gpg插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.5</version>
                    <executions>
                        <execution>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

八、上传jar包

首先,检查下项目版本号,由于发布的是正式版构件,所以它的版本号不应该以-SNAPSHOT结尾,如果是-SNAPSHOT结尾,会被误认为是快照版本,关于快照版本的jar包,我们后续再谈。

运行命令mvn clean deploy上传jar包

image-20231009103348108

差不多这个样子就表示上传成功了,然后打开网址:https://s01.oss.sonatype.org/#stagingRepositories

image-20231009103553218

先检查下content,确保上传的jar包没有问题

image-20231009104135009

如果有问题,选择Drop按钮,删除掉这次的上传内容,回去检查内容,重新上传;如果没有问题,先点击close,完成后点击Release

image-20231009104333235

release的时候这个框是默认勾选的,最好不要改,由于这是个暂存库,所以最终这些东西都是要删除的,这样就不用手动删除了。

完成之后,我们最原始的提的那个issue,会收到一个新的comment

image-20231009104458641

意思是我们已经成功发布了构件,构件将会在半小时内发布到 https://repo1.maven.org/maven2 ,在4个小时内可以在 https://search.maven.org 搜索到,实际体验下来,其实大概也就十来分钟,https://repo1.maven.org/maven2https://search.maven.org 就都有了。

验证下

image-20231009104854614

image-20231009104946213

至此,jar包上传就已经成功了。

九、一些疑问解答

1、能否上传snapshot版本jar包

答:能,但是有限制,具体文档如下:https://central.sonatype.org/publish/publish-maven/#performing-a-snapshot-deployment ,快照版本的jar包可以上传,但是不会被同步到中央仓库,如果想使用快照版本的jar包,需要额外引入repository:https://s01.oss.sonatype.org/content/repositories/snapshots/

在setting.xml文件中新增如何配置即可使用(之前配置已经配过的就不用配了)

<repository>
    <id>ossrh</id>
    <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
    <snapshots>
        <updatePolicy>always</updatePolicy>
        <enabled>true</enabled>
    </snapshots>
</repository>

2、有新的项目还需要重新提issue吗

答:不需要,只要groupId不变,artifactId就算是新的,也可以直接上传。

我觉得实际上在sonatype上提的issue最大的作用就是验证sonatype上自己的账号是否对对应的groupId所对应的域名或者代码托管平台上的账号有管理权,一旦验证过了,就不需要再验证了,在同一个groupId下自己就可以随心所欲的整活了

END


#maven
目录