1. 程式人生 > >如何將JAR包釋出到Maven中央倉庫?

如何將JAR包釋出到Maven中央倉庫?

將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倉庫。

註冊地址:https://issues.sonatype.org/secure/Signup!default.jspa

需要填寫Email, Full Name, Username以及password,其中Username與Password後面的步驟需要用到,請記下來。

2. 建立issue

通過在JIRA上建立issue來申請釋出新的jar包,Sonatype的工作人員會進行稽核,稽核不算嚴格,一般按照要求填寫不會有問題。

建立連結:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134

建立issue的時候需要填寫下面這些資訊:

  • Summary
  • Description
  • Group Id
  • Project URL
  • SCM url

大家可以參考我申請釋出fundebug-javafundebug-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 <[email protected]>
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 <[email protected]>
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>[email protected]</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賬號登陸:https://oss.sonatype.org/#stagingRepositories,就可以看到你所釋出的jar包了:

選中對於的repository之後,點選箭頭所指的close,close時會檢查釋出的構件是否符合要求。若符合要求,則close成功,成功之後點選箭頭所指的release,即可正式將jar包釋出到Sonatype OSSRH倉庫。

release成功大概2個小時之後,該構件就會同步到Maven中央倉庫

參考

關於Fundebug

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/