发布/上传Jar包到Maven中央仓库
一、Sonatype 帐号
Sonatype 地址
二、项目申请
创建工单
可以理解为在 Sonatype 系统中申请一个自己的项目,以 Group Id 为单位,Group Id 支持域名地址和 Github 地址两种方式验证,大家可根据实际情况自行选择。申请后需要经过管理员对我们填写的 Group Id 所有权进行验证,审核通过后方可上传 jar 包。我们在页面顶部点击 Create 按钮来创建工单。
Project: 项目类型。选择 Community Support - Open Source Project Repository Hosting (OSSRH)
Issue Type: 工单类型。选择 New Project
Summary: 填写 jar 包名称即可
Description: 选填项,写一些描述
Group Id: 填写项目的Group Id,域名和 Github Group Id两种方式下面分别介绍申请过程
Project URL: 填写项目主页,可以是托管地址,也可以是官网地址
SCM URL: 填写项目的 Git 地址
无
三、资质审核
创建完工单后,会显示一个未解决的工单(Resolution: Unresolved,Status: OPEN),同时在页面可以看到 JIRA 分配了一个管理员来跟进此工单作业。运气好过两分钟就会有人回复你相关的审核信息了,最长不超过1天应该,毕竟国内外存在时间差,工作人员也是需要下班休息的。
Github Group Id
使用 Github Group Id 申请的,则会收到类似下图的回复,他告诉我们需要在自己的 Github 仓库中新建一个名为工单号的项目。
作者这里项目名是 OSSRH-65608 ,并且该项目需要是 public 的权限,即所有人可访问,接下来在 Github 创建一个空项目。
创建完 Github 项目后,我们需要在页面上评论工单,告诉管理员我们已经创建好了他指定的项目。之后等待管理员审核。
I have been in the making created a public project on https://gitee.com/
We have added a TXT record to nohrd.com,please confirm.
审核通过
若收到类似下图的回复,则证明我们的项目审核通过了,而且工单的状态变更为[Resolution: RESOLVED,Status: Fixed],说明我们已经可以发布我们的 jar 包到中央仓库了,另外回复中告知我们如果第一次发布 jar 包,需要在工单中评论。
以前回复的地址域名为 oss.sonatype.org ,官方已声明建议使用新域名地址 s01.oss.sonatype.org。
而且之前的回复中会带上 snapshot 快照版和release 正式版的仓库地址,但新版回复中仅告诉我们在 https://s01.oss.sonatype.org/ 上进行配置。我们打开他回复中提供的开发文档 https: //central.sonatype.org/pages/ossrh-guide.html#deployment 可以找到新的 snapshot 快照版和release 正式版的仓库地址。这个地址在下面进行的配置中会用到。
GPG签名
GPG 的主要作用是生成密钥对,发布到 Maven 仓库中的所有文件都要使用 GPG 签名,以保障完整性。使用 Windows 的可以下载gpg4win 地址:https://www.gpg4win.org/download.html ,安装后执行 gpg --gen-key 命令新建密钥对。
执行命令后,需要输入 姓名 和 邮箱 还有 Passphase(证书密码),Passphase 需要记住,在我们使用证书的时候会要求我们输入密码。
执行完之后我们会得到上图中的 pub 串,接着我们执行以下命令将 pub 上传到 key 验证库(后面的串替换为自己的)。
发布公钥
a.保证 gps 签名正常,多发几个服务器
gpg --gen-key
gpg --keyserver http://pool.sks-keyservers.net:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keyserver.ubuntu.com:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.gnupg.net:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.openpgp.org:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
b.验证是否发送成功
gpg --keyserver http://pool.sks-keyservers.net:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keyserver.ubuntu.com:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.gnupg.net:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.openpgp.org:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
四、Maven 配置
setting.xml
首先是 Maven 全局配置文件 setting.xml,路径为 $MAVEN_HOME/conf/settings.xml ,若不知道该配置文件在哪,可以执行 mvn -v 命令查看 Maven Home 的 bin 目录,setting.xml 文件在与 bin 目录同级的 conf 目录下。
配置 sonatype 系统的账户密码,复制以下配置,修改为自己用户名密码后,添加到 setting.xml 文件中的 … 节点。注意查看节点是否已被注释掉。
<servers>
<server>
<id>ossrh</id>
<username>username</username>
<password>password</password>
</server>
</servers>
pom.xml
若以下配置节点,有些童鞋不清楚该配置到什么位置的,可以参考下面我的配置示例。
项目 URL 、SCM信息、开发者信息。修改对应值为自己的
<url>https://github.com/wxdfun/bones-spring-boot-tools</url>
<scm>
<url>https://github.com/wxdfun/bones-spring-boot-tools</url>
<connection>scm:git:https://github.com/wxdfun/bones-spring-boot-tools.git</connection>
<developerConnection>scm:git:https://github.com/wxdfun/bones-spring-boot-tools.git</developerConnection>
<tag>HEAD</tag>
</scm>
<developers>
<developer>
<id>wxdfun</id>
<name>wxdfun</name>
<email>wxd@wxdfun.com</email>
<url>https://www.wxdfun.com</url>
<timezone>+8</timezone>
</developer>
</developers>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
Javadoc 和 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>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--部署-->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
GPG 签名插件
<!-- 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>
Maven 部署流程插件 Nexus Staging Maven
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<!-- 与 setting.xml 的 server 配置中的 sonatype-release 对应-->
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
snapshot 快照版和 release 正式版的仓库地址
<distributionManagement>
<repository>
<id>ossrh</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>ossrh</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
五、pom.xml 示例
下面是我自己一个项目中的 pom.xml 配置
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>springboot-base</name>
<description>Base tools for Spring Boot</description>
<groupId>com.ykkblog</groupId>
<artifactId>sb-base</artifactId>
<version>1.0.0</version>
<url>https://gitee.com/xxx/</url>
<properties>
<projectUrl>https://gitee.com/xxxx.git</projectUrl>
<serverId>ossrh</serverId><!-- 服务id 也就是setting.xml中的servers.server.id -->
<!-- 打包时跳过测试 -->
<skipTests>true</skipTests>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!--项目编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<scm>
<connection>${projectUrl}</connection>
<developerConnection>${projectUrl}</developerConnection>
<url>${projectUrl}</url>
</scm>
<developers>
<developer>
<id>id</id>
<name>name</name>
<email>xxx@163.com</email>
<url>${projectUrl}</url>
<timezone>+8</timezone>
</developer>
</developers>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<dependencies>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.18</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<!-- fastjson -->
</dependencies>
<build>
<plugins>
<!-- 编译插件,设置源码以及编译的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</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>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<!-- 部署 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- 部署 -->
<!-- Gpg Signature -->
<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>
<!-- Gpg Signature -->
<!--将组件部署到OSSRH并将其发布到Central Repository-->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>${serverId}</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<!--将组件部署到OSSRH并将其发布到Central Repository-->
</plugins>
</build>
<distributionManagement>
<repository>
<id>${serverId}</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>${serverId}</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
打包发布
完成以上配置,我们就可以往 Maven 中央仓库上传 jar 包了,我们使用 Maven 命令上传。
mvn clean deploy -P release
mvn clean deploy -DskipTests
-Dmaven.javadoc.skip=true
命令执行期间可能会要求输入证书密码,执行成功后显示如下(该示例是 snapshot 版本):
snapshot 和 release 版本区别在于版本号以 -SNAPSHOT 结尾,在 deploy 命令时,发布快照版本。而发布 release 版本的时候,项目版本号不能以 -SNAPSHOT 结尾,插件和依赖项也不能以 -SNAPSHOT结尾。
发布成功之后,则可以在 https://s01.oss.sonatype.org/ 中根据Group Id查找到你发布的 jar 包信息。
之后我们需要在工单页面留言,标识我们已经发布了第一个版本的 jar 包,激活中央仓库的同步。之后我们便能在 https://repo1.maven.org/maven2/ 中找到我们的 jar 包,阿里云等其他 Maven 仓库都会陆续进行同步。
最后记得点 Release
We have released our first artifact, please confirm, thanks very much.