组件发布到Maven中央仓库过程🌎

概述

  最近发布了自己的项目到maven中央仓库、通过文章记录一下,第一方便自己,第二帮助他人,我想看这篇文章的同学肯定一定用过maven来构建项目,并且可能用过maven私服,上传maven中央仓库的起因是因为自己编写了一个脚手架,涉及到一部分和业务无关代码,想着是做个模块放到项目里好还是独立出来玩好呢,后来一想,为了让大家专注业务,还是分离出来,并打包上传到maven中央仓库吧。在刚学习maven的时候知道了仓库这个概念、本地、私服、云端。很久一来,都是玩本地,后来玩私服,这不也玩起中央仓库了,那就分享一下经验吧,分享的时候我尽力做到调理、清晰、让大家都看得懂,少走弯路。

要求

  • 只能将发行版上传到中央存储库
  • 具有java doc和代码源文件
  • 使用GPG/PGP签名文件PGP签名
  • 足够的源数据信息POM信息

步骤

准备pom

坐标(GAV)

  • groupId:以反向域名开头的项目的顶级名称空间级别
  • artifactId:组件的唯一名称
  • version:组件的版本字符串,版本可以是任意字符串,但不能以结尾-SNAPSHOT,因为这是用于标识当前正在开发的版本的保留字符串。强烈建议使用[语义版本控制](http://semver.org)帮助您的用户选择版本。
1
2
3
<groupId>cn.smallbun.scaffold</groupId>
<artifactId>scaffold-framework</artifactId>
<version>1.0.0</version>

项目名称,描述和URL

有关项目的一些人可读的信息和指向你的项目的网站获取更多,我们需要的存在name,description和url 。

1
2
3
<name>smallbun-scaffold-framework</name>
<description>smallbun企业级开发脚手架-核心框架</description>
<url>http://www.smallbun.cn</url>

一种常见且可接受的名称惯例是使用Maven属性从坐标中将其组装起来:

1
<name>${project.groupId}:${project.artifactId}</name>

许可证信息

需要声明用于分发组件的许可证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--Apache-->
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<!--OR MIT-->
<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

开发者信息

为了能够关联项目,需要添加一个开发人员部分。

1
2
3
4
5
6
7
8
<!--开发人员信息-->
<developers>
<developer>
<name>zuoqinggang</name>
<email>qinggang.zuo@gmail.com</email>
<url>https://www.pingfangushi.com</url>
</developer>
</developers>

SCM 信息

与源代码管理系统的连接是一个必需的元素。使用的语法取决于使用的版本控制系统。

  • connection详细说明只读连接。
  • developerConnection详细说明读取和写入访问连接详细信息。
  • url包含了一个Web前端,您的SCM系统的URL

Maven SCM文档中提供了有关各种受支持格式的详细信息,并提供了许多常见示例。
subversion

1
2
3
4
5
<scm>
<connection>scm:svn:http://subversion.example.com/svn/project/trunk/</connection>
<developerConnection>scm:svn:https://subversion.example.com/svn/project/trunk/</developerConnection>
<url>http://subversion.example.com/svn/project/trunk/</url>
</scm>

github

1
2
3
4
5
<scm>
<connection>scm:git:git://github.com/simpligility/ossrh-demo.git</connection>
<developerConnection>scm:git:ssh://github.com:simpligility/ossrh-demo.git</developerConnection>
<url>http://github.com/simpligility/ossrh-demo/tree/master</url>
</scm>

BitBucket

1
2
3
4
5
<scm>
<connection>scm:git:git://bitbucket.org/simpligility/ossrh-pipeline-demo.git</connection>
<developerConnection>scm:git:ssh://bitbucket.org:simpligility/ossrh-pipeline-demo.git</developerConnection>
<url>https://bitbucket.org/simpligility/ossrh-pipeline-demo/src</url>
</scm>

BitBucket上的Mercurial

1
2
3
4
5
<scm>
<connection>scm:hg:http://bitbucket.org/juven/hg-demo</connection>
<developerConnection>scm:hg:https://bitbucket.org/juven/hg-demo</developerConnection>
<url>http://bitbucket.org/juven/hg-demo</url>
</scm>

Apache Maven的Apache Git服务器上的Git

1
2
3
4
5
6
<scm>
<connection>scm:git:https://git-wip-us.apache.org/repos/asf/maven.git</connection>
<developerConnection>scm:git:https://git-wip-us.apache.org/repos/asf/maven.git</developerConnection>
<url>https://github.com/apache/maven/tree/${project.scm.tag}</url>
<tag>master</tag>
</scm>

完整的pom信息

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.smallbun.scaffold</groupId>
<artifactId>scaffold-framework</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>smallbun-scaffold-framework</name>
<description>smallbun企业级开发脚手架-核心框架</description>
<url>http://www.smallbun.cn</url>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<developers>
<developer>
<name>zuoqinggang</name>
<email>qinggang.zuo@gmail.com</email>
<url>https://www.pingfangushi.com</url>
</developer>
</developers>

<scm>
<connection>scm:git:git@github.com:pingfangushi/smallbun-scaffold-framework.git</connection>
<developerConnection>scm:git:git@github.com:pingfangushi/smallbun-scaffold-framework.git
</developerConnection>
<url>http://github.com/pingfangushi/smallbun-scaffold-framework/tree/master</url>
</scm>
...

</project>

Java doc和源代码

要生成Javadoc和Source jar文件,您必须配置javadoc和Source 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
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

GPG签名

  为了提高中央存储库的质量,maven中央仓库要求工件(除校验和之外的所有文件)提供PGP签名,并将公共密钥分发到诸如http://pgp.mit.edu的密钥服务器。

安装GnuPG

  从http://www.gnupg.org/download/下载GPG或使用您喜欢的软件包管理器进行安装,然后通过运行gpg --version命令进行验证。

注:在某些系统上,将使用较新的gpg2:gpg2 –version

生成密钥对

密钥对使我们可以使用GPG对工件进行签名,然后用户可以验证工件是否已由我们签名。执行命令根据提示提供用户名、邮箱,密码信息。

script
1
gpg --gen-key

列出秘钥

script
1
gpg --list-keys

列出可以使用的私钥

script
1
gpg --list-secret-keys

输出中pub一行’/‘后的8位HEX字符串就是密钥ID

上传到服务器

script
1
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys <密钥ID>

--keyserver参数标识目标密钥服务器地址,并使用--SEND-keys 是要分发密钥的keyid的。您可以通过gpg --list-secret-keys来获取密钥ID。一旦提交给密钥服务器,公钥将同步到其他密钥服务器。

pom配置

Maven GPG插件用于通过以下配置对组件进行签名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

上传仓库

提交 issues

首先你需要有一个账户:点击注册
然后创建问题项:创建问题

在未收到以解决的电子邮件之前,请不要进行部署
提交问题不困难,这里不截图讲了,如果使用域名,需要配置解析到问题URL,将会自动验证,正常情况下,白天提交,第二天早上就可以收到回复了。

配置身份验证

打开maven setting.xml文件配置加入如下内容

1
2
3
4
5
6
7
8
9
<settings>
<servers>
<server>
<id>ossrh</id>
<username>用户名</username>
<password>密码</password>
</server>
</servers>
</settings>

pom配置

由于OSSRH始终运行最新版本的Sonatype Nexus Repository Manager,因此最好使用Nexus Staging Maven插件的最新版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<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://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
...
</plugins>
</build>

或者,如果想使用Maven部署插件(默认),则需要添加完整的distributionManagement

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

推荐使用nexus-staging-maven-plugin发布

注:settings.xml server元素中的id元素与snapshotRepositoryand 元素中的repository元素以及serverIdNexus Staging Maven插件的配置如何相同

发布

如果版本是发行版(不要以-SNAPSHOT结尾),执行deploy就可以了。

script
1
mvn clean deploy

参考资料

Guide to uploading artifacts to the Central Repository
The Central Repository Apache Maven

组件发布到Maven中央仓库过程🌎

https://pingfangushi.com/posts/48969/

作者

SanLi

发布于

2019-12-25

更新于

2021-07-08

许可协议