注意点1: 测试gpg和javadoc插件不适用于jdk11,jdk8测试没问题
注意点2: 同一个版本号不可以重复发布
注意点3: 无法删除已发布的包

注册Sonatype的账户
地址
点击界面上方 Create 按钮即可创建Issue。
1 2 3 4 5 6 7 8
| Project: Community Support - Open Source Project Repository Hosting (OSSRH) Issue TypeRequired: New Project
Summary: Jar包的名称 # 注意这里,group id需要证实当前域是你本人所有的,我就在这里配错了,找了好半天,详细的过程可以看这里https://issues.sonatype.org/browse/OSSRH-79407 Group Id:io.github.wang-xiaowu Project URL:项目站点,如:https://gitee.com/snowheart/dingtalk-robot SCM url:项目源码仓库,如:https://gitee.com/snowheart/dingtalk-robot.git
|
配置完成后,等待这里通过就好

配置maven
修改setting文件
servers节点内部增加如下server配置
1 2 3 4 5
| <server> <id>ossrh</id> <username>wangxiaowu</username> <password>密码</password> </server>
|
修改项目pom.xml文件(以下插件版本适用jdk1.8)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<licenses> <license> <name>The ApacheSoftware License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <name>wangxiaowu</name> <email>943915349@qq.com</email> </developer> </developers> <scm> <tag>master</tag> <connection>scm:git:git@github.com:wang-xiaowu/behappy-redis.git</connection> <developerConnection>scm:git:ssh@github.com:wang-xiaowu/behappy-redis.git</developerConnection> <url>https://github.com/wang-xiaowu/behappy-redis/tree/master</url> </scm>
|
添加profile节点
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <profiles> <profile> <id>release</id> <build> <plugins> <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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.1</version> <configuration> <show>private</show> <nohelp>true</nohelp> <charset>UTF-8</charset> <encoding>UTF-8</encoding> <docencoding>UTF-8</docencoding> <additionalparam>-Xdoclint:none</additionalparam> <detectJavaApiLink>false</detectJavaApiLink> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <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> <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://s01.oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> <distributionManagement> <snapshotRepository> <id>ossrh</id> <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> </profile> </profiles>
|
配置pgp的公私钥信息
Windows下使用gpg4win来进行配置。下载地址 https://www.gpg4win.org/get-gpg4win.html
安装好之后,打开cmd控制台(如果提示gpg不是可执行程序或命令,请配置Path路径到 GnuPG 的bin目录),
生成密钥的命令 **gpg --gen-key**,过程中需要使用到姓名、邮箱等信息,这里的配置最好和Sonatype注册信息、pom文件配置信息保持一致,以免不必要的麻烦。
生成的过程中,会有个弹框要求输入Passphase信息,这个是密钥的密码,同样需要记牢。发布签名的过程中会用得到。
使用 gpg --list-keys 命令查询配置好的公私钥信息,使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公钥key> 将公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ C:\Users\Lwx> gpg --list-keys
C:/Users/Lwx/AppData/Roaming/gnupg/pubring.kbx
pub rsa2048 2018-12-15 [SC] [expires: 2020-12-14] 01C6D8A0B3C1DFD78CEE66E73B4D46C0075BD34C uid [ultimate] Wanxiang Liu <sxjwzxlwx@yeah.net> sub rsa2048 2018-12-15 [E] [expires: 2020-12-14]
$ C:\Users\Lwx> gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 01C6D8A0B3C1DFD78CEE66E73B4D46C0075BD34C
gpg: sending key 01C6D8A0B to hkp server keyserver.ubuntu.com
|
打包Deploy
1 2
| 使用 mvn clean deploy -DskipTests -P <profile-id> 命令,一键打包。 打包过程中会要求输入gpg密钥的Passphase信息,输入即可
|
正常这时候就完事儿了,然后进入https://repo1.maven.org/maven2/查看自己的包是否有正确发布,比如下图,如果没有,则进行手动发布release步骤

手动发布Release(可选)
1 2 3 4
| 进入https://s01.oss.sonatype.org/#stagingRepositories查看发布好的构件,点击左侧的Staging Repositories,一般最后一个就是刚刚发布的jar了,此时的构件状态为 Open 选中构件,并点击上方的 Close–>Confirm 在下边的Activity选项卡中查看状态。 当状态变成closed后,执行 Release–>Confirm 并在下边的Activity选项卡中查看状态。 成功后构件自动删除,一小段时间(约1-2个小时)后即可同步到maven的中央仓库。届时会有邮件通知。
|
扫尾工作
对sonatype 上的Issue增加Comment,留言致谢并表示发布已经完成,请工作人员关闭Issue。
在以下两个网站可以对自己上传的包进行搜索。
https://search.maven.org/
https://mvnrepository.com/