1. 程式人生 > 程式設計 >如何把JAR釋出到maven中央倉庫的幾種方法

如何把JAR釋出到maven中央倉庫的幾種方法

詳細描述maven中央倉庫釋出jar包的中間過程,以及遇到的一些問題彙總,儘量用文字描述清楚,耐心看下去,就一定會發布成功

----Sonatype篇----

名詞解釋:
Sonatype Nexus: Sonatype Nexus helps software development teams use open source so they can innovate faster and automatically control risk

maven社群唯一指定的倉庫地址為: https://search.maven.org/ 所以,我們現在用的遠端倉庫地址,無論是哪個,都是需要去這個倉庫同步index的,然後在把依賴同步到自己的倉庫下面

整個maven倉庫網路是一個拓撲型架構,倉庫與倉庫之間可以互相依賴,且互相索引

如果想上傳jar到公服倉庫,那麼首先要打通的就是sonatype,我們首先去sonatype官網註冊個賬號,訪問https://issues.sonatype.org/secure/Dashboard.jspa這個地址是sonatype
控制檯,如果未登入,則會先跳轉到登入介面,點選註冊,填寫相關資訊,註冊個賬號然後登入

1. 進入控制檯之後,點選新建按鈕,新建一個問題(issue)

project   選擇community support -open source....
issue    選擇new project
summary   填寫一些專案簡短描述
description 填寫專案的描述
group id   這個最重要了,要與你的工程pom檔案裡的頂級group id 一致,而且這個是不能亂填的,一般來說,是你自己的域名反寫
Project URL 進入到你專案的url,假如你的專案在github上,先進到你的專案中,然後複製位址列上的地址填進去就可以
SCM url   同上,但是這個url要填寫你的git資源下載地址,就是專案右側的clone&download那個按鈕彈出來的地址
Username(s) 使用者名稱
Already Synced to Central 預設選NO就可以,因為暫時我們還不需要同步到中央倉庫

2. 等待ISSUE審批,2分鐘左右,你就會收到審批結果(同時會給你的郵箱發郵件),告訴你,要驗證你的域名,也就是你的

group id對應的域名,會通過以下幾種方式驗證這個域名是不是你的

Central OSSRH updated OSSRH-xxxxx:
----------------------------------
  Status: Waiting for Response (was: Open)

Do you own the domain gomyck.com? If so,please verify ownership via one of the following methods:
* Add a TXT record to your DNS referencing this issue (Fastest)
* Setup a redirect to your Github page (if it does not already exist)
* Send an email to [email protected] referencing this issue from a gomyck.com email address

If you do not own this domain,please read:
http://central.sonatype.org/pages/choosing-your-coordinates.html
You may also choose a groupId that reflects your project hosting,in this case,something like 

這個裡面告訴你,最快的辦法就是在你的域名解析中,新增一條text記錄,我當時就是用的這種方式,具體操作如下:

  • 進入到域名商網站,登入之後選擇域名解析
  • 點選新增記錄,記錄型別選擇text
  • 主機記錄不要寫(預設是@)
  • 記錄值寫你的問題編號
  • 其他都不需要改,點選確定

如果填的沒問題的話,大概10分鐘左右,你就會收到稽核通過的訊息,告訴你可以上傳資源了

com.gomyck has been prepared,now user(s) gomyck,mzxc can:

Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
Promote staged artifacts into repository 'Releases'
Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public
Download snapshot,release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging
please comment on this ticket when you promoted your first release,thanks

他告訴你,如果你提交了版本,最好告訴他一下,不用管,以後我們也不會理他的

----GPG篇----

使用 GPG 生成金鑰對

Windows下載 Gpg4win 軟體來生成金鑰對,地址:https://www.gpg4win.org/download.html

我用的mac,使用brew安裝的gpg brew install gnupg

使用以下命令來生成祕鑰對:

$ gpg --gen-key
#按照提示輸入資訊,在輸入密碼的時候,如果嫌麻煩就直接摁回車就可以,這樣祕鑰就沒有密碼保護了,密碼保護只有在你的私鑰洩露的時候才有用,其他時候沒用

$ gpg --list-keys
#這個指令會顯示你的祕鑰環,類似於下面這樣
#---------------------------------
# pub  rsa4096 2018-09-25 [SC] 
#  EABB59A7BFXXXXXX46604F95ED1503AA8CDxxxx (這個才是祕鑰ID)
# uid      [ 絕對 ] xxx (zhushi) <[email protected]>
# sub  rsa4096 2018-09-25 [E]

$ gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 祕鑰ID
#傳送你的公鑰到祕鑰倉庫,以後你的jar會使用私鑰簽名,maven中央倉庫會去幾個指定的祕鑰倉庫去找公鑰來驗證這個簽名,如果不上傳是不能通過稽核的

$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 祕鑰ID
#驗證你的公鑰是否上傳成功,如果返回結果是 **未改變,那就是成功了

----maven篇----

在你的maven頂級工程中加入以下配置

<groupId>com.gomyck</groupId>
<artifactId>gomyck-quickdev</artifactId>
<packaging>pom</packaging>
<version>1.1.0-SNAPSHOT</version>
<name>gomyck-quickdev</name>
<url>http://www.gomyck.com</url>
<description>gomyck 快速開發平臺</description>
<developers>
  <developer>
    <id>gomyck</id>
    <name>haoy</name>
    <url>www.gomyck.com</url>
    <email>[email protected]</email>
  </developer>
</developers>

<licenses>
  <license>
    <name>The Apache Software License,Version 2.0</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
  </license>
</licenses>

<scm>
  <connection>scm:git:[email protected]:mzxc/gomyck-fastdfs-spring-boot-starter.git</connection>
  <developerConnection>scm:git:[email protected]:mzxc/gomyck-fastdfs-spring-boot-starter.git</developerConnection>
  <url>[email protected]:mzxc/gomyck-fastdfs-spring-boot-starter.git</url>
</scm>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>3.8.0</version>
      <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-gpg-plugin</artifactId>
      <version>1.6</version>
      <executions>
        <execution>
          <phase>verify</phase>
          <goals>
            <goal>sign</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <!-- 你的祕鑰ID -->
        <keyname>${gpg.keyname}</keyname>
        <passphraseServerId>${gpg.keyname}</passphraseServerId>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
      <version>3.1.1</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<distributionManagement>
  <repository>
    <id>sonatype-nexus-staging</id>
    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
  </repository>
  <snapshotRepository>
    <id>sonatype-nexus-snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </snapshotRepository>
</distributionManagement>

在你的settings.xml中加入以下配置

<server>
  <id>sonatype-nexus-snapshots</id>
  <username>sonatype賬號</username>
  <password>sonatype密碼</password>
</server>
<server>
  <id>sonatype-nexus-staging</id>
  <username>sonatype賬號</username>
  <password>sonatype密碼</password>
</server>
<profiles>
 <profile>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
    <gpg.keyname>你的祕鑰 ID</gpg.keyname>
   </properties>
 </profile>
</profiles>

執行deploy,如果按照我的步驟一步一步走,應該沒有錯誤(如果報錯也是你的doc問題,按照提示一步一步改好,否則maven稽核會失敗)

----nexus篇----

如果上一步沒有錯誤的話,訪問網址: https://oss.sonatype.org/#stagingRepositories

點選右上角登入,賬號密碼就是sonatype的賬號密碼,登入進去之後,點選Staging repositories

在右側搜尋框輸入你的group id,然後點選refresh,就會看到你的提交資訊

選中,點選close,這時當前的紀錄就會變成一個小齒輪,表示nexus在校驗你的jar

按照劇本,你的提交應該會全部通過(圖示顯示數字就是失敗,反之則是成功)

再次選中當前記錄,點選release,就會上傳成功了,這時sonatype會給你發郵件

以後你只需要按照maven篇deploy,然後在使用nexus篇提交release就可以了

注意: 如果是snapshot版本,則不需要稽核,直接deploy就可以直接引用,但是在maven倉庫(https://search.maven.org/)是搜尋不到的

----爬坑篇----

1.一開始總是提示檔案驗證簽名失敗,提示我說沒有在祕鑰倉庫找到對應的公鑰,但是我本地可以確定的是提交了且ID一致,後來過了大概1小時,驗證忽然就過了,
這期間,我把nexus提示的祕鑰倉庫地址都複製出來,挨個上傳我的公鑰,最後不知道是哪個倉庫生效了,但是我覺得是倉庫延遲問題

2.以後寫程式碼一定要把doc寫好,不然遇見這種場景,簡直就是折磨,尤其doc多的時候

3.如果你有多個gpg祕鑰,一定要指定祕鑰ID,不然gpg外掛是祕鑰環的順序來對你的工程簽名的,這會導致你上傳的公鑰不一定對應的上簽名的私鑰,祕鑰的ID請在settings.xml中配置profile,gpg外掛的配置請到apache-gpg官網看

4.sonatype在國內環境下,訪問非常困難,建議找個科學上網的方式,來實踐本教程

到此這篇關於如何把JAR釋出到maven中央倉庫的幾種方法的文章就介紹到這了,更多相關JAR釋出到maven中央倉庫內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!