平時我們都是從Maven中央倉庫下載依賴揍诽,如果我們想發(fā)布我們自己寫的Maven依賴到中央倉庫供別人下載使用應該怎么辦?這里以上傳自己寫的simian-maven-plugin(https://github.com/jiangxincode/simian-maven-plugin)為例說明如何發(fā)布Maven依賴到中央倉庫忘衍。
開始之前漓库,請注意幾個地址:
1愿伴、工單管理:https://issues.sonatype.org/secure/Dashboard.jspa
說明:注冊賬號、創(chuàng)建和管理issue呵扛,依賴的發(fā)布是以解決issue的方式起步的每庆。
2、緩存?zhèn)}庫:https://oss.sonatype.org/#stagingRepositories
說明:當我們發(fā)布非SNAPSHOT版本時今穿,會先將依賴上傳到該過渡倉庫缤灵,之后才能正式發(fā)布到中央倉庫。
創(chuàng)建工單
訪問工單管理的網(wǎng)址:https://issues.sonatype.org/secure/Dashboard.jspa荣赶。
如果之前沒有注冊過sonatype賬號凤价,需要先注冊一個賬號鸽斟,注冊過程本文不在贅述拔创,務必記住用戶名和密碼。
Create Issue 填寫內(nèi)容說明:
===Step 1===
Project:Community Support - Open Source Project Repository Hosting
Issue Type:New Project
===Step 2===
Summary:依賴名稱富蓄,如:simian-maven-plugin
Group Id:對應你的依賴的groupId剩燥,如com.github.jiangxincode
Project URL:項目站點,如:https://github.com/jiangxincode/simian-maven-plugin
SCM url:項目源碼倉庫立倍,如:https://github.com/jiangxincode/simian-maven-plugin.git
其他內(nèi)容不用填寫灭红,創(chuàng)建Issue后需要等待一小段時間,Sonatype的工作人員審核處理口注,速度還是很快的变擒,一般一個工作日以內(nèi),當Issue的Status變?yōu)镽ESOLVED后寝志,就可以進行下一步操作了娇斑。
配置pom.xml
cpom.xml文件中,引入Sonatype官方的一個通用配置oss-parent材部,這樣做的好處是很多pom.xml的發(fā)布配置不需要自己配置了:
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
并增加Licenses毫缆、SCM、Developers信息:
<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>
<tag>master</tag>
<url>git@github.com:jiangxincode/simian-maven-plugin.git</url>
<connection>scm:git:git@github.com:jiangxincode/simian-maven-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jiangxincode/simian-maven-plugin.git</developerConnection>
</scm>
<developers>
<developer>
<name>Aloys</name>
<email>jiangxinnju@163.com</email>
<organization>Github</organization>
</developer>
</developers>
修改maven配置文件settings.xml乐导,在servers中增加server配置苦丁。
<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>
配置gpg-key
首先需要使用gpg工具生成一對密鑰,以Windows為例物臂,如果你安裝了Git(https://git-scm.com/)則已經(jīng)有了gpg工具旺拉,如果沒有可以單獨下載gpg4win(https://www.gpg4win.org/download.html)。以Git自帶的gpg工具為例棵磷,生成密鑰方式如下:
jiang@windows MINGW64 /d/temp/Java/simian-maven-plugin (master)
$ gpg --gen-key
gpg (GnuPG) 1.4.22; 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.
gpg: directory `/c/Users/jiang/.gnupg' created
gpg: new configuration file `/c/Users/jiang/.gnupg/gpg.conf' created
gpg: WARNING: options in `/c/Users/jiang/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/c/Users/jiang/.gnupg/secring.gpg' created
gpg: keyring `/c/Users/jiang/.gnupg/pubring.gpg' created
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: Aloys
Email address: jiangxinnju@163.com
Comment: for sonatype
You selected this USER-ID: "Aloys (for sonatype) <jiangxinnju@163.com>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? C
Comment: JiangXin
You selected this USER-ID: "Aloys (JiangXin) <jiangxinnju@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.
........+++++ ...........+++++ 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/jiang/.gnupg/trustdb.gpg: trustdb created
gpg: key 2AB935A0 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/2AB935A0 2018-10-28 Key fingerprint = A5C1 8750 A311 0057 671C D8B7 A14C A7F2 2AB9 35A0
uid Aloys (JiangXin) <jiangxinnju@163.com> sub 2048R/94B6D362 2018-10-28
部署依賴到stagingRepositories
執(zhí)行如下命令部署插件:
mvn clean deploy -P sonatype-oss-release
如果使用eclipse的mvn插件發(fā)布的話账阻,配置如下:
如果發(fā)布成功,就可以到stagingRepositories(https://oss.sonatype.org/#stagingRepositories)中查看了泽本。
發(fā)布到中央倉庫
進入stagingRepositories查看發(fā)布好的構(gòu)件淘太,點擊左側(cè)的Staging Repositories,一般最后一個就是剛剛發(fā)布的依賴,此時的構(gòu)件狀態(tài)為open蒲牧。
選中剛才發(fā)布的構(gòu)件撇贺,并點擊上方的Close–>Confirm,在下邊的Activity選項卡中查看狀態(tài)冰抢,當狀態(tài)變成closed后松嘶,執(zhí)行Release–>Confirm,并在下邊的Activity選項卡中查看狀態(tài)挎扰,成功后構(gòu)件自動刪除翠订,一小段時間(約10分鐘)后即可同步到maven的中央倉庫,再過1-2個小時就可以搜索到該依賴了(https://search.maven.org)遵倦。
其它問題
配置pom.xml時尽超,為什么沒有配置倉庫的地址?
為什么我只是在settings.xml中配置了id梧躺,又沒有配置這個id對應的服務器地址似谁,Maven是如果找到我想上傳的服務器地址?
還記的之前我們在pom.xml中配置的<parent>節(jié)點么掠哥?這個節(jié)點中有如下內(nèi)容:
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>${sonatypeOssDistMgmtSnapshotsUrl}</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
我們的項目相當于繼承了這個配置巩踏,所以Maven根據(jù)我們在settings.xml中配置的id找到pom.xml中配置的url來上傳依賴。
其實我們完全可以不配置<parent>節(jié)點续搀,而是根據(jù)實際需要自己配置對應的內(nèi)容塞琼,只不過麻煩些,對新手不太友好禁舷。
我上傳的依賴會自動簽名么彪杉?
會的,別忘了之前配置的<parent>榛了,里面有如下插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
在執(zhí)行mvn clean deploy -P sonatype-oss-release時每次需要我們輸入密鑰的密碼在讶,比較麻煩,你可以在settings.xml或者pom.xml的做如下配置:
<profile>
<id>sonatype-oss-release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>密鑰的密碼</gpg.passphrase>
</properties>
</profile>
</profiles>
此時執(zhí)行mvn clean deploy -P sonatype-oss-release就不用輸入密碼了霜大。但是此時文件中會有密碼存儲构哺,不太安全,不推薦战坤。建議隨便配個字符串曙强,然后在執(zhí)行命令時覆蓋:
mvn clean deploy -P sonatype-oss-release -Darguments="gpg.passphrase=password"
安全和方便總是不可兼得,各取所需途茫。
如何配置生成javadoc和sources碟嘴?
只要按照之前的章節(jié)配置了<parent>節(jié)點,就不需要單獨配置囊卜,因為已經(jīng)繼承了如下內(nèi)容:
如果不符合想要的效果娜扇,可以在pom.xml中覆蓋修改错沃。
Snapshot/Staging/Release倉庫的地址分別是什么?
其中sonatype-nexus-snapshots和sonatype-nexus-staging倉庫的地址可以在<parent>節(jié)點中查看:
sonatype-nexus-snapshots: https://oss.sonatype.org/content/repositories/snapshots/
sonatype-nexus-staging: https://oss.sonatype.org/service/local/staging/deploy/maven2/
sonatype-nexus-release: https://repo1.maven.org/maven2
Maven怎么知道把我的依賴上傳到哪個倉庫雀瓢?
maven會根據(jù)模塊的版本號中是否帶有-SNAPSHOT來判斷是快照版本還是正式版本枢析。如果是快照版本,那么在mvn deploy時會自動發(fā)布到快照版本庫中刃麸,而使用快照版本的模塊醒叁,在不更改版本號的情況下,直接編譯打包時泊业,maven會自動從鏡像服務器上下載最新的快照版本把沼。如果是正式發(fā)布版本,那么在mvn deploy時會自動發(fā)布到正式版本庫中吁伺,而使用正式版本的模塊饮睬,在不更改版本號的情況下,編譯打包時如果本地已經(jīng)存在該版本的模塊則不會主動去鏡像服務器上下載箱蝠。
所以续捂,我們在開發(fā)階段垦垂,可以將公用庫的版本設(shè)置為快照版本宦搬,而被依賴組件則引用快照版本進行開發(fā),在公用庫的快照版本更新后劫拗,我們也不需要修改pom文件提示版本號來下載新的版本间校,直接mvn執(zhí)行相關(guān)編譯、打包命令即可重新下載最新的快照庫了页慷,從而也方便了我們進行開發(fā)憔足。
每次發(fā)布,如果要切換發(fā)布倉庫都要修改版本號酒繁,有沒有辦法簡化滓彰?
在pom.xml中做如下配置:
<groupId>com.github.jiangxincode</groupId>
<artifactId>simian-maven-plugin</artifactId>
<version>${project.release.version}</version>
<packaging>maven-plugin</packaging>
<name>Simian Maven Plugin</name>
<url>https://github.com/jiangxincode/simian-maven-plugin</url>
<properties>
<project.release.version>0.0.5-SNAPSHOT</project.release.version>
</properties>
<profiles>
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>0.0.5</project.release.version>
</properties>
</profile>
</profiles>
首先我們看到pom文件中version的定義是采用占位符的形式,這樣的好處是可以根據(jù)不同的profile來替換版本信息州袒。
如果在發(fā)布時使用mvn deploy -P release 的命令揭绑,那么會自動使用0.0.5作為發(fā)布版本,那么根據(jù)maven處理snapshot和release的規(guī)則郎哭,由于版本號后不帶-SNAPSHOT故當成是正式發(fā)布版本他匪,會被發(fā)布到release倉庫;
如果發(fā)布時使用mvn deploy命令夸研,那么就會使用默認的版本號0.0.5-SNAPSHOT邦蜜,此時maven會認為是快照版本,會自動發(fā)布到快照版本庫亥至。
部署到snapshot倉庫時悼沈,jar包會帶上時間戳贱迟,怎么辦?
這沒關(guān)系絮供,maven會自動取相應版本最新的jar包关筒。
提交到release倉庫是,報錯怎么辦杯缺?
報錯信息如下:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project simian-maven-plugin: Failed to deploy artifacts: Could not transfer artifact...from/to release...
這是因為elease的部署策略是【disable redeploy】蒸播,不允許覆蓋更新組件。修改一下版本號萍肆,再提交即可袍榆。
如何在Github項目中添加maven版本badge?
如果你想達到下圖的效果塘揣,只需要在README.md中添加如下內(nèi)容包雀,注意根據(jù)實際情況修改:
[](https://maven-badges.herokuapp.com/maven-central/com.github.jiangxincode/simian-maven-plugin)
如何上傳非開源Jar包到Maven中央倉庫
假設(shè)我們的groupId為com.github.jiangxincode,artifact為simian,版本為2.5.10亲铡。那么首先我們會有一個simian-2.5.10.jar才写,然后新建一個README文件和License文件,內(nèi)容可以為空也可以根據(jù)實際情況填寫奖蔓,利用如下命令打包成simian-2.5.10-sources.jar:
jar -cvf simian-2.5.10-javadoc.jar license.pdf README
同理生成simian-2.5.10-sources.jar赞草,那么我們現(xiàn)在有如下三個包了:
- simian-2.5.10.jar
- simian-2.5.10-sources.jar
- simian-2.5.10-javadoc.jar
然后我們需要編輯一個pom文件,名稱為simian-2.5.10.pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.jiangxincode</groupId>
<artifactId>simian</artifactId>
<version>2.5.10</version>
<packaging>jar</packaging>
<name>Simian</name>
<description>Simian (Similarity Analyser) identifies duplication in Java, C#, C, C++, COBOL, Ruby, JSP, ASP, HTML, XML, Visual Basic, Groovy source code and even plain text files. In fact, simian can be used on any human readable files such as ini files, deployment descriptors, you name it.</description>
<url>http://www.harukizaemon.com/simian/index.html</url>
<licenses>
<license>
<name>Simian Software License Agreement</name>
<url>http://www.harukizaemon.com/simian/license.pdf</url>
</license>
</licenses>
<scm>
<url>git@harukizaemon.com:simian/simian-git.git</url>
<connection>scm:git:git@harukizaemon.com:simian/simian-git.git</connection>
<developerConnection>scm:git:git@harukizaemon.com:simian/simian-git.git</developerConnection>
</scm>
<developers>
<developer>
<name>haruki_zaemon</name>
<email>haruki_zaemon@mac.com</email>
</developer>
</developers>
</project>
以上pom文件根據(jù)實際內(nèi)容填寫吆鹤,注意groupId,artifactId,version不能有錯誤厨疙,其次是packaging要填jar。scm節(jié)點中填寫一個git地址疑务,和代碼沒關(guān)系也行沾凄。
完成這一步后我們有四個文件分別為:
- simian-2.5.10.jar
- simian-2.5.10-sources.jar
- simian-2.5.10-javadoc.jar
- simian-2.5.10.pom
然后依次運行g(shù)pg命令簽名這四個文件,以pom文件為例:
gpg -ab simian-2.5.10.pom
然后會得到對應的asc文件知允,最后文件列表為
- simian-2.5.10.jar
- simian-2.5.10.jar.asc
- simian-2.5.10-sources.jar
- simian-2.5.10-sources.jar.asc
- simian-2.5.10-javadoc.jar
- simian-2.5.10-javadoc.jar.asc
- simian-2.5.10.pom
- simian-2.5.10.pom.asc
接下來需要把八個文件打成一個bundle.jar撒蟀,命令示例:
jar -cvf bundle.jar simian-2.5.10*
打開并登陸https://oss.sonatype.org/#welcome,點擊Staging Upload選項
點擊Staging Upload后温鸽,在Upload Mode后有個下拉選框保屯,選擇Artifact bundle,然后select bundle,找到之前生成的bundle.jar,點擊上傳按鈕嗤朴。
上傳后會在上圖的Staging Repositories中有顯示配椭。之后的步驟就不在贅述了,和上傳開源依賴是完全一樣的雹姊。
參考內(nèi)容
Github開源Java項目(IJPay)上傳到Maven Central 詳細介紹:https://blog.csdn.net/zyw_java/article/details/77802615