跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库
未分類
9 1 月 2020

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

資深大佬 : NebulaGraph 60

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

摘要

Maven 中央仓库并不支持直接上传 jar 包,因此需要将 jar 包发布到一些指定的第三方 Maven 仓库,比如:Sonatype OSSRH 仓库,然后该仓库再将 jar 包同步到 Maven ,本文详细记录整个发布、同步过程。

注册一个 Sonatype 用户

进入地址:https://issues.sonatype.org/secure/Signup!default.jspa 注册 Sonatype 用户,Sonatype 通过 JIRA ( JIRA 是 Atlassian 公司出品的项目与事务跟踪工具)来管理 OSSRH 仓库。

创建一个发布构件的 issue

提交「构件发布申请」的第一步是在 JIRA Dashborad 上创建一个 issue。如下所示,点击 Create 按钮:

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

会弹出一个对话框让你填写 issue 的详细信息,这里最重要的就是 Group Id,一般会带上域名,千万别弄错了,这关系到以后发布其它的构件。我们这里是com.vesoft。

Sonatype 有域名验证,验证方式:

  • 往你的 DNS 中添加 JIRA 编号记录
  • 重定向到你的 Github 主页

如果你没有域名,可参考这个链接:http://central.sonatype.org/pages/choosing-your-coordinates.html 的方法进行操作

  • 选择一个带有项目托管信息的 GroupId,例如 io.github.facebook 或 com.github.facebook
  • 另外一种推荐的方式是使用免费的 free managed security reporting service
    • 将 https://hackerone.com/central-security-project/reports/new 作为你项目的安全信息保障 url,剩下的事情 Sonatype 工作人员会自行搞定。

等待 issue 审核通过

审核因为时差原因需要一定时间,审核通过后会收到邮件通知,同时在对应 issue 下会看到 Sonatype 工作人员的回复,一般是添加一个 comment,内容大致如下:

Configuration has been prepared, now you can: Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/ deploy/maven2 Promote staged artifacts into repository ‘Releases’ Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging please comment on this ticket when you promoted your first release, thanks

使用 GPG 生成密钥对

生成密钥对

> gpg --gen-key 

会让选择加密方式:

  • RSA and RSA (default)
  • DSA and Elgamal
  • DSA (sign only)
  • RSA (sign only)

默认选第一个,选择之后,需输入用户名和邮箱,和 Passphase——相当于密钥库密码。

查看公钥

> gpg --list-keys  xxx/.gnupg/pubring.gpg --------------------------------- pub   2048R/xxxx 2019-12-02 uid   $YOUR_UID <$YOUR_EMAIL> sub   2048R/**** 2019-12-02 

这里的公钥 ID 是 xxxx,马上就会用到了。

将公钥上传到 PGP 密钥服务器

gpg --keyserver hkp://keys.gnupg.net:11371 --send-keys xxxx 

查看公钥是否上传成功

> gpg --keyserver hkp://keys.gnupg.net:11371 --recv-keys xxxx  gpg: 下载密钥‘xxxx’,从 hkp 服务器 keys.gnupg.net gpg: 密钥 xxxx:“$YOUR_UID <$YOUR_EMAIL>”未改变 gpg: 合计被处理的数量:1 gpg: 未改变:1 

NOTE:

  • 根据实际填写此处的公钥 ID
  • 很多网上教程给的是  pool.sks-keyservers.net ,个人感觉 pool.sks-keyservers.net 这个 keyserver 不好用,上传了 key,经常会验证失败,也获取失败,maven 支持两个 key servers http://keys.gnupg.net:11371 和 http://pool.sks-keyservers.net:11371
  • 此处采用 hkp 协议而不是 http 协议
  • 很多教程没有给端口,经试验,需加上端口号

本地的私钥用来对上传的构件进行数字签名,而下载该构件的用户可通过上传的公钥来验证签名–需验证这个构件是否由本人上传的,因为存在构件被篡改的可能。

修改 Maven 配置文件

修改 Maven 配置文件主要是需要修改 setting.xml 和项目的 pom.xml 文件

配置 Maven 的 setting.xml

修改 ~/.m2/setting.xml 文件

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                       http://maven.apache.org/xsd/settings-1.0.0.xsd">     <servers>       ...         <server>             <id>snapshots</id>             <username>$USER_NAME</username>             <password>$YOUR_PASSWORD</password>         </server>         <server>             <id>release</id>             <username>$USER_NAME</username>             <password>$YOUR_PASSWORD</password>         </server>     </servers>  </settings> 

替换 USER_NAME , YOUR_PASSWORD 为 Sonatype 上面注册的用户名和密码, 这里的 ID 会在 pom.xml 里面使用到。

配置 Maven 的 pom.xml

<project>     ...      <!-- More Project Information -->     <name>nebula-java</name>     <description>Nebula Java Client</description>     <url>https://github.com/vesoft-inc/nebula-java</url>     <scm>         <connection>scm:git:https://github.com/vesoft-inc/nebula</connection>         <url>https://github.com/vesoft-inc/nebula</url>         <developerConnection>scm:git:https://github.com/vesoft-inc/nebula</developerConnection>     </scm>     <licenses>         <license>             <name>Apache License, Version 2.0</name>             <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>             <distribution>repo</distribution>             <comments>license</comments>         </license>     </licenses>      ...     <profiles>         <profile>             <id>release</id>             <build>                 <plugins>                     <!-- Source -->                     <plugin>                         <groupId>org.apache.maven.plugins</groupId>                         <artifactId>maven-source-plugin</artifactId>                         <executions>                             <execution>                                 <id>attach-sources</id>                                 <goals>                                     <goal>jar</goal>                                 </goals>                             </execution>                         </executions>                     </plugin>                     <!-- Javadoc -->                     <plugin>                         <groupId>org.apache.maven.plugins</groupId>                         <artifactId>maven-javadoc-plugin</artifactId>                         <version>3.1.1</version>                         <configuration>                             <excludePackageNames>com.facebook.thrift:com.facebook.thrift.*</excludePackageNames>                         </configuration>                         <executions>                             <execution>                                 <id>attach-javadocs</id>                                 <phase>package</phase>                                 <goals>                                     <goal>jar</goal>                                 </goals>                                 <configuration>                                     <doclint>none</doclint>                                 </configuration>                             </execution>                         </executions>                     </plugin>                     <!-- GPG -->                     <plugin>                         <groupId>org.apache.maven.plugins</groupId>                         <artifactId>maven-gpg-plugin</artifactId>                         <version>1.6</version>                         <executions>                             <execution>                                 <phase>verify</phase>                                 <goals>                                     <goal>sign</goal>                                 </goals>                             </execution>                         </executions>                     </plugin>                 </plugins>             </build>             <distributionManagement>                 <repository>                     <id>release</id>                     <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>                 </repository>                 <snapshotRepository>                     <id>snapshots</id>                     <url>https://oss.sonatype.org/content/repositories/snapshots/</url>                 </snapshotRepository>             </distributionManagement>         </profile>     </profiles>     ... </project> 
  • pom.xml 中必须包括: name 、 description 、 url 、 licenses 、 developers 、 scm 等基本信息 (~~血泪史之踩过的坑~~)
  • 发布 maven 除了 jar 必须还有文档包和源码包。因此 pom 需添加 maven-javadoc-plugin  和  maven-source-plugin 。 参考示例:
com-vesoft-client |-- pom.xml |-- src `-- target     `-- attach-source-javadoc-1.0-SNAPSHOT.jar     `-- attach-source-javadoc-1.0-SNAPSHOT-javadoc.jar     `-- attach-source-javadoc-1.0-SNAPSHOT-sources.jar 
  • 发布构建需要秘钥加密,因此 pom 需添加 maven-gpg-plugin  (~~血泪史之踩过的坑~~)

多模块项目配置

nebula-java 是多模块项目

<modules>     <module>client</module>     <module>examples</module> </modules> 

为了上传 Client,需要上传 parent 的 pom.xml ,否则 Client 会找不到依赖(~~血泪史之踩过的坑~~),但我们又不希望上传 examples 模块,故做了如下改动:

  • 项目信息 name 、 description 、 url 、 licenses 、 developers 、 scm 等信息和 maven-gpg-plugin 放在 parent 的 pom.xml 文件中
<project>   ...     <name>nebula-java</name>     <description>Nebula Java Client</description>     <url>https://github.com/vesoft-inc/nebula-java</url>     <scm>         <connection>scm:git:https://github.com/vesoft-inc/nebula</connection>         <url>https://github.com/vesoft-inc/nebula</url>         <developerConnection>scm:git:https://github.com/vesoft-inc/nebula</developerConnection>     </scm>     <licenses>         <license>             <name>Apache License, Version 2.0</name>             <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>             <distribution>repo</distribution>             <comments>license</comments>         </license>     </licenses>      <developers>         <developer>             <id>$ID</id>             <name>$NAME</name>             <email>$EMAIL</email>             <organization>vesoft</organization>             <roles>                 <role>architect</role>                 <role>developer</role>             </roles>         </developer>     </developers>      <distributionManagement>         <repository>             <id>release</id>             <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>         </repository>         <snapshotRepository>             <id>snapshots</id>             <url>https://oss.sonatype.org/content/repositories/snapshots/</url>         </snapshotRepository>     </distributionManagement>      <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-gpg-plugin</artifactId>                 <version>1.6</version>                 <executions>                     <execution>                         <phase>verify</phase>                         <goals>                             <goal>sign</goal>                         </goals>                     </execution>                 </executions>             </plugin>         </plugins>     </build>   </project> 
  • 在 Java Client 的 pom.xml 中添加 maven-javadoc-plugin  、  maven-source-plugin 和 maven-deploy-plugin
<plugins>   ......     <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-source-plugin</artifactId>         <executions>             <execution>                 <id>attach-sources</id>                 <goals>                     <goal>jar</goal>                 </goals>             </execution>         </executions>     </plugin>     <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-javadoc-plugin</artifactId>         <version>3.1.1</version>         <configuration>             <excludePackageNames>com.facebook.thrift:com.facebook.thrift.*</excludePackageNames>         </configuration>         <executions>             <execution>                 <id>attach-javadocs</id>                 <phase>package</phase>                 <goals>                     <goal>jar</goal>                 </goals>                 <configuration>                     <doclint>none</doclint>                 </configuration>             </execution>         </executions>     </plugin>     <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-deploy-plugin</artifactId>         <executions>             <execution>                 <id>default-deploy</id>                 <phase>deploy</phase>             </execution>         </executions>     </plugin> </plugins> 

在 example 模块的 pom.xml 中声明 skip deploy

<plugins>   ......     <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-deploy-plugin</artifactId>         <configuration>             <skip>true</skip>         </configuration>     </plugin> </plugins> 

Q: 为什么 maven-gpg-plugin 放在 parent 的 pom.xm l 中,而 maven-javadoc-plugin,maven-source-plugin 插件放在 Client 的 pom.xml 中

A: 因为上传的所有构件都需要加密,包括 parent 的 pom.xml ,因此放在 parent 中; 而只有 Client 需要上传 javadoc,source,因此 maven-javadoc-plugin,maven-source-plugin 插件放在 Client 中。

上传构件到 OSS 中

在 nebula-java/ 目录下运行:

> mvn clean deploy -DpomFile=pom.xml 

NOTE:不加 -DpomFile,上传的文件中会没有 parent 的 pom.xml (~~又是一部血泪史~~)

在 OSS 中发布构件

使用 Sonatype 账号登录 https://oss.sonatype.org/#stagingRepositories,可在 Staging Repositories  中查看已上传的构件,这些构件目前是放在 Staging 仓库中,可进行模糊查询,定位到刚上传的构件。

此时,该构件的状态为 Open ,勾选它,然后点击 Close  按钮。系统会自动验证该构件是否满足指定要求 (幸福的人只有一种,不幸的人各有各的不幸,可能会遇到各种各样的不符合要求,Good luck !ヾ(◍°∇°◍)ノ゙)

当验证完毕后,状态会变为 Closed 。

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

最后,点击 Release 按钮来发布该构件

Dev 日志 | 如何将 jar 包发布到 Maven 中央仓库

页面可能要刷新一下才能看到最新的状态。

通知 Sonatype 构件已成功发布

在前面 JIRA 的 issue 下面回复一条“构件已成功发布”的评论,通知 Sonatype 的工作人员为要发布的构件做审批,发布后会关闭该 issue。

等待构件审批通过

然后,等待。。。

从中央仓库中搜索构件

大概十多分钟后,可以在这里 https://repo1.maven.org/maven2 找到刚刚发布的构件,可以直接在 pom.xml 中使用啦~~

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具