溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解如何將JAR包發(fā)布到Maven中央倉(cāng)庫(kù)

發(fā)布時(shí)間:2020-09-23 00:12:42 來(lái)源:腳本之家 閱讀:142 作者:Fundebug 欄目:編程語(yǔ)言

將jar包發(fā)布到Maven中央倉(cāng)庫(kù)(Maven Central Repository),這樣所有的Java開(kāi)發(fā)者都可以使用Maven直接導(dǎo)入依賴,例如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中央倉(cāng)庫(kù)并不支持直接發(fā)布jar包。我們需要將jar包發(fā)布到一些指定的第三方Maven倉(cāng)庫(kù),然后該倉(cāng)庫(kù)再將jar包同步到Maven中央倉(cāng)庫(kù)。

其中,最"簡(jiǎn)單"的方式是通過(guò)Sonatype OSSRH倉(cāng)庫(kù)來(lái)發(fā)布jar包。接下來(lái),我會(huì)介紹如何將jar包發(fā)布到Sonatype OSSRH。

本教程所使用的系統(tǒng)配置如下:

  • OS:macOS 10.14.2
  • JDK:1.8.0_192
  • Maven:3.5.4

1. 注冊(cè)JIRA賬號(hào)

JIRA是一個(gè)項(xiàng)目管理服務(wù),類似于國(guó)內(nèi)的Teambition。Sonatype通過(guò)JIRA來(lái)管理OSSRH倉(cāng)庫(kù)。

注冊(cè)地址:https://issues.sonatype.org/secure/Signup!default.jspa

需要填寫Email, Full Name, Username以及password,其中Username與Password后面的步驟需要用到,請(qǐng)記下來(lái)。

2. 創(chuàng)建issue

通過(guò)在JIRA上創(chuàng)建issue來(lái)申請(qǐng)發(fā)布新的jar包,Sonatype的工作人員會(huì)進(jìn)行審核,審核不算嚴(yán)格,一般按照要求填寫不會(huì)有問(wèn)題。

創(chuàng)建鏈接:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134

詳解如何將JAR包發(fā)布到Maven中央倉(cāng)庫(kù)

創(chuàng)建issue的時(shí)候需要填寫下面這些信息:

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

大家可以參考我申請(qǐng)發(fā)布fundebug-java與fundebug-spring時(shí)所填寫的內(nèi)容:OSSRH-45238

由于時(shí)差,前一天創(chuàng)建issue,第二天早上才會(huì)有回應(yīng)。當(dāng)issue的status變?yōu)镽ESOLVED,我們就可以進(jìn)行下一步操作了。

3. 安裝并配置GPG

發(fā)布到Maven倉(cāng)庫(kù)中的所有文件都要使用GPG簽名,以保障完整性。因此,我們需要在本地安裝并配置GPG。

安裝GPG

MacBook安裝GPG非常簡(jiǎn)單,下載并安裝GPG Suite即可。

生成GPG密鑰對(duì)

gpg --gen-key

生成密鑰時(shí)將需要輸入name、email以及password。password在之后的步驟需要用到,請(qǐng)記下來(lái)。

上傳GPG公鑰

將公鑰上傳到公共的密鑰服務(wù)器,這樣其他人才可以通過(guò)公鑰來(lái)驗(yàn)證jar包的完整性。

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys CAB4165C69B699D989D2A62BD74A11D3F9F41243

其中CAB4165C69B699D989D2A62BD74A11D3F9F41243為密鑰的ID,可以通過(guò)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>標(biāo)簽中,這樣我們才能將jar包部署到Sonatype OSSRH倉(cāng)庫(kù):

<servers>
    <server>
        <id>ossrh</id>
        <username>Fundebug</username>
        <password>passsword</password>
    </server>
</servers>

5. 配置項(xiàng)目的pom.xml

pom.xml挺長(zhǎng)的。根據(jù)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

配置時(shí)參考我的pom.xml,根據(jù)需要修改即可。

<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. 發(fā)布jar包

執(zhí)行mvn clean deploy處理,即可將jar包發(fā)布到Sonatype OSSRH倉(cāng)庫(kù)。

mvn clean deploy -projects fundebug-java,fundebug-spring

我們的項(xiàng)目fundebug-java-notifier含有多個(gè)模塊,僅需部署fundebug-java與fundebug-spring,因此使用-projects選項(xiàng)來(lái)指定。

第一次執(zhí)行mvn clean deploy命令時(shí),需要輸入GPG密鑰的密碼。

mvn clean deploy命令執(zhí)行成功的輸出是這樣的(部分日志):

[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命令執(zhí)行成功,使用JIRA賬號(hào)登陸:https://oss.sonatype.org/#stagingRepositories,就可以看到你所發(fā)布的jar包了:

詳解如何將JAR包發(fā)布到Maven中央倉(cāng)庫(kù)

選中對(duì)于的repository之后,點(diǎn)擊箭頭所指的close,close時(shí)會(huì)檢查發(fā)布的構(gòu)件是否符合要求。若符合要求,則close成功,成功之后點(diǎn)擊箭頭所指的release,即可正式將jar包發(fā)布到Sonatype OSSRH倉(cāng)庫(kù)。

release成功大概2個(gè)小時(shí)之后,該構(gòu)件就會(huì)同步到Maven中央倉(cāng)庫(kù):

詳解如何將JAR包發(fā)布到Maven中央倉(cāng)庫(kù)

參考

Guide to uploading artifacts to the Central Repository
OSSRH Guide
Maven入門教程

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI