汪献军(wx:icode-pm): 工作地:南京 做了19年的WEB应用开发,转型做了产品经理,偶尔做做Python编码。崇拜老师,乐于助人。做事有强迫症,强调持之以恒。 |
想一想,自己写的jar包能被异地的朋友直接在maven的pom文件中引用,而不需要把程序发给他,告诉他怎么做怎么做,这是不是很酷?
以前没有想过把自己写的java代码打包成jar上传到maven官方仓库,2018年认识了闲大赋,他有个开源作品beetl就把jar传到了maven官方仓库,于是我跟他取经,做了下面的事情。
一、创建工单
从https://issues.sonatype.org 注册一个帐号(已经有请忽略),账号密码要记住,下面登录账号密码还要用这个,后续还要配置 maven 的setting.xml
中,然后创建一个工单,如下图(记住是先注册,下面是创建工单的图)。
记住是先注册,再创建。
其中需要注意的地方:
①Group Id
,唯一标识,我用的是com.github.xxxxx
如果用的是其他的比如:com.sojson.core
之类的,管理员会问你这个是不是属于你的网站,告诉他就可以了(用com.github.xxxxx
这种比较方便)。官方文档:http://central.sonatype.org/pages/choosing-your-coordinates.html
② ProjectURL
,填项目源码的地址,官方有一篇文章说,如果不想公布源码,那么填写一个只含README
的项目的地址就可以了。
*其实管理员主要就是审核Group Id,其他的不重要
然后点击左侧列表中的Resolved recently
可以找到你刚创建的工单。
这时你的工单的状态Status
是Open
,等到审核通过状态会变为RESOLVED
,第一次审核要一天左右 ,因为时差原因,他们工作时间是我们的晚上,之后在创建工单如果GroupId
满足基本要求基本就是秒过。
二、配置Maven Settings.xml & pom.xml
1、配置 Maven 中的conf/settings.xml
,然后在servers
中添加如下代码。
sonatype-nexus-snapshots https://issues.sonatype.org的账号 https://issues.sonatype.org的密码 sonatype-nexus-staging https://issues.sonatype.org的账号 https://issues.sonatype.org的密码
2、配置使用项目中的 pom.xml
org.sonatype.oss oss-parent 7 The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt repo wang.xianjun xuejava@qq.com https://gitee.com/icode-studio_admin
三、GPG环境
我们需要一个GPG环境,用来对上传的文件进行加密和签名,保证你的jar包不被篡改,下载地址:https://www.gpg4win.org/
安装完成后 命令行窗口中输入 gpg –version 查看是否安装成功
成功后执行下列命令:
gpg --gen-keygpg (GnuPG) 2.2.10; Copyright (C) 2018 Free Software Foundation, Inc.This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Note: Use "gpg --full-generate-key" for a full featured key generation dialog.GnuPG needs to construct a user ID to identify your key.Real name: xianjunEmail address: xuejava@qq.comYou selected this USER-ID: "xianjun "Change (N)ame, (E)mail, or (O)kay/(Q)uit? OWe need to generate a lot of random bytes. It is a good idea to performsome other action (type on the keyboard, move the mouse, utilize thedisks) during the prime generation; this gives the random numbergenerator a better chance to gain enough entropy.We need to generate a lot of random bytes. It is a good idea to performsome other action (type on the keyboard, move the mouse, utilize thedisks) during the prime generation; this gives the random numbergenerator a better chance to gain enough entropy.gpg: key 794C231BD6E8CD67 marked as ultimately trustedgpg: directory 'C:/Users/Wang Xianjun/AppData/Roaming/gnupg/openpgp-revocs.d' createdgpg: revocation certificate stored as 'C:/Users/Wang Xianjun/AppData/Roaming/gnupg/openpgp-revocs.d\FF64AB401182B2224653EE2EE2FBF4046F12B0CE.rev'public and secret key created and signed.pub rsa2048 2018-10-08 [SC] [expires: 2020-10-07] FF*************************************CEuid xianjun sub rsa2048 2018-10-08 [E] [expires: 2020-10-07]
在命令行内按提示填写信息,结果信息中的794C231BD6E8CD67即为申请的key
。
过程中需要填写一个密码,记住它,下一步打包上传的时候需要它。
四、上传发布Jar到Maven中央仓库
注意上图中的3个红框内文字:
Profiles填写固定值:sonatype-oss-release
参数 gpg.passphrase填写第三步生成的KEY:794C231BD6E8CD67
在编译发布的过程中,需要输入第三步设置的密码。
BUILD SUCCESS成功之后执行下列命令(gpg --list-keys 命令可以查看自己的key)
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 794C231BD6E8CD67gpg: sending key 794C231BD6E8CD67 to hkp://keyserver.ubuntu.com:11371
五、登录发布
登录网站,并进入“Staging Repositories”https://oss.sonatype.org/
找到第四部发布上传的版本,点击“close”按钮
如无问题,则close成功。否则根据提示问题修改重新发布。
选择close状态的版本,点击“release”按钮。
release成功后,重新打开第一步创建的工单,告诉客服已经发布成功。客服会帮助你同步到maven官方仓库。
同步到maven官方仓库之后,就能在http://search.maven.org/上边搜到你的jar包maven引用了。