天天看點

将項目釋出到 Maven 中央倉庫踩過的坑

大緻步驟

  1. 注冊 Sonatype 的賬戶。位址:​​https://issues.sonatype.org/secure/Signup!default.jspa​​
  2. 送出釋出申請。建立 Issue 位址:​​https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134​​
  • 項目類型是 ​

    ​Community Support - Open Source Project Repository Hosting​

  • groupId 對應的域名你需要有所有權
  1. 使用 GPG 生成密鑰對。下載下傳位址:​​https://www.gnupg.org/download/​​。用得到的指令有如下幾條:
  • ​gpg --version​

    ​ 檢查安裝成功沒
  • ​gpg --gen-key​

    ​ 生成密鑰對
  • ​gpg --list-keys​

    ​ 檢視公鑰
  • ​gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公鑰ID​

    ​ 将公鑰釋出到 PGP 密鑰伺服器
  • ​gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公鑰ID​

    ​ 查詢公鑰是否釋出成功
  1. 配置 Maven。需要修改的 Maven 配置檔案包括:​

    ​setting.xml​

    ​​(全局級别)與 ​

    ​pom.xml​

    ​(項目級别)
  • setting.xml:在其中加入 server 資訊,包含 Sonatype 賬号的使用者名與密碼
  • pom.xml:在其中配置 profile,包括插件和 ​

    ​distributionManagement​

    ​,。snapshotRepository 與 repository 中的 id 一定要與 setting.xml 中 server 的 id 保持一緻。
  1. 上傳構件到 OSS 中。​

    ​mvn clean deploy -P release​

  2. 在 OSS 中釋出構件。進入 ​​https://oss.sonatype.org/​​,點選”Staging Repositories” -> 在搜尋欄輸入你的 groupId -> 勾選你的構件并點選 close -> 點選 tab 欄的 release。
  3. 通知 Sonatype 的從業人員關閉 issue。

等待審批通過後,就可以在中央倉庫中搜尋到自己釋出的構件了!下面是我在 Maven 中央倉庫的構件:

  • 搜尋位址: ​​https://search.maven.org/​​
  • 中央倉庫位址:​​http://mvnrepository.com/​​
  • 我釋出的構件位址:​​http://mvnrepository.com/artifact/com.github.brianway​​

圖文教程參考

主要參考如下三個連結:

  • ​​将 Smart 構件釋出到 Maven 中央倉庫 ​​by 黃勇
  • ​​釋出Maven構件到中央倉庫​​ by 阿信sxq
  • ​​将項目釋出到Maven中央庫​​ by 路小磊 (截圖詳細)

遇到的問題

在這個過程中遇到許多奇奇怪怪的問題,下面依次說明

插件沒找到

在 IntelliJ IDEA 中,pom.xml 裡的插件找不到并報紅,如下圖:

自己使用 mvn 指令建構的話,會提示

Plugin '''org.apache.maven.plugins:maven-gpg-plugin:1.6''' not found
Inspects a Maven model for resolution problems.      

兩種解決方法:

  • 方案一:手動下載下傳
mvn dependency:get -DrepoUrl=http://repo.maven.apache.org/maven2/ -Dartifact=org.apache.maven.plugins:maven-gpg-plugin:1.6      
  • 方案二:在 IntelliJ IDEA 中更新 ​

    ​Indexed Maven Repositories​

步驟: IntelliJ IDEA -> Preferences -> Build,Execution,Deployment -> Build Tools -> Maven -> Repositories -> Remote URL -> Update

這個過程耗時視網速而定,大概 5~10 分鐘。

方案一是參考下面第二個連結;方案二是我自己提出并驗證是有效的。

  • ​​IntelliJ IDEA shows plugin not found​​
  • ​​Maven plugins can not be found in IntelliJ​​

GPG 版本問題

我用的是 mac,下載下傳的是二進制發行包 GnuPG for OS X,然後發現是在 terminal 輸入的指令是 ​

​gpg2​

​​ 而不是 ​

​gpg​

​。比如,公鑰顯示如下:

$ gpg2 --list-keys 
/Users/brian/.gnupg/pubring.kbx
-------------------------------
pub   rsa2048 2017-05-10 [SC] [expires: 2019-05-10]
      DBD686EC6F4E34C4096C427506755FE5978EC644
      DBD686EC6F4E34C4096C427506755FE5978EC644
uid                      brianway <[email protected]>
sub   rsa2048 2017-05-10 [E] [expires: 2019-05-10]      

但 Maven 裡面的 maven-gpg-plugin 插件預設是使用 ​

​gpg​

​​ 指令,不是 ​

​gpg2​

​,是以需要配置 setting.xml 的 profile

<profiles>
    <profile>
        <id>gpg</id>
        <properties>
            <gpg.executable>gpg2</gpg.executable>
            <gpg.passphrase>mypassphrase</gpg.passphrase>
        </properties>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>gpg</activeProfile>
</activeProfiles>      
參考:​​http://stackoverflow.com/questions/14114528/avoid-gpg-signing-prompt-when-using-maven-release-plugin​​

釋出出問題

輸入 ​

​mvn clean deploy -P release​

​ 後,報錯如下:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] webporter-parent ................................... SUCCESS [ 11.690 s]
[INFO] webporter-core ..................................... SUCCESS [  5.208 s]
[INFO] webporter-data-elasticsearch ....................... SUCCESS [  2.769 s]
[INFO] webporter-collector-zhihu .......................... FAILURE [  7.889 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28.069 s
[INFO] Finished at: 2017-05-11T18:11:44+08:00
[INFO] Final Memory: 45M/723M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy (injected-nexus-deploy) on project webporter-collector-zhihu: Failed to deploy artifacts: Could not transfer artifact com.github.brianway:webporter-data-elasticsearch:jar:javadoc:1.0-20170511.101142-1 from/to sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots/): Failed to transfer file: https://oss.sonatype.org/content/repositories/snapshots/com/github/brianway/webporter-data-elasticsearch/1.0-SNAPSHOT/webporter-data-elasticsearch-1.0-20170511.101142-1-javadoc.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :webporter-collector-zhihu      

搞了幾個小時,最後發現是 pom.xml 的 ​

​distributionManagement​

​ 中 snapshotRepository 與 repository 中的 id 與 setting.xml 中 server 的 id 不一緻 導緻的。因為我的 pom.xml 是模仿的 ​​webmagic​​ 的 pom.xml,而 settings.xml 的 server 配置卻是複制的​​《将 Smart 構件釋出到 Maven 中央倉庫》 by 黃勇 ​​中的,結果導緻不一樣。

我主要從下面這篇文章中找到的靈感

​​Maven2部署構件到Nexus時出現的Failed to transfer file錯誤​​

github 上 release

在項目的 pom.xml 中配置 release 的插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>${plugin.release.version}</version>
    <configuration>
        <tagNameFormat>v@{project.version}</tagNameFormat>
        <autoVersionSubmodules>true</autoVersionSubmodules>
    </configuration>
</plugin>      

使用下面的指令即可直接在 github 遠端倉庫生成發行版本

mvn clean
mvn release:prepare
mvn release:perform      

我實驗的結果是,執行 ​

​mvn release:prepare​

​​ 并不會對遠端倉庫造成影響,再執行 ​

​mvn release:perform​

​​ 才會在 git 遠端倉庫多出兩個 commit 和一個 release 版本,檢視送出日志 ​

​git log --pretty=oneline​

​,内容如下:

2619dfaafca7a82f70e938f5b6efd6d9f4110554 [maven-release-plugin] prepare for next development iteration
01795a07b793930b74dc0cb5e6ce2b43ee3a5434 [maven-release-plugin] prepare release v1.0      
參考: ​​maven scm 配置git​​

用上面的方法,不另外配置的話,commit message 是由 Maven 插件自動生成的。也可以不使用插件釋出,自己 commit 上去,然後在 github 上釋出。

參考: ​​Creating Releases 建立釋出包​​

構件沒有出現在 Staging Repositories

按照教程裡的步驟,上傳構件到 OSS 中後,應該先在 https://oss.sonatype.org/ 中,點選”Staging Repositories” -> 在搜尋欄輸入你的 groupId -> 勾選你的構件并點選 close -> 點選 tab 欄的 release。

然而我卻沒有這個步驟,即我上傳構件成功後,在 Staging Repositories 中并沒有找到自己的構件,但在左邊側欄的 Artifact Search 框輸入自己的 groupId 卻能搜到我的構件,百思不得其解。最後還是求助 Sonatype 的從業人員才弄清楚,原來是我使用了插件 nexus-staging-maven-plugin 并且預設 autoReleaseAfterClose 是 true 導緻的,直接越過了手工 close 的步驟。

Sonatype 從業人員的答複:

It sounds like you have the nexus-staging-maven-plugin in your build (assuming you’re using Maven, but what I’m going to say might also apply to other tools) configured to autoReleaseAfterClose. If this is the case, oss.sonatype.org will automatically release your staging repository after it has been successfully closed. oss.sonatype.org is also configured, by default, to drop released staging repositories. Once your artifacts have been released, they will appear in the Releases repository on oss.sonatype.org, and from there they will sync to Maven Central. You shouldn’t expect to see staging repositories if everything worked and if all your components passed the ruleset validations. The fact that you can search for your artifacts on search.maven.org is a good sign.

具體可以參看我提的 issue: ​​OSSRH-31186​​

建構報錯 gpg: signing failed: Inappropriate ioctl for device

原因是 gpg 在目前終端無法彈出密碼輸入頁面。

解決辦法很簡單:

export GPG_TTY=$(tty)      

參考連結

  • ​​将 Smart 構件釋出到 Maven 中央倉庫 ​​by 黃勇
  • ​​釋出Maven構件到中央倉庫​​ by 阿信sxq
  • ​​将項目釋出到Maven中央庫​​ by 路小磊 (截圖詳細)
  • ​​GPG入門教程​​
  • ​​Working with PGP Signatures​​
  • ​​Creating Releases 建立釋出包​​
  • ​​Sonatype OSSRH-31186​​