做为 Java 开发者(伪),工做中必定离不开 Maven。 偶尔也须要发布本身的构件到 Maven 中央仓库中(oss.sonatype.org/)。 可是常常有这样那样的坑(由于老是换电脑 XD),在这里记录一下,以备后续查阅。java
将项目发布到 maven 中央仓库的通常步骤以下:apache
注册Sonatype的帐户。地址:issues.sonatype.org/secure/Sign…bash
提交发布申请。(仅第一次)服务器
Community Support - Open Source Project Repository Hosting
groupId
对应的域名你须要有全部权使用 GPG 生成密钥对。app
Windows 安装:gpg4win.org/jsp
Mac 安装:brew install gpg
maven
gpg --version
检查是否安装成功gpg --gen-key
生成密钥对gpg --list-keys
查看公钥gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥ID
将公钥发布到 PGP 密钥服务器gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公钥ID
查询公钥是否发布成功配置 maven。 须要修改的 Maven 配置文件包括:setting.xml
(全局级别)与 pom.xml
(项目级别)。ui
setting.xml
配置一览url
<settings>
...
<servers>
<server>
<id>snapshotRepository-id</id>
<username>用户名</username>
<password>密码</password>
</server>
</servers>
...
</settings>
复制代码
使用本身注册的 Sonatype 帐号的用户名与密码来配置以上 server 信息。spa
此处 id snapshotRepository-id
应和下面 pom.xml
中 snapshotRepository 和 repository 里面的 id 保持一致。
pom.xml
配置一览<project>
...
<name>your project's name</name>
<description>your project's description</description>
<url>http://www.chengww.com/</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>chengww</name>
<email>chengww5217@163.com</email>
</developer>
</developers>
...
<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>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.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>snapshotRepository-id</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>snapshotRepository-id</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
...
</project>
复制代码
注意:以上 pom.xml
必须包括:name、description、url、licenses、developers、scm 等基本信息,此外,使用了 Maven 的 profile 功能,只有在 release 的时候,建立源码包、建立文档包、使用 GPG 进行数字签名。此外,snapshotRepository 与 repository 中的 id 必定要与 setting.xml
中 server 的 id 保持一致。
上传构件到 OSS 中。mvn clean deploy -P release
在 OSS 中发布构件。进入 oss.sonatype.org/,点击Staging Repositories
-> 在搜索栏输入你的 groupId -> 勾选你的构件并点击 close -> 点击 tab 栏的 release。
通知 Sonatype 的工做人员关闭 issue。(仅第一次)
参考地址:my.oschina.net/huangyong/b…
等待审批经过后,就能够在中央仓库中搜索到本身发布的构件了!
可是,事情并非那么简单的。老是会出现这样那样的坑。
出现该步骤实际上是输入密码的步骤。可是因为是中文的缘故,消息提示乱码了。
只须要在下面横线上输入密码以后,将光标移动到下面的好,回车便可。注意,密码须要输入两次,请保持两次一致。
因安装了新版的 gpg,在 gpg --list-keys
时显示以下:
pub rsa2048 2019-04-12 [SC] [有效至:2021-04-11]
9A1640F7A2551131612D51B12D83594B7B29D86A
uid [ 绝对 ] chengww <chengww5217@163.com>
sub rsa2048 2019-04-12 [E] [有效至:2021-04-11]
复制代码
发布公钥到服务器时,填的公钥 ID 为 9A1640F7A2551131612D51B12D83594B7B29D86A
,终端上显示为:
gpg --keyserver hkp://subkeys.pgp.net --send-keys 9A1640F7A2551131612D51B12D83594B7B29D86A
...
gpg: 正在发送密钥 2D83594B7B29D86A 到 hkp://subkeys.pgp.net
gpg: 发送至公钥服务器失败:Server indicated a failure
gpg: 发送至公钥服务器失败:Server indicated a failure
复制代码
只须要将公钥 ID 从 9A1640F7A2551131612D51B12D83594B7B29D86A
修改为 2D83594B7B29D86A
便可:
gpg --keyserver hkp://subkeys.pgp.net --send-keys 2D83594B7B29D86A
复制代码
settings.xml
文件通常存在于两个位置: 全局配置: ${M2_HOME}/conf/settings.xml
用户配置:${user.home}/.m2/settings.xml
若是实在是不清楚的,请自行 mvn -X
查看:
...
[DEBUG] Reading global settings from /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml
[DEBUG] Reading user settings from /Users/chengww/.m2/settings.xml
...
复制代码
关于 setting.xml
相关讲解参见:www.jianshu.com/p/110d897a5…
401 错误,通常都是未在 setting.xml
中设置用户名密码所致(或用户名密码不正确)。
参见上述 4.配置 maven 配置下 setting.xml
。
缘由是新版 gpg 在当前终端没法弹出密码输入页面。
解决:
export GPG_TTY=$(tty)
复制代码
在当前终端中 export(临时解决)
或者加入到 ~/.bash_profile
,而后 source ~/.bash_profile
执行上述命令后在 IntelliJ IDEA 中的终端(Terminal)中仍是不能弹出密码输入界面,且报上面的错。
这个时候就要到系统的的终端,cd 到项目目录,而后执行 mvn clean deploy -P release
路漫漫其修远兮,愿天下没有 BUG。