發佈Jar包到Maven中央倉庫

註冊Sonatype用戶

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

使用了JIRA來管理流程,記住用戶名和密碼,後面會用到。

發佈構件的 Issue

登錄進入後,create Issue

輸入圖片說明

輸入圖片說明

之後回收到評論:

輸入圖片說明

如果是你的域名你就可以回覆,xxx.com is my domain 完了等待審覈,一般一天時間;

審覈通過後工作人員會回覆如下:

輸入圖片說明

使用 GPG 生成密鑰對

下載gpg4win:http://files.gpg4win.org/

選(帶vanilla的) gpg4win-vanilla-2.3.4.exe

查看是否安裝成功

gpg --version

生成密鑰對

gpg --gen-key

會讓你選擇加密的方式:

lease select what kind

(1) RSA and RSA (default)

(2) DSA and Elgamal

(3) DSA (sign only)

(4) RSA (sign only)

選1,之後往下,會讓你輸入用戶名和郵箱,還有一個Passphase,相當於密鑰庫密碼,不要忘記

查看公鑰

gpg --list-keys
pub   2048R/99F1B186 2018-02-02
uid       [ultimate] YaleRen (hello) <[email protected]>
sub   2048R/DBD61A9E 2018-02-02

99F1B186 就是公鑰ID

將公鑰發佈到 PGP 密鑰服務器

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 99F1B186

此後,可使用本地的私鑰來對上傳構件進行數字簽名,而下載該構件的用戶可通過上傳的公鑰來驗證簽名,也就是說,大家可以驗證這個構件是否由本人上傳的,因爲有可能該構件被壞人給篡改了

查詢公鑰是否發佈成功

#: gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 99F1B186

gpg: requesting key 99F1B186 from hkp server pool.sks-keyservers.net
gpg: key 99F1B186: "YaleRen (hello) <[email protected]>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

修改Maven配置文件

找到maven的全局配置文件settings.xml,在裏面找到 節點,這個節點默認是註釋掉的,增加如下配置:

setting.xml

<servers>
    <server>
        <id>oss</id>
        <username>Sonatype 用戶名</username>
        <password>Sonatype 密碼</password>
    </server>
</servers>

pom.xml

<project>
...
    <name>dexcoder-assistant</name>
    <description>dexcoder-assistant is a rapid development kit.</description>
    <url>http://www.dexcoder.com/</url>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <name>selfly</name>
            <email>[email protected]</email>
        </developer>
    </developers>
    <scm>
        <connection>scm:git:[email protected]:selfly/dexcoder-assistant.git</connection>
        <developerConnection>scm:git:[email protected]:selfly/dexcoder-assistant.git</developerConnection>
        <url>[email protected]:selfly/dexcoder-assistant.git</url>
    </scm>
...
    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
              <skip>true</skip>
            </configuration>
          </plugin>


                    <!-- Source -->
                    <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>
                    <!-- Javadoc -->
                    <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>
                    <!-- GPG -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.5</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>
...
</project>

pom.xml中必須包括:name、description、url、licenses、developers、scm 等基本信息,使用了 Maven 的 profile 功能,只有在 release 的時候,創建源碼包、文檔包、使用 GPG 進行數字簽名。

此外,snapshotRepository 與 repository 中的 id 一定要與 setting.xml 中 server 的 id 保持一致。

如果是多模塊項目的話,只需要在父pom.xml中聲明這些,子pom.xml中只需要修改相應的一些信息,如name標籤。

上傳構件到 OSS 中

mvn clean deploy -P release

當執行以上 Maven 命令時,會自動彈出一個對話框,需要輸入上面提到的 Passphase,它就是剛纔設置的 GPG 密鑰庫的密碼。

隨後會看到大量的 upload 信息,因爲在國內網絡的緣故,時間有點久,耐心等待吧。

注意:此時上傳的構件並未正式發佈到中央倉庫中,只是部署到 OSS 中了,下面纔是真正的發佈。

在 OSS 中發佈構件

登錄 https://oss.sonatype.org

在 OSS 中,使用自己的 Sonatype 賬號登錄後,可在 Staging Repositories 中查看剛纔已上傳的構件,這些構件目前是放在 Staging 倉庫中,可進行模糊查詢,快速定位到自己的構件(要拉到最下面)。

此時,該構件的狀態爲 Open,需要勾選它,然後點擊 Close 按鈕。系統會自動驗證該構件是否滿足指定要求,當驗證完畢後,狀態會變爲 Closed

輸入圖片說明

最後,點擊 Release 按鈕來發布該構件

輸入圖片說明

這裏頁面可能要刷新一下才能看到新的狀態。

通知 Sonatype 構件已成功發佈

這個前面的Sonatype工作人員其實在審覈你的Issue時,在comment中已經提示你了,My repository had released!

在Issue下面回覆一條“構件已成功發佈”的評論,這是爲了通知 Sonatype 的工作人員爲需要發佈的構件做審批,發佈後會關閉該Issue。

等待構件審批通過

這個,又只能等待了,當然他們晚上上班,還是第二天看。當審批通過後,將會收到郵件通知。

從中央倉庫中搜索構件

這時,就可以在maven的中央倉庫中搜索到自己發佈的構件了,以後可以直接在pom.xml中使用了!

中央倉庫搜索網站:http://search.maven.org/

第一次成功發佈之後,以後就不用這麼麻煩了,可以直接使用Group Id發佈任何的構件,當然前提是Group Id沒有變。

以後的發佈流程:

a)構件完成後直接使用maven在命令行上傳構建;

b)在https://oss.sonatype.org/ close並release構件;

c)等待同步好(大約2小時多)之後,就可以使用了

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