如何把自己的Jar包上传到 maven 官方仓库中,Maven上传图文讲解

来源:互联网 发布:js事件兼容性写法 编辑:程序博客网 时间:2024/04/28 23:50

之前有同学在本站发布过“Maven的Mirror和Repository 的详细讲解”,这种我们自己搭建仓库和私服,一般这种是服务于公司或者团体。但是针对小“ 团伙 ”或者单兵作战,或者我们希望开源,开发给其他人去用,那么我们可以直接提交到官方(maven.org ),然后dependency 去引入即可,如下图:

当然,上面是已经开源好的,在Maven公共库里面是存在的。

一、Maven中央仓库提交 · 前奏准备

开始要注意这个几个Maven相关地址:

① https://issues.sonatype.org 工单管理地址,就是申请上传资格和groupId 的地方。

② https://oss.sonatype.org/#welcome  构件仓库,把jar包上传到这里,Release 之后就会同步到maven中央仓库。

③ http://search.maven.org/  最终表现在可以在这里搜索到。

部分网址第一次打开有点慢,请稍等。

二、创建工单

从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 中添加如下代码。

  1. <server>
  2. <id>sonatype-nexus-snapshots</id>
  3. <username>https://issues.sonatype.org的账号</username>
  4. <password>https://issues.sonatype.org的密码</password>
  5. </server>
  6. <server>
  7. <id>sonatype-nexus-staging</id>
  8. <username>https://issues.sonatype.org的账号</username>
  9. <password>https://issues.sonatype.org的密码</password>
  10. </server>

2.配置使用项目中的 pom.xml 

  1. <parent>
  2. <groupId>org.sonatype.oss</groupId>
  3. <artifactId>oss-parent</artifactId>
  4. <version>7</version>
  5. </parent>
  6. <licenses>
  7. <license>
  8. <name>The Apache Software License, Version 2.0</name>
  9. <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
  10. <distribution>repo</distribution>
  11. </license>
  12. </licenses>
  13. <!--写你自己的,这只是个例子-->
  14. <scm>
  15. <url> https://github.com/CallThink/Tools</url>
  16. <connection>https://github.com/CallThink/Tools.git</connection>
  17. <developerConnection>https://github.com/CallThink/Tools</developerConnection>
  18. </scm>
  19. <!--写你自己的,这只是个例子-->
  20. <developers>
  21. <developer>
  22. <name>changfeng</name>
  23. <email> changfeng @allytel.com.cn</email>
  24. <url>https://github.com/CallThink/Tools</url>
  25. </developer>
  26. </developers>

四、GPG环境

我们需要一个GPG环境,用来对上传的文件进行加密和签名,保证你的jar包不被篡改,下载地址:https://www.gpg4win.org/ 

安装完成后 命令行窗口中输入 gpg –version 查看是否安装成功

成功后执行下列命令:

  1. C:\Windows\system32>gpg --gen-key
  2. gpg (GnuPG) 2.0.30; Copyright (C) 2015 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. Please select what kind of key you want:
  6. (1) RSA and RSA (default)
  7. (2) DSA and Elgamal
  8. (3) DSA (sign only)
  9. (4) RSA (sign only)
  10. Your selection? 1
  11. RSA keys may be between 1024 and 4096 bits long.
  12. What keysize do you want? (2048) 2048
  13. Requested keysize is 2048 bits
  14. Please specify how long the key should be valid.
  15. 0 = key does not expire
  16. <n> = key expires in n days
  17. <n>w = key expires in n weeks
  18. <n>m = key expires in n months
  19. <n>y = key expires in n years
  20. Key is valid for? (0) 0
  21. Key does not expire at all
  22. Is this correct? (y/N) y
  23. GnuPG needs to construct a user ID to identify your key.
  24. Real name: changfeng
  25. Email address: 1010667394@qq.com
  26. Comment: changfeng-chang
  27. You selected this USER-ID:
  28. "changfeng (changfeng-chang) <1010667394@qq.com>"
  29. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
  30. You need a Passphrase to protect your secret key.
  31. We need to generate a lot of random bytes. It is a good idea to perform
  32. some other action (type on the keyboard, move the mouse, utilize the
  33. disks) during the prime generation; this gives the random number
  34. generator a better chance to gain enough entropy.
  35. We need to generate a lot of random bytes. It is a good idea to perform
  36. some other action (type on the keyboard, move the mouse, utilize the
  37. disks) during the prime generation; this gives the random number
  38. generator a better chance to gain enough entropy.
  39. gpg: key EFFF81F9 marked as ultimately trusted
  40. public and secret key created and signed.
  41. gpg: checking the trustdb
  42. gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
  43. gpg: depth: 0 valid: 3 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 3u
  44. pub 2048R/EFFF81F9 2017-09-12
  45. Key fingerprint = B245 4560 4E6F EA74 FA7A 5ED2 5C48 1BA6 EFFF 81F9
  46. uid [ultimate] changfeng (changfeng-chang) <1010667394@qq.com>
  47. sub 2048R/8477CCD1 2017-09-12

其中EFFF81F9 就是申请的key 

过程中需要填写一个密码,记住他,下一步打包上传的时候需要它。

五、上传发布Jar到Maven中央仓库

BUILD SUCCESS成功之后执行下列命令(gpg --list-keys 命令可以查看自己的key)

  1. C:\Windows\system32>gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 5C1845F3
  2. gpg: sending key 5C1845F3 to hkp server keyserver.ubuntu.com

会在中间仓库中Staging Repositories 菜单中看到自己刚刚上传的jar 包,如果没有错的话,选中它然后点Release 就可以同步到中央仓库了,一般过十分钟左右就能在http://search.maven.org/上边搜到了。

转载出处:http://www.sojson.com/blog/250.html