ToB企服应用市场:ToB评测及商务社交产业平台

标题: 发布jar包到maven中央仓库 [打印本页]

作者: 铁佛    时间: 2024-2-15 02:43
标题: 发布jar包到maven中央仓库
1. 环境

在网上找的很多文章中写得都有很多问题,这里记录一下最近一次成功地发布jar包到maven中央仓库的过程。并附带上每一个步骤官方的指导链接。

2. 项目配置

官方对发布的项目做了一些必需的要求,如果不按要求来,发布可能会报错。
2.1. javadoc和source

以非pom方式打包的项目(pom.xml配置文件中可以通过标签配置打包方式,默认就是jar),在打包完成后,target目录下必须要包含***-sources.jar和***-javadoc.jar。如下图:
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211113242420-1891198919.jpg[/img]
为了达成以上效果,需要在pom.xml中依赖两个插件:
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.apache.maven.plugins</groupId>
  5.             <artifactId>maven-source-plugin</artifactId>
  6.             <version>2.2.1</version>
  7.             <executions>
  8.                 <execution>
  9.                     <id>attach-sources</id>
  10.                     <goals>
  11.                         <goal>jar-no-fork</goal>
  12.                     </goals>
  13.                 </execution>
  14.             </executions>
  15.         </plugin>
  16.         <plugin>
  17.             <groupId>org.apache.maven.plugins</groupId>
  18.             <artifactId>maven-javadoc-plugin</artifactId>
  19.             <version>2.9.1</version>
  20.             <executions>
  21.                 <execution>
  22.                     <id>attach-javadocs</id>
  23.                     <goals>
  24.                         <goal>jar</goal>
  25.                     </goals>
  26.                 </execution>
  27.             </executions>
  28.         </plugin>
  29.     </plugins>
  30. </build>
复制代码
参考:https://central.sonatype.org/publish/requirements/#supply-javadoc-and-sources
2.2. 使用 GPG/PGP 签署文件

打包完成后,target目录下必须要包含***.jar.sac。如下图:
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211133726358-776036640.jpg[/img]
为了达成以上效果,需要在pom.xml中依赖插件(加完插件不要急着构建,还要下载GPG生成密钥,后面会说):
  1. <build>
  2.     <plugin>
  3.         <groupId>org.apache.maven.plugins</groupId>
  4.         <artifactId>maven-gpg-plugin</artifactId>
  5.         <version>3.1.0</version>
  6.         <executions>
  7.             <execution>
  8.                 <id>sign-artifacts</id>
  9.                 <phase>verify</phase>
  10.                 <goals>
  11.                     <goal>sign</goal>
  12.                 </goals>
  13.             </execution>
  14.         </executions>
  15.     </plugin>
  16. </build>
复制代码
参考:https://central.sonatype.org/publish/requirements/#sign-files-with-gpgpgp
2.3. 项目名称和版本配置

[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211140047674-519762873.jpg[/img]
参考:https://central.sonatype.org/publish/requirements/#correct-coordinates
2.4. 项目名称、描述和 URL

这个是为了增加可读性。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211140910624-1677314503.jpg[/img]
参考:https://central.sonatype.org/publish/requirements/#project-name-description-and-url
2.5. 许可证书

可以使用Apache/MIT的许可证书,直接拷贝下面的即可:
  1. <licenses>
  2.         <license>
  3.         <name>The Apache Software License, Version 2.0</name>
  4.         <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
  5.     </license>
  6. </licenses>
复制代码
参考:https://central.sonatype.org/publish/requirements/#license-information
2.6. 开发人员信息

留你自己的艺名和邮箱:
  1. <developers>
  2.         <developer>
  3.         <name>你的艺名</name>
  4.         <email>你的邮箱</email>
  5.     </developer>
  6. </developers>
复制代码
参考:https://central.sonatype.org/publish/requirements/#developer-information
2.7. SCM 源码管理系统信息

可以使用github(或gitee)的项目地址。
  1. <scm>
  2.     <connection>scm:git:git://github.com:maidoubaobao/easy-tool.git</connection>
  3.     <developerConnection>scm:git:ssh://github.com:maidoubaobao/easy-tool.git</developerConnection>
  4.     <url>https://github.com/maidoubaobao/easy-tool/tree/master</url>
  5. </scm>
复制代码
参考:https://central.sonatype.org/publish/requirements/#scm-information
2.8. 文件签名校验(非必需)

官网说必需要包含.md5和.sha1签名校验文件,但我没有特地去配置这个,也是可以发布的。可以跳过这一步。
参考:https://central.sonatype.org/publish/requirements/#provide-files-checksums
3. 在 JIRA 上提发布申请

3.1. 注册 JIRA

点击如下链接注册账号并登录,如果有账号直接登录即可:
https://issues.sonatype.org/secure/Signup!default.jspa
参考:https://central.sonatype.org/publish/publish-guide/#initial-setup
3.2. 创建项目

点击新建,按照下面的图示来填写:
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212142730368-699111626.jpg[/img]
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212142825185-1737658773.jpg[/img]
3.3. 查看刚刚创建的项目

正常情况下创建好默认就会进入刚刚创建的项目,如果进不去,可以按照路径项目-查看所有项目-类别-Central即可看到项目链接,点进去后,切换筛选器我的报告就能看到创建的项目了。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211150752302-1906610182.jpg[/img]
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212143213692-121911178.jpg[/img]
3.4. 在github中创建一个空项目

创建好的项目,会分配一个唯一标识OSSRH-***(见上图),需要用这个标识去github上创建一个空项目,这个是为了向核审管理员证明需要发布的github项目确实是你自己的项目。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212143328644-1097893055.jpg[/img]
3.5. 等待管理员审核

管理员都在国外,因为时差,一般隔天才会核审通过。没办法,耐心等着吧。
审核通过后,会发邮件(邮箱就是注册JIRA的那个),也会在JIRA项目下出现活动日志。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212101553138-906049508.jpg[/img]
4. 获取 GPG 密钥

在等待JIRA项目审核的同时,可以并行把GPG密钥搞定。
注意:mac系统和windows系统在获取GPG密钥时有些区别,mac全程都是通过命令行搞定的,而windows提供了操作界面,可能会方便一点。这里只介绍mac系统的配置方式,windows系统可以自行搜配置教程。
4.1. 安装 GnuPG

https://gnupg.org/download/index.html#sec-1-2
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212143449960-1103643298.jpg[/img]
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212143541255-268744540.jpg[/img]
使用gpg --version命令来验证,如果输出了版本则说明安装成功。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212140536338-715198362.jpg[/img]
参考:https://central.sonatype.org/publish/requirements/gpg/#installing-gnupg
4.2. 生成密钥对

使用命令gpg --full-generate-key生成密钥对,2年内有效。执行命令后,需要输入姓名和邮箱(建议和注册JIRA时用的一致),然后输入o(注意是字母o)确认生成。随后会提示输入一个passphrase口令来保护你的密钥对,这个口令要记住,后面配置需要使用。
注意:这个命令不建议执行多次,不然生成了多个密钥对,发布时还要去配置使用哪个密钥对,比较麻烦。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211171323749-1754565062.jpg[/img]
参考:https://central.sonatype.org/publish/requirements/gpg/#generating-a-key-pair
4.3. 发布公钥

官网提供了3个地址,有的地址可能不能用,上面这个地址亲测有效。(所以官方也不一定靠谱)
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211184153518-1763399774.jpg[/img]
注意:验证的命令要等一会执行才能响应数据,估计发布可能有点延时,可以多执行几次。反正如果响应如图所示,就代表发布成功了。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231211184512000-724411032.jpg[/img]
参考:https://central.sonatype.org/publish/requirements/gpg/#distributing-your-public-key
5. JIRA 项目核审通过后的配置

5.1. 仓库地址

核审通过后,在JIRA上的项目里可以看到日志,这代表项目已经拥有发布权限了。下图标记出来的就是仓库地址。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212101553138-906049508.jpg[/img]
5.2. 修改 maven 配置

这里修改的是maven的setting.xml配置文件,如果不知道配置文件在哪里(那么你不是一个合格的程序猿),可以在IntelliJ IDEA的settings > Build,Execution,Deployment > Builds Tools > Maven页面找到User settings file。

  1. <servers>
  2.     <server>
  3.         <id>ossrh</id>
  4.         <username>JIRA用户名</username>
  5.         <password>JIRA登录密码</password>
  6.     </server>
  7. </servers>
复制代码
  1. <profiles>
  2.     <profile>
  3.         <id>ossrh</id>
  4.         <activation>
  5.             <activeByDefault>true</activeByDefault>
  6.         </activation>
  7.         <properties>
  8.             <gpg.executable>gpg2</gpg.executable>
  9.             <gpg.passphrase>GPG口令</gpg.passphrase>
  10.         </properties>
  11.     </profile>
  12. </profiles>
复制代码
5.3. 配置仓库地址和插件

这里是在项目的pom文件中配置。

  1. <distributionManagement>
  2.     <snapshotRepository>
  3.         <id>ossrh</id>
  4.         <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
  5.     </snapshotRepository>
  6.     <repository>
  7.         <id>ossrh</id>
  8.         <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
  9.     </repository>
  10. </distributionManagement>
复制代码
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.sonatype.plugins</groupId>
  5.             <artifactId>nexus-staging-maven-plugin</artifactId>
  6.             <version>1.6.13</version>
  7.             <extensions>true</extensions>
  8.             <configuration>
  9.                 <serverId>ossrh</serverId>
  10.                 <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
  11.                 <autoReleaseAfterClose>true</autoReleaseAfterClose>
  12.             </configuration>
  13.         </plugin>
  14.     </plugins>
  15. </build>
复制代码
6. 发布

JIRA 核审通过后,就可以发布了
6.1. 发布到中央仓库

[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212132843578-639628821.jpg[/img]
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212133354383-1994332081.jpg[/img]6.2. 验证发布结果

发布成功以后,并不能立刻在maven中央仓库中搜索到,大概隔一天以后才能搜索得到,所以不要心急,耐心等待。

执行完发布动作以后,大概隔了几分钟以后,JIRA项目下会多出一条日志:
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212134234759-1514846093.jpg[/img]
这条日志给出了两个地址和两个时间,反正差不多要等个那么长的时间才能搜得到。我已经发布到中央仓库里了,第一个地址里还是找不到,但是第二个地址里能搜到:
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212135129869-1389879459.jpg[/img]
下面这个地址好像是执行完发布操作后,过一会就能搜得到,可以试试:
https://s01.oss.sonatype.org/content/groups/public/
一般是隔天,才能在中央仓库里搜得到,在阿里云的仓库里也可以搜得到,阿里云本来就是从中央仓库同步的。
建议使用groupId来搜,一搜一个准。用artifactId来搜在第一页可能找不到,因为maven会对搜索结果做排序。
[img=30%,auto]https://img2023.cnblogs.com/blog/2706986/202312/2706986-20231212135628986-111183510.jpg[/img]
7. 彩蛋

如果遇到这样的报错The environment variable JAVA_HOME is not correctly set,不要慌,一般是因为你本地环境变量没有设置JAVA_HOME或者设置成了jre的目录。如果是因为前者可以去设置一下JAVA_HOME,如果是因为后者,可以把JAVA_HOME的目录改成jdk/bin的目录,或者可以改一下项目的pom配置文件:
  1. <plugin>
  2.     <groupId>org.apache.maven.plugins</groupId>
  3.     <artifactId>maven-javadoc-plugin</artifactId>
  4.     <version>2.9.1</version>
  5.     <executions>
  6.         <execution>
  7.             <id>attach-javadocs</id>
  8.             <goals>
  9.                 <goal>jar</goal>
  10.             </goals>
  11.             <configuration>
  12.                
  13.                 <javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
  14.             </configuration>
  15.         </execution>
  16.     </executions>
  17. </plugin>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4