如何发布Jar包到Maven中央仓库

本文系转载,原文地址:

https://www.iteblog.com/archives/1807.html


本博客曾经介绍了《如何手动添加依赖的jar文件到本地Maven仓库》这里的方法非常的简单,而且局限性很大:只能提供给本人开发使用,无法共享给其他需要的人。本文将介绍如何把自己开发出来的Java包发布到Maven中央仓库(http://search.maven.org/),这样任何人都可以搜索到这个包并使用它。如果你现在还不了解Maven是啥东西,请你先找本Maven书看看。

将自己的Java包发布到Maven中央仓库需要经过许多繁琐的步骤,本文将详细地介绍这些步骤。相信大家在看完本文之后也会发布jar包到Maven中央仓库。

文章目录

创建工单

在发布Java包到Maven中央仓库首先需要在https://issues.sonatype.org/secure/Dashboard.jspa网站创建一个工单(Issues),第一次使用这个网站的时候需要注册自己的帐号(这个帐号和密码需要记住,后面会用到),之后创建自己的Issue,点击导航最上面的Create按钮,然后会弹出下面的对话框,将ProjectIssue Type设置为下图的内容:


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop

然后根据你Java包的功能分别写上SummaryDescriptionGroup IdSCM url以及Project URL等必要信息,可以参见我之前创建的Issue:https://issues.sonatype.org/browse/OSSRH-24874。创建完之后需要等待Sonatype的工作人员审核处理,审核时间还是很快的,我的审核差不多等待了两小时。当Issue的Status变为RESOLVED后,就可以进行下一步操作了。

如果你的Group Id填写的是自己的网站(我的就是这种情况),Sonatype的工作人员会询问你那个Group Id是不是你的域名,你只需要在上面回答是就行,然后就会通过审核。

使用gpg生成密钥对

上面创建的issuce经过审核之后,我们可以使用gpg生成密钥对,这里分两种情况:

1、如果使用的是Windows,可以到https://www.gpg4win.org/download.html下载gpg4win,推荐使用 Gpg4win-Vanilla 2.3.3版本,因为它仅包括 GnuPG,这个工具才是我们所需要的;

2、如果使用的是Linux,可以通过yum install gpg命令安装gpg。

之后可以通过gpg --version命令查看是否安装成功,如果出现版本等信息说明安装成功了,如下:

<pre class="brush: bash; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[iteblog@www.iteblog.com ~] $ gpg --version
gpg (GnuPG) 2.0.14
libgcrypt 1.4.5
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: ~/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA
Cipher: 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128,
CAMELLIA192, CAMELLIA256
Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
</pre>

安装完gpg之后,下面一次按照如下的命令设置好你的名字,邮箱,其他步骤可以使用默认值。不过输入Passphrase的值需要记住,这个相当于密钥的密码,发布过程中进行签名操作的时候会用到:

<pre class="brush: bash; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ gpg --gen-key
gpg (GnuPG) 1.4.19; Copyright (C) 2015 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.

Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key 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)
Key does not expire at all
Is this correct? (y/N) Y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) heinrichh@duesseldorf.de"

Real name: iteblog
Email address: wyphao.2007@163.com
Comment: flink-elasticsearch-connector
You selected this USER-ID:
"iteblog (flink-elasticsearch-connector) wyphao.2007@163.com"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
+++++
.+++++
gpg: /c/Users/iteblog/.gnupg/trustdb.gpg: trustdb created
gpg: key B15C5AA3 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
pub 2048R/B15C5AA3 2016-09-19
Key fingerprint = DB61 9873 924C 020E 20E7 E461 0170 C912 B15C 5AA3
uid iteblog (flink-elasticsearch-connector) wyphao.2007@163.com
sub 2048R/31A906E1 2016-09-19
</pre>

到这里我们就设置好密钥对了。上面代码中导数第四行的B15C5AA3需要记住,其相当于我们生成的key,后面会用到。

设置Maven配置

为了发布更简便,我们可以工程的pom.xml文件里面加入以下的配置:

<pre class="brush: xml; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
</pre>

并增加Licenses、SCM、Developers信息:

<pre class="brush: xml; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/397090770/flink-elasticsearch2-connector</url>
<connection>git@github.com:397090770/flink-elasticsearch2-connector.git</connection>
<developerConnection>https://www.iteblog.com</developerConnection>
</scm>

<developers>
<developer>
<name>iteblog</name>
<email>wyphao.2007@163.com</email>
<url>https://www.iteblog.com</url>
</developer>
</developers>
</pre>

最后修改Maven的setting.xml配置文件,设置如下:

<pre class="brush: xml; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><servers>
<server>
<id>sonatype-nexus-snapshots</id>
<username>Sonatype网站的账号</username>
<password>Sonatype网站的密码</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>Sonatype网站的账号</username>
<password>Sonatype网站的密码</password>
</server>
</servers>
</pre>

上面的username和password就是你在步骤一注册的帐号和密码。

部署和发布Jar包

配置好pom.xml文件和Maven的setting.xml文件之后,我们就可以部署Jar包了,运行如下命令:

<pre class="brush: bash; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ mvn clean deploy -P sonatype-oss-release -Darguments="gpg.passphrase=设置gpg设置密钥时候输入的Passphrase"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building flink-elasticsearch2-connector 1.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ flink-elasticsearch2-connector ---
[INFO] Deleting D:\work\flink-elasticsearch2-connector\target
[INFO]
[INFO] --- maven-enforcer-plugin:1.0:enforce (enforce-maven) @ flink-elasticsearch2-connector ---
[INFO]
[INFO] --- maven-source-plugin:2.1.2:jar-no-fork (attach-sources) @ flink-elasticsearch2-connector ---
[INFO]
[INFO] --- maven-javadoc-plugin:2.7:jar (attach-javadocs) @ flink-elasticsearch2-connector ---
[INFO] Not executing Javadoc as the project is not a Java classpath-capable package
[INFO]
[INFO] --- maven-gpg-plugin:1.1:sign (sign-artifacts) @ flink-elasticsearch2-connector ---
GPG Passphrase: ***************** *[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ flink-elasticsearch2-connector ---
[INFO] Installing D:\work\flink-elasticsearch2-connector\pom.xml to D:\q\repos\com\iteblog\flink-elasticsearch2-connector\1.0.1\flink-elasticsearch2-connector-1.0.1.pom
[INFO] Installing D:\work\flink-elasticsearch2-connector\target\flink-elasticsearch2-connector-1.0.1.pom.asc to D:\q\repos\com\iteblog\flink-elasticsearch2-connector\1.0.1\flink-elasticsearch2-connector-1.0.1.pom.asc
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ flink-elasticsearch2-connector ---
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/1.0.1/flink-elasticsearch2-connector-1.0.1.pom
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/1.0.1/flink-elasticsearch2-connector-1.0.1.pom (4 KB at 0.4 KB/sec)
Downloading: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/maven-metadata.xml
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/maven-metadata.xml
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/maven-metadata.xml (321 B at 0.0 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/1.0.1/flink-elasticsearch2-connector-1.0.1.pom.asc
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/com/iteblog/flink-elasticsearch2-connector/1.0.1/flink-elasticsearch2-connector-1.0.1.pom.asc (473 B at 0.1 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.272s
[INFO] Finished at: Mon Sep 19 14:15:21 CST 2016
[INFO] Final Memory: 21M/224M
[INFO] ------------------------------------------------------------------------
</pre>

运行上面的命令之后,我们再上传到第三方的key验证库,如下:

<pre class="brush: bash; title: ; notranslate" title="" style="padding: 8px; color: rgb(51, 51, 51); border-radius: 3px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ gpg --list-keys
/c/Users/iteblog/.gnupg/pubring.gpg


pub 2048R/B15C5AA3 2016-09-19
uid iteblog (flink-elasticsearch-connector) wyphao.2007@163.com
sub 2048R/31A906E1 2016-09-19

iteblog@iteblog MINGW64 /d/work/flink-elasticsearch2-connector (master)
$ gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys B15C5AA3
gpg: sending key B15C5AA3 to hkp server keyserver.ubuntu.com
</pre>

之后我们的Java包将会发布到sonatype的构件仓库中,可以到https://oss.sonatype.org/#stagingRepositories查看发布好的Jar。进入之后会看到中间一个Table窗口,将滑动条移到最后,找到我们刚刚发布的Jar包,然后依次点击上方的Close–>Confirm,这将会弹出类似于下面的对话框,在其中输入我们Jar包的描述信息,这个信息将会在Maven搜索结果当作简介介绍我们Jar包的,所以建议输的详细点。


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop

当状态变成closed后,执行Release–>Confirm,同样会弹出一个类似于上面的对话框,我们还是输入那些介绍信息即可,当这步执行完之后,构件将会自动删除,并经过几小时后便可以在Maven中央仓库搜索到(http://search.maven.org)。如下:


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop

mvnrepository网站的显示:


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop

升级Jar包

我们开发的Jar包在后面可能需要更新,比如从1.0.1版本升级到1.0.2版本。升级Jar包比初次发布的步骤简单的多,我们只需要更新项目工程代码,并修改pom.xml文件里面的版本号,最后重新执行上面的部署和发布Jar包步骤即可。

本博客文章除特别声明,全部都是原创!
转载本文请加上:转载自过往记忆(https://www.iteblog.com/)
本文链接: 【如何发布Jar包到Maven中央仓库】(https://www.iteblog.com/archives/1807.html)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容