如何發(fā)布Jar包到Maven中央倉庫

本文系轉(zhuǎn)載,原文地址:

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


本博客曾經(jīng)介紹了《如何手動(dòng)添加依賴的jar文件到本地Maven倉庫》這里的方法非常的簡單,而且局限性很大:只能提供給本人開發(fā)使用,無法共享給其他需要的人。本文將介紹如何把自己開發(fā)出來的Java包發(fā)布到Maven中央倉庫(http://search.maven.org/),這樣任何人都可以搜索到這個(gè)包并使用它。如果你現(xiàn)在還不了解Maven是啥東西,請(qǐng)你先找本Maven書看看。

將自己的Java包發(fā)布到Maven中央倉庫需要經(jīng)過許多繁瑣的步驟,本文將詳細(xì)地介紹這些步驟。相信大家在看完本文之后也會(huì)發(fā)布jar包到Maven中央倉庫。

文章目錄

創(chuàng)建工單

在發(fā)布Java包到Maven中央倉庫首先需要在https://issues.sonatype.org/secure/Dashboard.jspa網(wǎng)站創(chuàng)建一個(gè)工單(Issues),第一次使用這個(gè)網(wǎng)站的時(shí)候需要注冊(cè)自己的帳號(hào)(這個(gè)帳號(hào)和密碼需要記住,后面會(huì)用到),之后創(chuàng)建自己的Issue,點(diǎn)擊導(dǎo)航最上面的Create按鈕,然后會(huì)彈出下面的對(duì)話框,將ProjectIssue Type設(shè)置為下圖的內(nèi)容:


如果想及時(shí)了解Spark、Hadoop或者Hbase相關(guān)的文章,歡迎關(guān)注微信公共帳號(hào):iteblog_hadoop

然后根據(jù)你Java包的功能分別寫上Summary、Description、Group Id、SCM url以及Project URL等必要信息,可以參見我之前創(chuàng)建的Issue:https://issues.sonatype.org/browse/OSSRH-24874。創(chuàng)建完之后需要等待Sonatype的工作人員審核處理,審核時(shí)間還是很快的,我的審核差不多等待了兩小時(shí)。當(dāng)Issue的Status變?yōu)?code>RESOLVED后,就可以進(jìn)行下一步操作了。

如果你的Group Id填寫的是自己的網(wǎng)站(我的就是這種情況),Sonatype的工作人員會(huì)詢問你那個(gè)Group Id是不是你的域名,你只需要在上面回答是就行,然后就會(huì)通過審核。

使用gpg生成密鑰對(duì)

上面創(chuàng)建的issuce經(jīng)過審核之后,我們可以使用gpg生成密鑰對(duì),這里分兩種情況:

1、如果使用的是Windows,可以到https://www.gpg4win.org/download.html下載gpg4win,推薦使用 Gpg4win-Vanilla 2.3.3版本,因?yàn)樗鼉H包括 GnuPG,這個(gè)工具才是我們所需要的;

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

之后可以通過gpg --version命令查看是否安裝成功,如果出現(xiàn)版本等信息說明安裝成功了,如下:

<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之后,下面一次按照如下的命令設(shè)置好你的名字,郵箱,其他步驟可以使用默認(rèn)值。不過輸入Passphrase的值需要記住,這個(gè)相當(dāng)于密鑰的密碼,發(fā)布過程中進(jìn)行簽名操作的時(shí)候會(huì)用到:

<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>

到這里我們就設(shè)置好密鑰對(duì)了。上面代碼中導(dǎo)數(shù)第四行的B15C5AA3需要記住,其相當(dāng)于我們生成的key,后面會(huì)用到。

設(shè)置Maven配置

為了發(fā)布更簡便,我們可以工程的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配置文件,設(shè)置如下:

<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網(wǎng)站的賬號(hào)</username>
<password>Sonatype網(wǎng)站的密碼</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>Sonatype網(wǎng)站的賬號(hào)</username>
<password>Sonatype網(wǎng)站的密碼</password>
</server>
</servers>
</pre>

上面的username和password就是你在步驟一注冊(cè)的帳號(hào)和密碼。

部署和發(fā)布Jar包

配置好pom.xml文件和Maven的setting.xml文件之后,我們就可以部署Jar包了,運(yùn)行如下命令:

<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=設(shè)置gpg設(shè)置密鑰時(shí)候輸入的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>

運(yùn)行上面的命令之后,我們?cè)偕蟼鞯降谌降膋ey驗(yàn)證庫,如下:

<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包將會(huì)發(fā)布到sonatype的構(gòu)件倉庫中,可以到https://oss.sonatype.org/#stagingRepositories查看發(fā)布好的Jar。進(jìn)入之后會(huì)看到中間一個(gè)Table窗口,將滑動(dòng)條移到最后,找到我們剛剛發(fā)布的Jar包,然后依次點(diǎn)擊上方的Close–>Confirm,這將會(huì)彈出類似于下面的對(duì)話框,在其中輸入我們Jar包的描述信息,這個(gè)信息將會(huì)在Maven搜索結(jié)果當(dāng)作簡介介紹我們Jar包的,所以建議輸?shù)脑敿?xì)點(diǎn)。


如果想及時(shí)了解Spark、Hadoop或者Hbase相關(guān)的文章,歡迎關(guān)注微信公共帳號(hào):iteblog_hadoop

當(dāng)狀態(tài)變成closed后,執(zhí)行Release–>Confirm,同樣會(huì)彈出一個(gè)類似于上面的對(duì)話框,我們還是輸入那些介紹信息即可,當(dāng)這步執(zhí)行完之后,構(gòu)件將會(huì)自動(dòng)刪除,并經(jīng)過幾小時(shí)后便可以在Maven中央倉庫搜索到(http://search.maven.org)。如下:


如果想及時(shí)了解Spark、Hadoop或者Hbase相關(guān)的文章,歡迎關(guān)注微信公共帳號(hào):iteblog_hadoop

mvnrepository網(wǎng)站的顯示:


如果想及時(shí)了解Spark、Hadoop或者Hbase相關(guān)的文章,歡迎關(guān)注微信公共帳號(hào):iteblog_hadoop

升級(jí)Jar包

我們開發(fā)的Jar包在后面可能需要更新,比如從1.0.1版本升級(jí)到1.0.2版本。升級(jí)Jar包比初次發(fā)布的步驟簡單的多,我們只需要更新項(xiàng)目工程代碼,并修改pom.xml文件里面的版本號(hào),最后重新執(zhí)行上面的部署和發(fā)布Jar包步驟即可。

本博客文章除特別聲明,全部都是原創(chuàng)!
轉(zhuǎn)載本文請(qǐng)加上:轉(zhuǎn)載自過往記憶(https://www.iteblog.com/)
本文鏈接: 【如何發(fā)布Jar包到Maven中央倉庫】(https://www.iteblog.com/archives/1807.html)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容