將jar包發布到Maven中央倉庫(Maven Central Repository),這樣所有的Java開發者都可以使用Maven直接導入依賴,例如fundebug-java:
<!-- https://mvnrepository.com/artifact/com.fundebug/fundebug-java -->
<dependency><groupId>com.fundebug</groupId><artifactId>fundebug-java</artifactId><version>0.2.0</version>
</dependency>
復制代碼
但是,Maven中央倉庫并不支持直接發布jar包。我們需要將jar包發布到一些指定的第三方Maven倉庫,然后該倉庫再將jar包同步到Maven中央倉庫。
其中,最"簡單"的方式是通過Sonatype OSSRH倉庫來發布jar包。接下來,我會介紹如何將jar包發布到Sonatype OSSRH。
本教程所使用的系統配置如下:
- OS:macOS 10.14.2
- JDK:1.8.0_192
- Maven:3.5.4
1. 注冊JIRA賬號
JIRA是一個項目管理服務,類似于國內的Teambition。Sonatype通過JIRA來管理OSSRH倉庫。
注冊地址:issues.sonatype.org/secure/Sign…
需要填寫Email, Full Name, Username以及password,其中Username與Password后面的步驟需要用到,請記下來。
2. 創建issue
通過在JIRA上創建issue來申請發布新的jar包,Sonatype的工作人員會進行審核,審核不算嚴格,一般按照要求填寫不會有問題。
創建鏈接:issues.sonatype.org/secure/Crea…
創建issue的時候需要填寫下面這些信息:
- Summary
- Description
- Group Id
- Project URL
- SCM url
大家可以參考我申請發布fundebug-java與fundebug-spring時所填寫的內容:OSSRH-45238
由于時差,前一天創建issue,第二天早上才會有回應。當issue的status變為RESOLVED,我們就可以進行下一步操作了。
3. 安裝并配置GPG
發布到Maven倉庫中的所有文件都要使用GPG簽名,以保障完整性。因此,我們需要在本地安裝并配置GPG。
安裝GPG
MacBook安裝GPG非常簡單,下載并安裝GPG Suite即可。
生成GPG密鑰對
gpg --gen-key
復制代碼
生成密鑰時將需要輸入name、email以及password。password在之后的步驟需要用到,請記下來。
上傳GPG公鑰
將公鑰上傳到公共的密鑰服務器,這樣其他人才可以通過公鑰來驗證jar包的完整性。
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys CAB4165C69B699D989D2A62BD74A11D3F9F41243
復制代碼
其中CAB4165C69B699D989D2A62BD74A11D3F9F41243為密鑰的ID,可以通過gpg --list-keys命令查看
gpg --list-keys/Users/kiwenlau/.gnupg/pubring.kbx
----------------------------------
pub dsa2048 2010-08-19 [SC] [expires: 2020-06-15]85E38F69046B44C1EC9FB07B76D78F0500D026C4
uid [ unknown] GPGTools Team <team@gpgtools.org>
sub elg2048 2010-08-19 [E] [expires: 2020-06-15]
sub rsa4096 2014-04-08 [S] [expires: 2024-01-02]pub rsa2048 2019-01-03 [SC] [expires: 2021-01-02]CAB4165C69B699D989D2A62BD74A11D3F9F41243
uid [ultimate] kiwenlau <kiwenlau@gmail.com>
sub rsa2048 2019-01-03 [E] [expires: 2021-01-02]
復制代碼
4. 配置Maven的setting.xml
setting.xml為Maven的全局配置文件,在MacBook上的位置為**/usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml**,我們需要將第1步配置的Username和Password添加到<servers></servers>
標簽中,這樣我們才能將jar包部署到Sonatype OSSRH倉庫:
<servers><server><id>ossrh</id><username>Fundebug</username><password>passsword</password></server>
</servers>
復制代碼
5. 配置項目的pom.xml
pom.xml挺長的。根據Sonatype OSSRH的要求,以下信息都必須配置:
- Supply Javadoc and Sources
- Sign Files with GPG/PGP
- Sufficient Metadata
- Correct Coordinates
- Project Name, Description and URL
- License Information
- Developer Information
- SCM Information
配置時參考我的pom.xml,根據需要修改即可。
<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.fundebug</groupId><artifactId>fundebug-java-notifier</artifactId><version>0.2.0</version><packaging>pom</packaging><name>fundebug-java-notifier</name><url>https://github.com/Fundebug/fundebug-java-notifier</url><description>Capture Java and Spring exceptions automatically</description><licenses><license><name>Server Side Public License</name><url>https://www.mongodb.com/licensing/server-side-public-license</url><distribution>repo</distribution><comments>A not business-friendly OSS license</comments></license></licenses><scm><url>https://github.com/Fundebug/fundebug-java-notifier</url><connection>https://github.com/Fundebug/fundebug-java-notifier.git</connection></scm><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.deploy.skip>true</maven.deploy.skip></properties><developers><developer><name>kiwenlau</name><id>kiwenlau</id><email>kiwenlau@gmail.com</email><roles><role>Developer</role></roles><timezone>+8</timezone></developer></developers><profiles><profile><id>default</id><activation><activeByDefault>true</activeByDefault></activation><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.2.1</version><executions><execution><phase>package</phase><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>2.9.1</version><executions><execution><phase>package</phase><goals><goal>jar</goal></goals></execution></executions></plugin><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><snapshotRepository><id>ossrh</id><url>https://oss.sonatype.org/content/repositories/snapshots/</url></snapshotRepository><repository><id>ossrh</id><url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url></repository></distributionManagement></profile></profiles><modules><module>fundebug-java</module><module>fundebug-spring</module><module>examples/hello-world</module><module>examples/spring-rest-api</module></modules>
</project>
復制代碼
6. 發布jar包
執行mvn clean deploy處理,即可將jar包發布到Sonatype OSSRH倉庫。
mvn clean deploy -projects fundebug-java,fundebug-spring
復制代碼
我們的項目fundebug-java-notifier含有多個模塊,僅需部署fundebug-java與fundebug-spring,因此使用**-projects**選項來指定。
第一次執行mvn clean deploy命令時,需要輸入GPG密鑰的密碼。
mvn clean deploy命令執行成功的輸出是這樣的(部分日志):
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] fundebug-java 0.2.0 ................................ SUCCESS [ 22.183 s]
[INFO] fundebug-spring 0.2.0 .............................. SUCCESS [ 16.383 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.728 s
[INFO] Finished at: 2019-01-12T20:10:16+08:00
[INFO] ------------------------------------------------------------------------
復制代碼
7. close并release
mvn clean deploy命令執行成功,使用JIRA賬號登陸:oss.sonatype.org/#stagingRep…,就可以看到你所發布的jar包了:
選中對于的repository之后,點擊箭頭所指的close,close時會檢查發布的構件是否符合要求。若符合要求,則close成功,成功之后點擊箭頭所指的release,即可正式將jar包發布到Sonatype OSSRH倉庫。
release成功大概2個小時之后,該構件就會同步到Maven中央倉庫:
參考
- Guide to uploading artifacts to the Central Repository
- OSSRH Guide
- Maven入門教程
關于Fundebug
Fundebug專注于JavaScript、微信小程序、微信小游戲、支付寶小程序、React Native、Node.js和Java線上應用實時BUG監控。 自從2016年雙十一正式上線,Fundebug累計處理了9億+錯誤事件,付費客戶有Google、360、金山軟件、百姓網等眾多品牌企業。歡迎大家免費試用!
版權聲明
轉載時請注明作者Fundebug以及本文地址: blog.fundebug.com/2019/01/14/…