如何發佈自己的 jar 包到 maven 中央倉庫

原文來自 xjtushilei

偶然看到了個軟件設計大賽,就設計了個通用的爬蟲框架,想做到java爬蟲會優先考慮我的框架。這個任務還需要很長的時間去努力!

當然了,爲了讓別人用,肯定要發佈到中央倉庫的。

準備開車!

註冊

首先到sonatype官網進行註冊!

找到註冊“sign up”,如圖,進行註冊!

638681697dc842aea1a5a4c6609c875e-1.png

登錄

登錄後,在頁面上方點擊“create”進行創建issue。

獲得如下圖所示。

b15212d73f2e4f7a93898d5d333b04c2-2.png

  • Summary:寫你想做什麼,你的包的功能。簡單概述,要短一點。
  • Description:可以直接複製summary,然後加一點描述信息。
  • Group Id :推薦寫github。例如我的是 **com.github.xjtushilei** ,很快能通過,自己域名的話我沒試過,雖然我有自己的域名。
  • Project UR : 你的項目的描述,填寫你的項目的github地址就可以了。
  • SCM url: 有填寫說明的。
  • Username(s):邀請其他人有權限一起完成這個項目。我當時沒填。

等待 Issue 審批通過

網上說需要一天到兩天。爲何我這一步是秒過的。大概20秒過後我就接收到通過郵件了。

或許是我填寫的github比較好認真真實性。

配置GPG

如果是 Windows 操作系統,需要下載 Gpg4win 軟件來生成密鑰對。建議大家下載 Gpg4win-Vanilla 版本,因爲它僅包括 GnuPG,這個工具纔是我們所需要的。

安裝 GPG 軟件後,打開命令行窗口,依次做以下操作:

  1. 查看是否安裝成功

gpg --version

能夠顯示 GPG 的版本信息,說明安裝成功了。

  1. 生成密鑰對

gpg --gen-key

此時需要輸入姓名、郵箱等字段,其它字段可使用默認值,此外,還需要輸入一個 Passphase,相當於一個密鑰庫的密碼,一定不要忘了,也不要告訴別人,最好記下來,因爲後面會用到。

  1. 查看公鑰

gpg --list-keys

如下:

D:\IdeaProjects\ScriptSpider>gpg --list-keys
C:/Users/shilei/AppData/Roaming/gnupg/pubring.gpg
-------------------------------------------------
pub   2048R/CAD14E5B 2017-04-11
uid       [ultimate] shilei 
sub   2048R/91AAE050 2017-04-11

我的公鑰是:CAD14E5B

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

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

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

  1. 查詢公鑰是否發佈成功

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys CAD14E5B

實際上就是從 key server 上通過公鑰 ID 來接收公鑰,此外,也可以到 sks-keyservers.net 上通過公鑰 ID 去查詢。

修改 Maven 配置文件



 ...     
 <servers>     
     <server>         
     <id>oss</id>  //這裏要和後面呼應        
     <username>用戶名</username>         
     <password>密碼</password>      
     </server>   
 </servers>     
 ...

修改你工程的pom文件

你可以增加你自己其他的,這個是我當時的pom。


    <groupId>com.github.xjtushilei</groupId>
    <artifactId>scriptspider</artifactId>
    <version>0.1.1</version>
    <packaging>jar</packaging>

    <name>scriptspider</name>
    <description>ScriptSpider(SS) is a Java distributed crawler framework that supports hot swappable components.SS是一個java版本的分佈式的通用爬蟲,可以熱插拔各個組件(提供默認的),自動切換代理,自動結構化數據與存儲。使用redis,分佈式調度等技術。
    </description>
    <url>https://github.com/xjtushilei/ScriptSpider</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>shilei</name>
            <email>[email protected]</email>
        </developer>
    </developers>

    <scm>
        <connection>
            scm:git:https://github.com/xjtushilei/ScriptSpider.git
        </connection>
        <developerConnection>
            scm:git:https://github.com/xjtushilei/ScriptSpider.git
        </developerConnection>
        <url>https://github.com/xjtushilei/ScriptSpider</url>
    </scm>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        。。。


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <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>
                        <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>
                        <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>

上傳構件到 OSS 中

mvn clean deploy -P release

或者

mvn clean deploy -P release -Dgpg.passphrase=密碼

第一個會彈出來讓你輸入密碼,第二個是告訴它你的密碼。Passphase就是GPG 密鑰對的密碼,只有自己才知道。在之前的步驟中已經告訴你要牢記了。

隨後會看到大量的 upload 信息,而且速度比較慢,如果出現timeout,需要反覆嘗試。

在 OSS 中發佈構件

在 OSS 中,使用自己的 Sonatype 賬號登錄後,再左側的 Staging Repositories 中,在最下方可以看到剛纔已上傳的構件,這些構件目前是放在 Staging 倉庫中,可進行模糊查詢,快速定位到自己的構件。此時,該構件的狀態爲 Open,需要勾選它,然後點擊 Close 按鈕。接下來系統會自動驗證該構件是否滿足指定要求,當驗證完畢後,狀態會變爲 Closed,最後,點擊 Release 按鈕來發布該構件。

這一步我忘記了點release就進行了下一步,當然,我得到了一個回覆。不得不說,他們的回覆還是態度很好!

通知 Sonatype“構件已成功發佈”

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

我的 issue 展示

通過該截圖,你應該知道我們都做了什麼。

f289a058f0fc4b808143f0d9c0b23650-3.png

如果是發佈的話,請注意artifactId不能是 -SNAPSHOT

因爲從截圖看(經過證明),一旦你的issue第一次被批准,你就可以發佈-SNAPSHOT了,而且我當時嘗試了使用我的-SNAPSHOT版本的jar,並不需要進行後續操作。

等待構件審批通過

沒錯,還是要等,我大概等了一個多小時吧。感覺他們的效率還是很高,沒有我查到的別人用一到兩天。

從中央倉庫中搜索構件

最後,就可以到中央倉庫中搜索到自己發佈的構件了!

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

等多長時間呢?看我的郵件列表的回覆。意思大概是10分鐘就可以使用了,2小時才能被搜索到。我猜用鏡像的應該更慢,需要同步的。

After you successfully release, your component will be published to Central, typically within 10 minutes, though updates to search.maven.org can take up to two hours.

第一次之後

你就可以安心的開車了~

以後的發佈流程是:

  1. 構件準備好之後,在命令行上傳構建

  2. https://oss.sonatype.org/ “close”並“release”構件

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

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