Maven公共中央倉庫發佈自己的Jar包

Maven公共中央倉庫發佈自己的Jar包

流程概要

  1. 創建工單
  2. 配置環境和項目
  3. 發佈jar包

一、創建工單

1. 註冊賬號

https://issues.sonatype.org/secure/Signup!default.jspa ,前往該地址註冊一個 sonatype 賬號

2. 創建工單

登錄註冊的賬號,頁面控制面板上方菜單,有一個新建按鈕(由於語言設置的不同,你的界面可能是英文)。點擊,創建一個工單,如下圖:
在這裏插入圖片描述
如果你有自己的域名,可以寫你自己域名。沒有的話可以用github賬號作爲組織域名標識。假如你的github用戶名爲xxx, 項目名爲simple,則可以這樣填寫:

項目:Community Support - Open Source Project Repository Hosting (OSSRH)
問題類型:New Project
Group Id:com.github.xxx
Project URL:https://github.com/xxx/simple
SCM url:https://github.com/xxx/simple.git

創建後進入自己的工單,如下所示:
在這裏插入圖片描述
我的工單已經被管理員審批通過,所以是已解決狀態。沒有審批過的是開放狀態,需要等待管理員的審批,由於時區不同,東半球的白天,西半球卻是黑夜,耐心等待即可。

二、配置環境和項目

1. 配置maven setting.xml

上傳jar到公共的中央倉庫,首先要在setting.xml加入一些配置,如下

  <servers>
    <server>
      <id>snapshot</id>
      <username>sonatype用戶名</username>
      <password>sonatype密碼</password>
    </server>
    <server>
      <id>release</id>
      <username>sonatype用戶名</username>
      <password>sonatype密碼</password>
    </server>
  </servers>

2. 配置pom.xml

我把自己項目的pom.xml文件粘貼如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.github.rxyor</groupId>
    <artifactId>carp-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>carp</name>
    <description>carp is project which contains common utils ,redis delay job , distributed lock and
        so on!
    </description>
    <url>https://github.com/rxyor/carp</url>
    
     <properties>
        <!--project-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--plugin-->
        <maven-source-plugin.version>3.0.1</maven-source-plugin.version>
        <maven-clean-plugin.version>3.0.0</maven-clean-plugin.version>
        <maven-resources-plugin.version>3.0.2</maven-resources-plugin.version>
        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <maven-surefire-plugin.version>2.20.1</maven-surefire-plugin.version>
        <maven-war-plugin.version>3.2.0</maven-war-plugin.version>
        <maven-install-plugin.version>2.5.2</maven-install-plugin.version>
        <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
        <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
    </properties>

    <!--構建配置-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <charset>UTF-8</charset>
                    <docencoding>UTF-8</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <additionalJOption>-Xdoclint:none</additionalJOption>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <scm>
        <url>https://github.com/rxyor/carp</url>
        <connection>scm:git:https://github.com/rxyor/carp.git</connection>
        <developerConnection>scm:git:https://github.com/rxyor/carp</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>rxyor</name>
            <email>[email protected]</email>
            <url>https://github.com/rxyor</url>
        </developer>
    </developers>
    <profiles>
        <profile>
            <id>release</id>
            <activation>
                <jdk>[1.8,)</jdk>
            </activation>
            <properties>
                <additionalparam>-Xdoclint:none</additionalparam>
            </properties>
            <distributionManagement>
                <snapshotRepository>
                    <!--id 要與setting.xml server id一致-->
                    <id>snapshot</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
                </snapshotRepository>
                <repository>
                    <!--id 要與setting.xml server id一致-->
                    <id>release</id>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
                </repository>
            </distributionManagement>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>release</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>snapshot</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>${maven-gpg-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>release</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>release</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

3. pom.xml配置說明

上傳到中央倉庫並可以成功發佈有以下前提條件:

  • 包含jar源碼包
  • 包含java doc
  • 包含簽名校驗
  • 版本號包含snapshot的可能也無法發佈成功
    等…

pom.xml配置的幾個插件就是解決上述問題的,

  • maven-source-plugin用於生成源碼包
  • maven-javadoc-plugin用於生成java doc,大部分代碼的java doc註釋都不規範,爲了防止不規範的java doc註釋導致打包失敗,插件可以加入如下配置:
<configuration>
	<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
  • maven-gpg-plugin用於添加簽名

4. 安裝並配置gpg

#安裝gpg
brew install gpg
#生成key
gpg --gen-key
#輸入對應信息
Real name: 
Email address: 

#查詢生成的公鑰的信息
gpg --list-keys

---------------------------------
pub   rsa2048 2019-05-30 [SC] [expires: 2021-05-29]
      B619ABB4B37B166061CD17352FCF6A922383XXXX
uid           [ultimate] rxyor <[email protected]>
sub   rsa2048 2019-05-30 [E] [expires: 2021-05-29]

#上傳到密鑰服務器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: sending key 2FCF6A922383D13B to hkp://keyserver.ubuntu.com:11371

#查詢密鑰是否上傳成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: key 2FCF6A922383D13B: "rxyor <[email protected]>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

三、發佈jar包

1. deploy

工程根目錄打開命令行工具,輸入如下命令:

mvn clean deploy -P release -Darguments=gpg.passphrase="gpg密鑰密碼"

即使按照上述教程配置,你有可能遇到如下錯誤:

1) gpg簽名錯誤

[INFO] --- maven-gpg-plugin:1.6:sign (release) @ carp-parent ---
gpg: signing failed: Inappropriate ioctl for device
gpg: signing failed: Inappropriate ioctl for device
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (release) on project carp-parent: Exit code: 2 -> [Help 1]

如果發生錯誤,你的gpg可能需要額外的配置,以Mac爲例,在gpg安裝目錄(~/.gnup)下建立兩個配置文件:gpg.conf、gpg-agent.conf

# gpg.conf中加入
use-agent
pinentry-mode loopback
# gpg-agent.conf中加入
allow-loopback-pinentry

2) 400或401錯誤
請注意檢查是否存在以下情況:

  • 發佈到release時版本號不能包含snapshot
  • setting.xml 密碼配置不對
  • setting.xml 中server id 與pom.xml配置不一致
  • maven環境變量配置setting.xml位置與自己配置的setting.xml不是同一個

解決上述錯誤後,再次執行上述命令,不出意外的話應該就可以deploy成功了。

2. Staging Repositories 關閉倉庫操作

前往https://oss.sonatype.org/#stagingRepositories ,並使用sonatype賬戶登錄, 按照下圖所示關閉倉庫
在這裏插入圖片描述
關閉倉庫的時候需要檢查是否出現錯誤,如果有錯誤需要按錯誤提示信息進行解決,如下圖可以查看錯誤信息:
在這裏插入圖片描述
但實際上,由於pom.xml加入了插件:nexus-staging-maven-plugin,這插件在deploy成功後會自動幫你進行Staging Repositories Close操作。

3. 提醒管理員審批

關閉成功後,還需要在你的工單中提醒管理員你的組件發佈成功,可以添加如下注釋:

Component has been successfully issued

管理審批通過後,2個小時左右你就能在公共中央倉庫搜索到發佈的jar包了。
在這裏插入圖片描述

4. 中央倉庫搜索發佈的jar包

中央倉庫搜索地址:https://search.maven.org/
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章