继(一)Jar包发布到Maven中央仓库之域名认证项目申请通过之后,我们就可以开始准备发布的事情了。
安装并配置gpg
安装gpg
- Windows系统可以访问https://www.gpg4win.org/download.html进行下载;
- Mac系统可以通过以下脚本安装
brew install gpg
安装完成后验证是否成功:
gpg --version
配置gpg
- 生成密钥对
gpg --gen-key
按提示填写名字和邮箱等等基本信息,这些都不是重点,最主要的是有个Passphase
的选项在填完之后记下来,到时候发布 jar 包的时候要用到。
- 查看生成的密钥,并上传至密钥服务器 通过
gpg --list-keys
可以查看密钥相关信息,例如我的是:
然后通过以下命令上传到服务器:
// 上传
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys ${xxx截图位置红色框的部分}
// 验证
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --receive-keys ${xxx截图位置红色框的部分}
配置Maven
相关信息
Maven
的settings.xml
增加sonatype
相关账号信息
<server>
<id>sonatype-snapshot</id>
<username>sonatype账号</username>
<password>sonatype密码</password>
</server>
<server>
<id>sonatype-release</id>
<username>sonatype账号</username>
<password>sonatype密码</password>
</server>
- 项目的
pom.xml
增加作者、项目相关信息
<!-- LICENSE信息 -->
<licenses>
<license>
<name>The Apache Software License, Version2.0</name>
<url>http://www.apache.org/licenses/</url>
<distribution>repo</distribution>
</license>
</licenses>
<!-- 项目信息 -->
<scm>
<url>https://github.com/zzq0324/feature-flag</url>
<connection>https://github.com/zzq0324/feature-flag.git</connection>
<developerConnection>https://github.com/zzq0324/feature-flag</developerConnection>
</scm>
<!-- 开发者信息 -->
<developers>
<developer>
<name>zzq0324</name>
<email>zzq0324@qq.com</email>
<url>https://www.zzq0324.cn</url>
</developer>
</developers>
<!--仓库地址配置信息-->
<distributionManagement>
<snapshotRepository>
<!--id 要与setting.xml server id一致 -->
<id>sonatype-snapshot</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<!--id 要与setting.xml server id一致 -->
<id>sonatype-release</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
- 增加gpg以及文档生成的构建插件
<!--java source生成插件-->
<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>
<!--java doc生成插件-->
<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>
<!--gpg签名插件-->
<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>
完整信息可以参考feature-flag pom.xml
发布
本地Deploy
进入项目路径,执行以下命令即可发布:
mvn clean deploy -Dgpg.passphrase=${gpg初始化的Passphase}
sonatype后台release
更多发布的教程详见官方说明:Releasing Deployment from OSSRH to the Central Repository - Introduction
常见问题
- Deploy的时候提示
gpg: signing failed: Inappropriate ioctl for device
解决办法如下:// 先执行以下命令 export GPG_TTY=$(tty) // 再执行发布的命令 mvn clean deploy
- 发布的时候提示
Authorization failed
、403 Forbidden
完整错误如下:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project feature-flag: Failed to deploy artifacts: Could not transfer artifact cn.zzq0324:feature-flag:jar:1.0.1 from/to sonatype-release (https://oss.sonatype.org/service/local/staging/deploy/maven2): Authorization failed for https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/zzq0324/feature-flag/1.0.1/feature-flag-1.0.1.jar 403 Forbidden -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
后面通过搜索发现,我使用的是地址是https://oss.sonatype.org
,替换为https://s01.oss.sonatype.org
之后解决。详细可以查看OSSRH-71278
- oss sonatype执行close操作的时候报错
Invalid POM:/cn/zzq0324/feature-flag/1.0.1/feature-flag-1.0.1.pom:Project name missing,Project description missing,Project URL missing
此时需要在项目中加入项目相关的描述
<name>Feature Flag</name>
<description>A feature flag tool to control new feature's open status.</description>
<url>https://github.com/zzq0324/feature-flag</url>