將項目發佈到 maven 中央倉庫踩過的坑

將項目發佈到 maven 中央倉庫踩過的坑

標籤 : webporter mavenhtml


[TOC]java


記錄第一次將項目發佈到 maven 中央倉庫踩過的坑和解決方案。git

大體步驟

  1. 註冊Sonatype的帳戶。地址:https://issues.sonatype.org/secure/Signup!default.jspagithub

  2. 提交發布申請。建立 Issue 地址:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134web

    • 項目類型是 Community Support - Open Source Project Repository Hostingshell

    • groupId 對應的域名你須要有全部權apache

  3. 使用 GPG 生成密鑰對。下載地址:https://www.gnupg.org/download/。用獲得的指令有以下幾條:服務器

    • gpg --version 檢查安裝成功沒intellij-idea

    • gpg --gen-key 生成密鑰對app

    • gpg --list-keys 查看公鑰

    • gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公鑰ID 將公鑰發佈到 PGP 密鑰服務器

    • gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公鑰ID 查詢公鑰是否發佈成功

  4. 配置 maven。須要修改的 Maven 配置文件包括:setting.xml(全局級別)與 pom.xml(項目級別)

    • setting.xml:在其中加入 server 信息,包含 Sonatype 帳號的用戶名與密碼

    • pom.xml:在其中配置 profile,包括插件和 distributionManagement,。snapshotRepository 與 repository 中的 id 必定要與 setting.xml 中 server 的 id 保持一致。

  5. 上傳構件到 OSS 中。mvn clean deploy -P release

  6. 在 OSS 中發佈構件。進入 https://oss.sonatype.org/,點擊"Staging Repositories" -> 在搜索欄輸入你的 groupId -> 勾選你的構件並點擊 close -> 點擊 tab 欄的 release。

  7. 通知 Sonatype 的工做人員關閉 issue。

等待審批經過後,就能夠在中央倉庫中搜索到本身發佈的構件了!下面是我在 maven 中央倉庫的構件:

webporter 發佈到 maven 中央倉庫

圖文教程參考

主要參考以下三個連接:

遇到的問題

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

插件沒找到

在 IntelliJ IDEA 中,pom.xml 裏的插件找不到並報紅,以下圖:

maven 依賴不識別

本身使用 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 -> Mavne -> Repositories -> Remote URL -> Update

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

maven 依賴不識別解決方案

方案一是參考下面第二個連接;方案二是我本身提出並驗證是有效的。

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 <weichuyang@163.com>
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錯誤

](http://www.cnblogs.com/chowmi...

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

參考連接


做者@brianway更多文章:我的網站 | CSDN | oschina

相關文章
相關標籤/搜索