摘要
Maven 中央倉庫并不支持直接上傳 jar 包震蒋,因此需要將 jar 包發(fā)布到一些指定的第三方 Maven 倉庫查剖,比如:Sonatype OSSRH 倉庫笋庄,然后該倉庫再將 jar 包同步到 Maven ,本文詳細(xì)記錄整個(gè)發(fā)布静暂、同步過程摹迷。
注冊一個(gè) Sonatype 用戶
進(jìn)入地址:https://issues.sonatype.org/secure/Signup!default.jspa 注冊 Sonatype 用戶,Sonatype 通過 JIRA(JIRA 是 Atlassian 公司出品的項(xiàng)目與事務(wù)跟蹤工具)來管理 OSSRH 倉庫驮审。<br />
創(chuàng)建一個(gè)發(fā)布構(gòu)件的 issue
提交「構(gòu)件發(fā)布申請」的第一步是在 JIRA Dashborad 上創(chuàng)建一個(gè) issue塔拳。如下所示,點(diǎn)擊 Create
按鈕:
會(huì)彈出一個(gè)對話框讓你填寫 issue 的詳細(xì)信息峡竣,這里最重要的就是 Group Id靠抑,一般會(huì)帶上域名,千萬別弄錯(cuò)了适掰,這關(guān)系到以后發(fā)布其它的構(gòu)件颂碧。我們這里是com.vesoft
。
Sonatype 有域名驗(yàn)證类浪,驗(yàn)證方式:
- 往你的 DNS 中添加 JIRA 編號(hào)記錄
- 重定向到你的 Github 主頁
如果你沒有域名载城,可參考這個(gè)鏈接:http://central.sonatype.org/pages/choosing-your-coordinates.html 的方法進(jìn)行操作
- 選擇一個(gè)帶有項(xiàng)目托管信息的 GroupId,例如 io.github.facebook 或 com.github.facebook
- 另外一種推薦的方式是使用免費(fèi)的 free managed security reporting service
- 將 https://hackerone.com/central-security-project/reports/new 作為你項(xiàng)目的安全信息保障 url力细,剩下的事情 Sonatype 工作人員會(huì)自行搞定昔脯。
等待 issue 審核通過
審核因?yàn)闀r(shí)差原因需要一定時(shí)間静陈,審核通過后會(huì)收到郵件通知,同時(shí)在對應(yīng) issue 下會(huì)看到 Sonatype 工作人員的回復(fù),一般是添加一個(gè) comment劣领,內(nèi)容大致如下:
Configuration has been prepared, now you can:
Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/ deploy/maven2
Promote staged artifacts into repository 'Releases'
Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public
Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging
please comment on this ticket when you promoted your first release, thanks
使用 GPG 生成密鑰對
生成密鑰對
> gpg --gen-key
會(huì)讓選擇加密方式:
- RSA and RSA (default)
- DSA and Elgamal
- DSA (sign only)
- RSA (sign only)
默認(rèn)選第一個(gè)饼丘,選擇之后忽肛,需輸入用戶名和郵箱,和 Passphase——相當(dāng)于密鑰庫密碼。
查看公鑰
> gpg --list-keys
xxx/.gnupg/pubring.gpg
---------------------------------
pub 2048R/xxxx 2019-12-02
uid $YOUR_UID <$YOUR_EMAIL>
sub 2048R/**** 2019-12-02
這里的公鑰 ID 是 xxxx,馬上就會(huì)用到了。
將公鑰上傳到 PGP 密鑰服務(wù)器
gpg --keyserver hkp://keys.gnupg.net:11371 --send-keys xxxx
查看公鑰是否上傳成功
> gpg --keyserver hkp://keys.gnupg.net:11371 --recv-keys xxxx
gpg: 下載密鑰‘xxxx’行楞,從 hkp 服務(wù)器 keys.gnupg.net
gpg: 密鑰 xxxx:“$YOUR_UID <$YOUR_EMAIL>”未改變
gpg: 合計(jì)被處理的數(shù)量:1
gpg: 未改變:1
NOTE:
- 根據(jù)實(shí)際填寫此處的公鑰 ID
- 很多網(wǎng)上教程給的是 pool.sks-keyservers.net钓丰,個(gè)人感覺 pool.sks-keyservers.net 這個(gè) keyserver 不好用,上傳了 key,經(jīng)常會(huì)驗(yàn)證失敗予跌,也獲取失敗航邢,maven 支持兩個(gè) key servers http://keys.gnupg.net:11371 和 http://pool.sks-keyservers.net:11371
- 此處采用 hkp 協(xié)議而不是 http 協(xié)議
- 很多教程沒有給端口,經(jīng)試驗(yàn)冀惭,需加上端口號(hào)
本地的私鑰用來對上傳的構(gòu)件進(jìn)行數(shù)字簽名划址,而下載該構(gòu)件的用戶可通過上傳的公鑰來驗(yàn)證簽名--需驗(yàn)證這個(gè)構(gòu)件是否由本人上傳的抚恒,因?yàn)榇嬖跇?gòu)件被篡改的可能混萝。
修改 Maven 配置文件
修改 Maven 配置文件主要是需要修改 setting.xml
和項(xiàng)目的 pom.xml
文件
配置 Maven 的 setting.xml
修改 ~/.m2/setting.xml
文件
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
...
<server>
<id>snapshots</id>
<username>$USER_NAME</username>
<password>$YOUR_PASSWORD</password>
</server>
<server>
<id>release</id>
<username>$USER_NAME</username>
<password>$YOUR_PASSWORD</password>
</server>
</servers>
</settings>
替換 USER_NAME
司光, YOUR_PASSWORD
為 Sonatype 上面注冊的用戶名和密碼坞淮, 這里的 ID 會(huì)在 pom.xml
里面使用到。
配置 Maven 的 pom.xml
<project>
...
<!-- More Project Information -->
<name>nebula-java</name>
<description>Nebula Java Client</description>
<url>https://github.com/vesoft-inc/nebula-java</url>
<scm>
<connection>scm:git:https://github.com/vesoft-inc/nebula</connection>
<url>https://github.com/vesoft-inc/nebula</url>
<developerConnection>scm:git:https://github.com/vesoft-inc/nebula</developerConnection>
</scm>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>license</comments>
</license>
</licenses>
...
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<excludePackageNames>com.facebook.thrift:com.facebook.thrift.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<doclint>none</doclint>
</configuration>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>
...
</project>
- pom.xml 中必須包括:
name
秤涩、description
帜乞、url
、licenses
筐眷、developers
黎烈、scm
等基本信息 (血淚史之踩過的坑) -
發(fā)布 maven 除了 jar 必須還有文檔包和源碼包。因此 pom 需添加
maven-javadoc-plugin
和maven-source-plugin
匀谣。 參考示例:
com-vesoft-client
|-- pom.xml
|-- src\
`-- target
`-- attach-source-javadoc-1.0-SNAPSHOT.jar
`-- attach-source-javadoc-1.0-SNAPSHOT-javadoc.jar
`-- attach-source-javadoc-1.0-SNAPSHOT-sources.jar
- 發(fā)布構(gòu)建需要秘鑰加密照棋,因此 pom 需添加
maven-gpg-plugin
(血淚史之踩過的坑)
多模塊項(xiàng)目配置
nebula-java 是多模塊項(xiàng)目
<modules>
<module>client</module>
<module>examples</module>
</modules>
為了上傳 Client,需要上傳 parent 的 pom.xml武翎,否則 Client 會(huì)找不到依賴(血淚史之踩過的坑)烈炭,但我們又不希望上傳 examples 模塊,故做了如下改動(dòng):
- 項(xiàng)目信息
name
后频、description
梳庆、url
暖途、licenses
卑惜、developers
、scm
等信息和maven-gpg-plugin
放在 parent 的 pom.xml 文件中
<project>
...
<name>nebula-java</name>
<description>Nebula Java Client</description>
<url>https://github.com/vesoft-inc/nebula-java</url>
<scm>
<connection>scm:git:https://github.com/vesoft-inc/nebula</connection>
<url>https://github.com/vesoft-inc/nebula</url>
<developerConnection>scm:git:https://github.com/vesoft-inc/nebula</developerConnection>
</scm>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>license</comments>
</license>
</licenses>
<developers>
<developer>
<id>$ID</id>
<name>$NAME</name>
<email>$EMAIL</email>
<organization>vesoft</organization>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
</developer>
</developers>
<distributionManagement>
<repository>
<id>release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 在 Java Client 的 pom.xml 中添加
maven-javadoc-plugin
驻售、maven-source-plugin
和maven-deploy-plugin
<plugins>
......
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<excludePackageNames>com.facebook.thrift:com.facebook.thrift.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<doclint>none</doclint>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
在 example 模塊的 pom.xml 中聲明 skip deploy
<plugins>
......
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
Q: 為什么 maven-gpg-plugin
放在 parent 的 pom.xm l中露久,而 maven-javadoc-plugin
,maven-source-plugin
插件放在 Client 的 pom.xml 中
A: 因?yàn)樯蟼鞯乃袠?gòu)件都需要加密欺栗,包括 parent 的 pom.xml毫痕,因此放在 parent 中; 而只有 Client 需要上傳 javadoc迟几,source消请,因此 maven-javadoc-plugin
,maven-source-plugin
插件放在 Client 中类腮。
上傳構(gòu)件到 OSS 中
在 nebula-java/ 目錄下運(yùn)行:
> mvn clean deploy -DpomFile=pom.xml
NOTE:不加 -DpomFile 臊泰,上傳的文件中會(huì)沒有 parent 的 pom.xml (
又是一部血淚史)
在 OSS 中發(fā)布構(gòu)件
使用 Sonatype 賬號(hào)登錄 https://oss.sonatype.org/#stagingRepositories,可在 Staging Repositories
中查看已上傳的構(gòu)件蚜枢,這些構(gòu)件目前是放在 Staging 倉庫中缸逃,可進(jìn)行模糊查詢,定位到剛上傳的構(gòu)件厂抽。
此時(shí)需频,該構(gòu)件的狀態(tài)為 Open
,勾選它筷凤,然后點(diǎn)擊 Close
按鈕昭殉。系統(tǒng)會(huì)自動(dòng)驗(yàn)證該構(gòu)件是否滿足指定要求 (幸福的人只有一種,不幸的人各有各的不幸,可能會(huì)遇到各種各樣的不符合要求挪丢,Good luck莽鸭!ヾ(?°?°?)??)
當(dāng)驗(yàn)證完畢后,狀態(tài)會(huì)變?yōu)?Closed
吃靠。
最后硫眨,點(diǎn)擊 Release
按鈕來發(fā)布該構(gòu)件
頁面可能要刷新一下才能看到最新的狀態(tài)。
通知 Sonatype 構(gòu)件已成功發(fā)布
在前面 JIRA 的 issue 下面回復(fù)一條“構(gòu)件已成功發(fā)布”的評論巢块,通知 Sonatype 的工作人員為要發(fā)布的構(gòu)件做審批礁阁,發(fā)布后會(huì)關(guān)閉該 issue。
等待構(gòu)件審批通過
然后族奢,等待姥闭。。越走。
從中央倉庫中搜索構(gòu)件
大概十多分鐘后棚品,可以在這里 https://repo1.maven.org/maven2 找到剛剛發(fā)布的構(gòu)件,可以直接在 pom.xml 中使用啦~~ ????
等同步完成大約 2 個(gè)小時(shí)廊敌,中央倉庫(連接:http://search.maven.org/)就可以搜到啦铜跑。
國內(nèi)很多使用的是阿里云的鏡像,鏡像同步不是實(shí)時(shí)同步骡澈。為了及時(shí)使用锅纺,可以添加中央倉庫鏡像源,在 ~/.m2/setting.xml
文件添加肋殴,如下:
<mirrors>
......
<mirror>
<id>nexus-mvn</id>
<mirrorOf>central</mirrorOf>
<name>Nexus Central</name>
<url>http://repo1.maven.org/maven2</url>
</mirror>
</mirrors>
第一次成功發(fā)布之后囤锉,以后就不用這么麻煩了,可以直接使用 Group Id 發(fā)布構(gòu)件护锤。
后續(xù)操作
之后同一個(gè) Group Id 的發(fā)布流程
- 按照上文編輯 setting.xml 和 pom.xml官地,
- 上傳構(gòu)建;
- 在 https://oss.sonatype.org/ close 并 release 構(gòu)件烙懦;
- 等待同步完成(大約 2 小時(shí))后驱入,就可以使用了
最后溫馨提示:發(fā)布的版本不支持修改,或者刪除
附錄
最后修陡,附上 Nebula Graph GitHub 地址:https://github.com/vesoft-inc/nebula沧侥,如果你在使用 Nebula Graph 過程中遇到任何問題,歡迎 GitHub 聯(lián)系我們或者加入微信交流群魄鸦,請聯(lián)系微信號(hào):NebulaGraphbot