如何把自己的jar包上传到maven官方库中
我们的目的:
这条博客是半年前写的,偶然想起没发布,写了能白写了,现在就贴上来,现在专心做.net了没再研究java了
一般情况下,我们的jar包只在团队内部使用的话,搭建一个maven私服就能满足需求。但是有时候我们希望别人也能使用,如果能把自己的jar包上传到maven中央仓库,只要配置pox.xml就可以使用我们的jar包,这样就非常方便了。
①https://issues.sonatype.org 工单管理地址,就是申请上传资格和groupid的地方
②https://oss.sonatype.org/#welcome 构件仓库,把jar包上传到这里,Release之后就会同步到maven中央仓库
③http://search.maven.org/ 最终表现在可以在这里搜索到
一、 创建工单:
注册一个账号,账号密码要记住,登录到第二个网站的账号密码也用这个,后续还要配置maven的setting.xml中,然后创建一个工单。
其中需要注意的地方:
① Group Id,唯一标识,我用的是com.github.xxxxx 如果用的是其他的比如:com.bbb.ccc之类的,管理员会问你这个是不是属于你的网站,告诉他就可以了(用com.github.xxxxx这种比较方便)。官方文档:http://central.sonatype.org/pages/choosing-your-coordinates.html
② ProjectURL,填项目源码的地址,官方有一篇文章说,如果不想公布源码,那么填写一个只含README的项目的地址就可以了。
*其实管理员主要就是审核Group Id,其他的不重要
然后点击左侧列表中的Resolved recently 可以找到你刚创建的工单,
这时你的工单的状态Status是Open,等到审核通过状态会变为RESOLVED,第一次审核要一天左右 ,因为时差原因,他们工作时间是我们的晚上,之后在创建工单如果GroupId满足基本要求基本就是秒过。
二、 配置xml文件
Maven的conf /setting.xml:
在servers 标签中增加:
<server>
<id>sonatype-nexus-snapshots</id>
<username>https://issues.sonatype.org的账号</username>
<password>https://issues.sonatype.org的密码</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>https://issues.sonatype.org的账号</username>
<password>https://issues.sonatype.org的密码</password>
</server>
项目中的pom.xml:
增加
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<licenses>
<license>
<name>The Apache Software License, Version2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm> --------写你自己的,这只是个例子
<url> https://github.com/CallThink/Tools</url>
<connection>https://github.com/CallThink/Tools.git</connection>
<developerConnection>https://github.com/CallThink/Tools</developerConnection>
</scm>
<developers> --------写你自己的,这只是个例子
<developer>
<name>changfeng</name>
<email> changfeng @allytel.com.cn</email>
<url>https://github.com/CallThink/Tools</url>
</developer>
</developers>
注意:
<groupId>com.github.callthink</groupId> -----必须写我们在①地址中申请的GroupId
<packaging>jar</packaging> -----必须写jar,因为我们上传的就是jar包
三、 GPG环境
我们需要一个GPG环境,用来对上传的文件进行加密和签名,保证你的jar包不被篡改
安装完成后命令行窗口中输入gpg –version查看是否安装成功
成功后执行下列命令:
C:\Windows\system32>gpg--gen-key
gpg (GnuPG) 2.0.30; Copyright(C) 2015 Free Software Foundation, Inc.
This is free software: you arefree to change and redistribute it.
There is NO WARRANTY, to theextent permitted by law.
Please select what kind of keyyou want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024and 4096 bits long.
What keysize do you want?(2048) 2048
Requested keysize is 2048 bits
Please specify how long thekey should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct auser ID to identify your key.
Real name: changfeng
Email address:[email protected]
Comment: changfeng-chang
You selected this USER-ID:
"changfeng (changfeng-chang)<[email protected]>"
Change (N)ame, (C)omment,(E)mail or (O)kay/(Q)uit? o
You need a Passphrase toprotect your secret key.
We need to generate a lot ofrandom bytes. It is a good idea to perform
some other action (type on thekeyboard, move the mouse, utilize the
disks) during the primegeneration; this gives the random number
generator a better chance togain enough entropy.
We need to generate a lot ofrandom bytes. It is a good idea to perform
some other action (type on thekeyboard, move the mouse, utilize the
disks) during the primegeneration; this gives the random number
generator a better chance togain enough entropy.
gpg: key EFFF81F9 marked asultimately trusted
public and secret key createdand signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1complete(s) needed, PGP trust model
gpg: depth: 0 valid: 3 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 3u
pub 2048R/EFFF81F9 2017-09-12
Key fingerprint = B245 4560 4E6F EA74FA7A 5ED2 5C48 1BA6 EFFF 81F9
uid [ultimate] changfeng (changfeng-chang)<[email protected]>
sub 2048R/8477CCD1 2017-09-12
其中EFFF81F9 就是申请的key
过程中需要填写一个密码,记住他,下一步打包上传的时候需要它
四、 上传发布
使用eclipse maven插件上传
BUILD SUCCESS成功之后执行下列命令(gpg--list-keys 命令可以查看自己的key)
C:\Windows\system32>gpg--keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 5C1845F3
gpg: sending key 5C1845F3 tohkp server keyserver.ubuntu.com
会在中间仓库中Staging Repositories菜单中看到自己刚刚上传的jar包,如果没有错的话,选中它然后点Release就可以同步到中央仓库了,一般过十分钟左右就能在http://search.maven.org/上边搜到了