发布jar包到Maven中央仓库

动手之前,先通读全文,因为前后有些信息会相互关联。有不清楚的,欢迎咨询博主。

一、注册并登录sonatype

https://issues.sonatype.org/secure/Dashboard.jspa

记住帐号和密码

二、项目配置

2.1 配置pom.xml

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
82
83
84
<project...>
<scm>
<connection>scm:git:https://gitee.com/bxl/simple-tiny-program.git</connection>
<url>http://www.adouteam.com/simple-tiny-program.html</url>
</scm>
<developers>
<developer>
<name>buxianglong</name>
<email>buxianglongoschina@gmail.com</email>
</developer>
</developers>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 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>2.9.1</version>
<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.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>

三、 首次发布

3.1 提交申请issue

还是在Sonatype:https://issues.sonatype.org/secure/Dashboard.jspa

可以参考我的提交:https://issues.sonatype.org/browse/OSSRH-42535

由于时差问题,需要耐心等待。

3.2 认证

groupId为 com.github 的,比较容易认证,只要scm信息和groupId一致即可,不需要额外操作。

如果是使用自己的域名,最简单的认证方式是通过域名邮箱发送邮件给central@sonatype.com。更多说明:https://central.sonatype.org/pages/choosing-your-coordinates.html

四、发布升级版本

4.1 ~/.m2/setting.xml 增加server配置:

1
2
3
4
5
6
7
8
9
<settings>
<servers>
<server>
<id>oss</id>
<username>{{sonatype_email}}</username>
<password>{{sonatype_password}}</password>
</server>
</servers>
</settings>

4.2 安装gpg签名工具

mac系统

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装gpg
brew install gpg
# 查看工具版本
gpg2 --version
# 生成签名,设置用户名{{gpg_name}}、邮箱{{gpg_email}}、记住这里的密码{{gpg_password}}
gpg2 --gen-key
# 列出所有签名
gpg2 --list-keys
-------------------------------------
pub rsa2048 2019-01-11 [SC] [expires: 2021-01-10]
F72902B6CB632C4721EBB38FBDE0876BCF3C0D45
uid [ultimate] buxianglong <buxianglongoschina@gmail.com>
sub rsa2048 2019-01-11 [E] [expires: 2021-01-10]

或者 https://sourceforge.net/p/gpgosx/docu/Download/ 下载安装GnuPG for OSX

4.3 将签名公钥发布到PGP密钥服务器

服务器可以这里找:https://sks-keyservers.net/status/

可以命令提交,也可以进入对应网站服务页面提交

1
2
3
4
5
6
7
8
# 发布到PGP密钥服务器
gpg2 --keyserver pgp.net.nz --send-keys F72902B6CB632C4721EBB38FBDE0876BCF3C0D45
# 验证是否发布成功
gpg2 --keyserver pgp.net.nz --recv-keys F72902B6CB632C4721EBB38FBDE0876BCF3C0D45

gpg: key BDE0876BCF3C0D45: "buxianglong <buxianglongoschina@gmail.com>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1

五、发布到中央仓库

1
mvn clean deploy -P release

登录:https://oss.sonatype.org/#stagingRepositories

进入左侧菜单“Staging Repositories”,找到对应的“Repository”,先Close,然后 Release.

等 Description 变成 ok 后,10分钟左右,即可在中央仓库https://oss.sonatype.org/#welcome 搜索到jar包。

https://search.maven.org 可能需要2小时后,才能够搜索到。

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×