Maven公共中央仓库发布自己的Jar包
流程概要
- 创建工单
- 配置环境和项目
- 发布jar包
一、创建工单
1. 注册账号
https://issues.sonatype.org/secure/Signup!default.jspa ,前往该地址注册一个 sonatype 账号
2. 创建工单
登录注册的账号,页面控制面板上方菜单,有一个新建按钮(由于语言设置的不同,你的界面可能是英文)。点击,创建一个工单,如下图:
如果你有自己的域名,可以写你自己域名。没有的话可以用github账号作为组织域名标识。假如你的github用户名为xxx, 项目名为simple,则可以这样填写:
项目:Community Support - Open Source Project Repository Hosting (OSSRH)
问题类型:New Project
Group Id:com.github.xxx
Project URL:https://github.com/xxx/simple
SCM url:https://github.com/xxx/simple.git
创建后进入自己的工单,如下所示:
我的工单已经被管理员审批通过,所以是已解决状态。没有审批过的是开放状态,需要等待管理员的审批,由于时区不同,东半球的白天,西半球却是黑夜,耐心等待即可。
二、配置环境和项目
1. 配置maven setting.xml
上传jar到公共的中央仓库,首先要在setting.xml加入一些配置,如下
<servers>
<server>
<id>snapshot</id>
<username>sonatype用户名</username>
<password>sonatype密码</password>
</server>
<server>
<id>release</id>
<username>sonatype用户名</username>
<password>sonatype密码</password>
</server>
</servers>
2. 配置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>
<groupId>com.github.rxyor</groupId>
<artifactId>carp-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>carp</name>
<description>carp is project which contains common utils ,redis delay job , distributed lock and
so on!
</description>
<url>https://github.com/rxyor/carp</url>
<properties>
<!--project-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--plugin-->
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-clean-plugin.version>3.0.0</maven-clean-plugin.version>
<maven-resources-plugin.version>3.0.2</maven-resources-plugin.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.20.1</maven-surefire-plugin.version>
<maven-war-plugin.version>3.2.0</maven-war-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
</properties>
<!--构建配置-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://github.com/rxyor/carp</url>
<connection>scm:git:https://github.com/rxyor/carp.git</connection>
<developerConnection>scm:git:https://github.com/rxyor/carp</developerConnection>
</scm>
<developers>
<developer>
<name>rxyor</name>
<email>rxyor@outlook.com</email>
<url>https://github.com/rxyor</url>
</developer>
</developers>
<profiles>
<profile>
<id>release</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<properties>
<additionalparam>-Xdoclint:none</additionalparam>
</properties>
<distributionManagement>
<snapshotRepository>
<!--id 要与setting.xml server id一致-->
<id>snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<!--id 要与setting.xml server id一致-->
<id>release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>release</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
<execution>
<id>snapshot</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<executions>
<execution>
<id>release</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<serverId>release</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
3. pom.xml配置说明
上传到中央仓库并可以成功发布有以下前提条件:
- 包含jar源码包
- 包含java doc
- 包含签名校验
- 版本号包含snapshot的可能也无法发布成功
等…
pom.xml配置的几个插件就是解决上述问题的,
- maven-source-plugin用于生成源码包
- maven-javadoc-plugin用于生成java doc,大部分代码的java doc注释都不规范,为了防止不规范的java doc注释导致打包失败,插件可以加入如下配置:
<configuration>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
- maven-gpg-plugin用于添加签名
4. 安装并配置gpg
#安装gpg
brew install gpg
#生成key
gpg --gen-key
#输入对应信息
Real name:
Email address:
#查询生成的公钥的信息
gpg --list-keys
---------------------------------
pub rsa2048 2019-05-30 [SC] [expires: 2021-05-29]
B619ABB4B37B166061CD17352FCF6A922383XXXX
uid [ultimate] rxyor <rxyor@outlook.com>
sub rsa2048 2019-05-30 [E] [expires: 2021-05-29]
#上传到密钥服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: sending key 2FCF6A922383D13B to hkp://keyserver.ubuntu.com:11371
#查询密钥是否上传成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: key 2FCF6A922383D13B: "rxyor <rxyor@outlook.com>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
三、发布jar包
1. deploy
工程根目录打开命令行工具,输入如下命令:
mvn clean deploy -P release -Darguments=gpg.passphrase="gpg密钥密码"
即使按照上述教程配置,你有可能遇到如下错误:
1) gpg签名错误
[INFO] --- maven-gpg-plugin:1.6:sign (release) @ carp-parent ---
gpg: signing failed: Inappropriate ioctl for device
gpg: signing failed: Inappropriate ioctl for device
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (release) on project carp-parent: Exit code: 2 -> [Help 1]
如果发生错误,你的gpg可能需要额外的配置,以Mac为例,在gpg安装目录(~/.gnup)下建立两个配置文件:gpg.conf、gpg-agent.conf
# gpg.conf中加入
use-agent
pinentry-mode loopback
# gpg-agent.conf中加入
allow-loopback-pinentry
2) 400或401错误
请注意检查是否存在以下情况:
- 发布到release时版本号不能包含snapshot
- setting.xml 密码配置不对
- setting.xml 中server id 与pom.xml配置不一致
- maven环境变量配置setting.xml位置与自己配置的setting.xml不是同一个
解决上述错误后,再次执行上述命令,不出意外的话应该就可以deploy成功了。
2. Staging Repositories 关闭仓库操作
前往https://oss.sonatype.org/#stagingRepositories ,并使用sonatype账户登录, 按照下图所示关闭仓库
关闭仓库的时候需要检查是否出现错误,如果有错误需要按错误提示信息进行解决,如下图可以查看错误信息:
但实际上,由于pom.xml加入了插件:nexus-staging-maven-plugin,这插件在deploy成功后会自动帮你进行Staging Repositories Close操作。
3. 提醒管理员审批
关闭成功后,还需要在你的工单中提醒管理员你的组件发布成功,可以添加如下注释:
Component has been successfully issued
管理审批通过后,2个小时左右你就能在公共中央仓库搜索到发布的jar包了。
4. 中央仓库搜索发布的jar包
中央仓库搜索地址:https://search.maven.org/