將jar包發佈到Maven中央倉庫(Maven Central Repository),這樣全部的Java開發者均可以使用Maven直接導入依賴,例如fundebug-java:html
<!-- 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中央倉庫。java
其中,最"簡單"的方式是經過Sonatype OSSRH倉庫來發布jar包。接下來,我會介紹如何將jar包發佈到Sonatype OSSRH。git
本教程所使用的系統配置以下:github
JIRA是一個項目管理服務,相似於國內的Teambition。Sonatype經過JIRA來管理OSSRH倉庫。spring
註冊地址:https://issues.sonatype.org/secure/Signup!default.jspamongodb
須要填寫Email, Full Name, Username以及password,其中Username與Password後面的步驟須要用到,請記下來。apache
經過在JIRA上建立issue來申請發佈新的jar包,Sonatype的工做人員會進行審覈,審覈不算嚴格,通常按照要求填寫不會有問題。ubuntu
建立連接:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134小程序
建立issue的時候須要填寫下面這些信息:微信小程序
你們能夠參考我申請發佈fundebug-java與fundebug-spring時所填寫的內容:OSSRH-45238
因爲時差,前一天建立issue,次日早上纔會有迴應。當issue的status變爲RESOLVED,咱們就能夠進行下一步操做了。
發佈到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]
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>
pom.xml挺長的。根據Sonatype OSSRH的要求,如下信息都必須配置:
配置時參考個人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>
執行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] ------------------------------------------------------------------------
mvn clean deploy命令執行成功,使用JIRA帳號登錄:https://oss.sonatype.org/#stagingRepositories,就能夠看到你所發佈的jar包了:
選中對於的repository以後,點擊箭頭所指的close,close時會檢查發佈的構件是否符合要求。若符合要求,則close成功,成功以後點擊箭頭所指的release,便可正式將jar包發佈到Sonatype OSSRH倉庫。
release成功大概2個小時以後,該構件就會同步到Maven中央倉庫:
Fundebug專一於JavaScript、微信小程序、微信小遊戲、支付寶小程序、React Native、Node.js和Java線上應用實時BUG監控。 自從2016年雙十一正式上線,Fundebug累計處理了9億+錯誤事件,付費客戶有Google、360、金山軟件、百姓網等衆多品牌企業。歡迎你們免費試用!
轉載時請註明做者Fundebug以及本文地址: https://blog.fundebug.com/2019/01/14/how-to-deploy-jar-to-maven-central-repository/