一篇搞定发布自己的类库到Maven中央仓库

打印 上一主题 下一主题

主题 930|帖子 930|积分 2790

在Maven项目中,90%以上的jar包是通过pom文件直接从开源仓库中获取依赖jar包文件,然后在项目中进行集成使用。
此时如果你有一个开源项目,那么如何将该开源项目发布到Maven中央仓库,让其他人可以方便的使用,而不是先下载jar,然后install的本地?
本文将通过一步步的操作带领大家讲自己的开源项目发布到Maven中央仓库(Maven Central Repository)中,https://mvnrepository.com/。
Maven中央仓库并不支持直接发布jar包,需要将jar包发布到一些指定的第三方Maven仓库,然后该仓库再将jar包同步到Maven中央仓库,Sonatype便是这样的角色
准备工作


  • 注册Github账户,在申请的时候需要用到GroupId,这边如果没有自己的网站及域名建议注册Github。
  • 注册Sonatype账户,注册地址:sonatype sign up

    • Username和Password作为登录的账号密码
    • 密码有强度要求
    • Email用于后续关联的通知,务必使用真实地址

项目发布申请


  • 点击导航栏的"新建"进行项目发布申请创建。

    通过在JIRA上创建issue来申请发布新的jar包,Sonatype的工作人员会进行审核,一般按照要求填写不会有问题。
    注意:

    • Group Id:如果有域名可以填写自己的域名,如果没有可以使用Github,Gitee,Gitlib等地址例如:io.github.you_account,此处注意必须是 io 的
    • Project Url及SCM Url建议都采用Git地址,其中SCM的地址需要带.git
    • Already Synced to Central选择No

  • 提交后等待一小段时间再来查看刚提交的问题。你会收到类似下图的回复。此时会要求进行Group Id的所有权验证。

    如果填写的是Github则会要求创建一个指定的仓库,创建一个即可。
    如果填写的是自己的自己的域名则会要求创建个指定文件到此域名下。
  • 填写按要求完成后修改状态为开放状态 。点击Wait for Reponse在弹出的状态列表中选择 开放 。

    如果按要求完成了所有的操作等待一段时间后一般就自动通过了。这时会给予推送服务器的地址,后续所有的相关地址都需要用到。

    到此发布申请也就完成了。
安装GPG

发布到Maven仓库中的所有文件都要使用GPG签名,以保障完整性。因此,我们需要在本地安装并配置GPG。

  • 进入gnupg下载,https://www.gnupg.org/download/index.html
  • 根据操作系统下载不同的版本并进行安装。
    此处以Windows为例:
    建议下载2个安装包分别为

    • Gpg4win:Full featured Windows version of GnuPG
      此工具通过Windows窗口进行密钥的管理。
    • download sig:Simple installer for GnuPG 1.4
      此工具用于后续Maven发布使用

  • 创建密钥

    • Gpg4win
      生成秘钥,按步步骤提示进行操作
      必须勾选密码

      按提示输入密码

      秘钥生成好后,右键选择密钥,需要你把公钥上传到公共服务器供sonatype验证。

    • 命令行
      生成秘钥,按步骤提示进行操作
      1. gpg --generate-key
      2. # 生成结果
      3. gpg (GnuPG/MacGPG2) 2.2.32; Copyright (C) 2021 Free Software Foundation, Inc.
      4. This is free software: you are free to change and redistribute it.
      5. There is NO WARRANTY, to the extent permitted by law.
      6. 注意:使用 “gpg --full-generate-key” 以获得一个全功能的密钥生成对话框。
      7. GnuPG 需要构建用户标识以辨认您的密钥。
      8. 真实姓名: xxxxxxx
      9. 电子邮件地址: xxxxxxx@foxmail.com
      10. 您选定了此用户标识:
      11.     “xxxxxxxx <xxxxx@foxmail.com>”
      12. 更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)? u
      13. 更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)? o
      14. 我们需要生成大量的随机字节。在质数生成期间做些其他操作(敲打键盘
      15. 、移动鼠标、读写硬盘之类的)将会是一个不错的主意;这会让随机数
      16. 发生器有更好的机会获得足够的熵。
      17. 我们需要生成大量的随机字节。在质数生成期间做些其他操作(敲打键盘
      18. 、移动鼠标、读写硬盘之类的)将会是一个不错的主意;这会让随机数
      19. 发生器有更好的机会获得足够的熵。
      20. gpg: 密钥 54EC3C8FA3A5B50F 被标记为绝对信任
      21. gpg: 目录‘/Users/xxxxxxxxx/.gnupg/openpgp-revocs.d’已创建
      22. gpg: 吊销证书已被存储为‘/Users/xxxxxxxxx/.gnupg/openpgp-revocs.d/xxxxxxxxxxxxxxxxxxxxxxxxxxxx.rev’
      23. 公钥和私钥已经生成并被签名。
      24. pub   rsa3072 2021-12-06 [SC] [有效至:2023-12-06]
      25.       8BDxxxxxxxxxxxxxxxxxxxxxxxxxxxxB50F
      26. uid                      xxxxxxxx <xxxxxxxx@foxmail.com>
      27. sub   rsa3072 2021-12-06 [E] [有效至:2023-12-06]
      复制代码
      秘钥生成好后,需要你把公钥上传到公共服务器供sonatype验证。
      官网教程里有三个地址可以用:

      • keyserver.ubuntu.com
      • keys.openpgp.org
      • pgp.mit.edu
      1. ➜  ~ gpg --keyserver pgp.mit.edu:11371 --send-keys 8BD96B0EA18E5162B94EA7F754EC3C8FA3A5B50F
      2. gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 pgp.mit.edu:11371
      3. gpg: 发送至公钥服务器失败:文件结尾
      4. gpg: 发送至公钥服务器失败:文件结尾
      5. ➜  ~ gpg --keyserver keyserver.ubuntu.com --send-keys 8BD96B0EA18E5162B94EA7F754EC3C8FA3A5B50F
      6. gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 hkp://keyserver.ubuntu.com
      7. gpg: 发送至公钥服务器失败:Network is unreachable
      8. gpg: 发送至公钥服务器失败:Network is unreachable
      9. ➜  ~ gpg --keyserver pgp.mit.edu:11371 --send-keys 54EC3C8FA3A5B50F
      10. gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 pgp.mit.edu:11371
      11. gpg: 发送至公钥服务器失败:文件结尾
      12. gpg: 发送至公钥服务器失败:文件结尾
      复制代码

配置setting.xml

setting.xml为Maven的全局配置文件,一般路径为$MAVEN_HOME/conf/settings.xml
找到Services节点进行如下配置
  1. <servers>
  2.     <server>
  3.         <id>ossrh</id>
  4.         <username>你的Sonatype账号</username>
  5.         <password>你的Sonatype密码</password>
  6.     </server>
  7. </servers>
复制代码
找到profiles节点进行如下配置,gpg.executable节点建议配置第二个安装包安装的gpg路径,这一步一定要做不然容易发布的时候提示找不到gpg。
  1. <profiles>
  2.       <profile>
  3.           <id>ossrh</id>
  4.           <activation>
  5.               <activeByDefault>true</activeByDefault>
  6.           </activation>
  7.           <properties>
  8.               <gpg.executable>"D:\Program Files (x86)\GnuPG\bin\gpg.exe"</gpg.executable>
  9.               <gpg.passphrase>你的密钥创建时候的密码</gpg.passphrase>
  10.           </properties>
  11.       </profile>
  12. </profiles>
复制代码
配置pom.xml

以下为一些关键点的配置示例
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.    
  7.     <name>fluent-validation</name>
  8.     <url>https://github.com/yikuaibaiban/fluent-validation</url>
  9.     <description>fluent-validation is a streaming validation framework.</description>
  10.    
  11.     <issueManagement>
  12.         <system>Github Issue</system>
  13.         <url>https://github.com/yikuaibaiban/fluent-validation/issues</url>
  14.     </issueManagement>
  15.     <licenses>
  16.         <license>
  17.             <name>The Apache Software License, Version 2.0</name>
  18.             <url>https://apache.org/licenses/LICENSE-2.0.txt</url>
  19.         </license>
  20.     </licenses>
  21.    
  22.     <developers>
  23.         <developer>
  24.             <name>yikuaibaiban</name>
  25.             <email>xxxx@xxxx.com</email>
  26.             <timezone>+8</timezone>
  27.         </developer>
  28.     </developers>
  29.    
  30.     <scm>
  31.         <url>https://github.com/yikuaibaiban/fluent-validation</url>
  32.         <connection>scm:https://github.com/yikuaibaiban/fluent-validation.git</connection>
  33.         <developerConnection>scm:git:https://github.com/yikuaibaiban/fluent-validation.git</developerConnection>
  34.     </scm>
  35.     <properties>
  36.         <maven.compiler.source>8</maven.compiler.source>
  37.         <maven.compiler.target>8</maven.compiler.target>
  38.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  39.     </properties>
  40.     <build>
  41.         <plugins>
  42.             <plugin>
  43.                 <groupId>org.apache.maven.plugins</groupId>
  44.                 <artifactId>maven-compiler-plugin</artifactId>
  45.                 <version>3.8.1</version>
  46.                 <configuration>
  47.                     <source>${maven.compiler.source}</source>
  48.                     <target>${maven.compiler.target}</target>
  49.                     <encoding>${project.build.sourceEncoding}</encoding>
  50.                 </configuration>
  51.             </plugin>
  52.         </plugins>
  53.     </build>
  54.     <profiles>
  55.         <profile>
  56.             <id>release</id>
  57.             <build>
  58.                 <plugins>
  59.                     
  60.                     <plugin>
  61.                         <groupId>org.apache.maven.plugins</groupId>
  62.                         <artifactId>maven-source-plugin</artifactId>
  63.                         <version>2.2.1</version>
  64.                         <executions>
  65.                             <execution>
  66.                                 <id>attach-sources</id>
  67.                                 <goals>
  68.                                     <goal>jar-no-fork</goal>
  69.                                 </goals>
  70.                             </execution>
  71.                         </executions>
  72.                     </plugin>
  73.                     
  74.                     <plugin>
  75.                         <groupId>org.apache.maven.plugins</groupId>
  76.                         <artifactId>maven-javadoc-plugin</artifactId>
  77.                         <version>3.5.0</version>
  78.                         <configuration>
  79.                             <windowtitle>${project.name}</windowtitle>
  80.                             <doctitle>${project.name}</doctitle>
  81.                             <show>private</show>
  82.                             <detectLinks>false</detectLinks>
  83.                             <detectOfflineLinks>true</detectOfflineLinks>
  84.                             <linksource>true</linksource>
  85.                             <detectJavaApiLink>true</detectJavaApiLink>
  86.                             <source>8</source>
  87.                         </configuration>
  88.                         <executions>
  89.                             <execution>
  90.                                 <id>attach-javadocs</id>
  91.                                 <goals>
  92.                                     <goal>jar</goal>
  93.                                 </goals>
  94.                             </execution>
  95.                         </executions>
  96.                     </plugin>
  97.                     
  98.                     <plugin>
  99.                         <groupId>org.apache.maven.plugins</groupId>
  100.                         <artifactId>maven-gpg-plugin</artifactId>
  101.                         <version>1.6</version>
  102.                         <executions>
  103.                             <execution>
  104.                                 <phase>verify</phase>
  105.                                 <goals>
  106.                                     <goal>sign</goal>
  107.                                 </goals>
  108.                             </execution>
  109.                         </executions>
  110.                     </plugin>
  111.                 </plugins>
  112.             </build>
  113.             <distributionManagement>
  114.                 <snapshotRepository>
  115.                     <id>ossrh</id>
  116.                     <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
  117.                 </snapshotRepository>
  118.                 <repository>
  119.                     <id>ossrh</id>
  120.                     <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
  121.                 </repository>
  122.             </distributionManagement>
  123.         </profile>
  124.     </profiles>
  125. </project>
复制代码
发布到Maven中央仓库

推送代码


  • 使用命令行发布 mvn clean deploy -P release
  • 使用Idea发布

后续操作(必须要操作)

此时进入https://s01.oss.sonatype.org并登录(登录的账号密码与刚开始注册的账号密码一致)查看发布好的构件,点击左侧的Staging Repositories。
此时需注意,如果项目中版本信息为1.0.0-SNAPSHOT,即SNAPSHOT为后缀,则发布的项目位于Snapshots目录下。在左上角的Artifact Search中能够搜到。
如果是Release后缀,则可直接在Staging Repositories中看到(有可能要稍等一下,等待平台处理)。
选中对于的repository之后,点击的close,close时会检查发布的构件是否符合要求。若符合要求,则close成功,成功之后点击箭头所指的release,即可正式将jar包发布到Sonatype OSSRH仓库。

当点击Release之后,邮件中会收到Issues变化的信息,提示同步已经被激活,正常10分钟就可以更新同步。
release成功大概2个小时之后,该构件就会同步到Maven中央仓库,届时会有邮件通知。
实践过程中发现十分钟之内已经成功同步到https://repo1.maven.org/的中央仓库当中。
逐步的其他Maven仓库,包括阿里镜像都会进行同步。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

道家人

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表