注意点:

  1. 测试 gpg 和 javadoc 插件不适用于 JDK 11,JDK 8 测试没问题
  2. 同一个版本号不可以重复发布
  3. 无法删除已发布的包

注册 Sonatype 的账户

注册地址

点击界面上方 Create 按钮即可创建 Issue。

1
2
3
4
5
6
7
8
Project: Community Support - Open Source Project Repository Hosting (OSSRH)
Issue TypeRequired: New Project

Summary: Jar 包的名称
# 注意这里,group id 需要证实当前域是你本人所有的,我就在这里配错了,找了好半天,详细的过程可以看这里 https://issues.sonatype.org/browse/OSSRH-79407
Group Id:io.github.wang-xiaowu
Project URL:项目站点,如:https://gitee.com/snowheart/dingtalk-robot
SCM url:项目源码仓库,如:https://gitee.com/snowheart/dingtalk-robot.git

配置完成后,等待这里通过就好。

配置 Maven

修改 setting 文件

servers 节点内部增加如下 server 配置:

1
2
3
4
5
<server>
<id>ossrh</id>
<username>wangxiaowu</username>
<password>密码</password>
</server>

修改项目 pom.xml 文件(以下插件版本适用 JDK 1.8)

添加必要的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--<url> 标记用于常规可浏览 URL
<connection> 用于读取访问,
<developerConnection> 用于写入访问
见 https://central.sonatype.org/publish/requirements/-->
<licenses>
<license>
<name>The ApacheSoftware License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<name>wangxiaowu</name>
<email>943915349@qq.com</email>
</developer>
</developers>
<scm>
<tag>master</tag>
<connection>scm:git:git@github.com:wang-xiaowu/behappy-redis.git</connection>
<developerConnection>scm:git:ssh@github.com:wang-xiaowu/behappy-redis.git</developerConnection>
<url>https://github.com/wang-xiaowu/behappy-redis/tree/master</url>
</scm>

添加 profile 节点:

参考:https://central.sonatype.org/publish/publish-maven/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<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.0.1</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<additionalparam>-Xdoclint:none</additionalparam>
<detectJavaApiLink>false</detectJavaApiLink>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</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>
<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>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
<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>
</profile>
</profiles>

配置 GPG 的公私钥信息

Windows 下使用 gpg4win 来进行配置。下载地址:https://www.gpg4win.org/get-gpg4win.html

安装好之后,打开 cmd 控制台(如果提示 gpg 不是可执行程序或命令,请配置 Path 路径到 GnuPG 的 bin 目录)。

  • 生成密钥的命令 gpg --gen-key,过程中需要使用到姓名、邮箱等信息,这里的配置最好和 Sonatype 注册信息、pom 文件配置信息保持一致,以免不必要的麻烦。
  • 生成的过程中,会有个弹框要求输入 Passphase 信息,这个是密钥的密码,同样需要记牢。发布签名的过程中会用得到。
  • 使用 gpg --list-keys 命令查询配置好的公私钥信息。
  • 使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公钥key> 将公钥信息发送到 ubuntu.com 服务器,后续推送 Maven 仓库会做校验。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ C:\Users\Lwx> gpg --list-keys
######
C:/Users/Lwx/AppData/Roaming/gnupg/pubring.kbx

pub rsa2048 2018-12-15 [SC] [expires: 2020-12-14]
01C6D8A0B3C1DFD78CEE66E73B4D46C0075BD34C
uid [ultimate] Wanxiang Liu <sxjwzxlwx@yeah.net>
sub rsa2048 2018-12-15 [E] [expires: 2020-12-14]
######
$ C:\Users\Lwx> gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 01C6D8A0B3C1DFD78CEE66E73B4D46C0075BD34C
######
gpg: sending key 01C6D8A0B to hkp server keyserver.ubuntu.com
######

打包 Deploy

使用 mvn clean deploy -DskipTests -P <profile-id> 命令,一键打包。打包过程中会要求输入 gpg 密钥的 Passphase 信息,输入即可。

查看发布结果

正常这时候就完事儿了,然后进入 https://repo1.maven.org/maven2/ 查看自己的包是否有正确发布,比如下图。如果没有,则进行手动发布 Release 步骤。

手动发布 Release(可选)

  1. 进入 https://s01.oss.sonatype.org/#stagingRepositories 查看发布好的构件
  2. 点击左侧的 Staging Repositories,一般最后一个就是刚刚发布的 jar 了,此时的构件状态为 Open
  3. 选中构件,并点击上方的 CloseConfirm,在下边的 Activity 选项卡中查看状态
  4. 当状态变成 closed 后,执行 ReleaseConfirm,并在下边的 Activity 选项卡中查看状态
  5. 成功后构件自动删除,一小段时间(约 1-2 个小时)后即可同步到 Maven 的中央仓库。届时会有邮件通知。

扫尾工作

对 Sonatype 上的 Issue 增加 Comment,留言致谢并表示发布已经完成,请工作人员关闭 Issue。

在以下两个网站可以对自己上传的包进行搜索: