手把手教你如何將項目發佈到Maven中央倉庫

業餘時間寫了個輕量級的權限控制框架 light-security[1] ,併發布到了 Maven 中央倉庫。發佈時的操作步驟還挺多,我這個記性是記不住的,所以記錄一下,便於以後查閱,也希望對大家有幫助。

一、Sonartype相關準備工作

1 前往 Sonartype[2] 註冊賬號,並記好賬號和密碼,後面有用

2 前往 Sonartype Dashboard[3] ,點擊導航欄上的 Create 按鈕,並按提示填寫項目信息:

手把手教你如何將項目發佈到Maven中央倉庫

個人為項目 light-security-spring-boot-starter 填寫的信息如 OSSRH-47914[4] 所示 ,供大家參考。

3 創建 Issue 後,等待審核即可。一般會在一個工作日內審核完成。當Issue的Status變為RESOLVED 或 FIXED 後,即可進行下一步操作

二、GPG相關準備工作

2.1 安裝GPG

Mac安裝GPG:

brew install gpg

Ubuntu安裝GPG:

sudo apt-get install gnupg

2.2 GPG常用命令

gpg --version 檢查安裝成功沒
gpg --gen-key 生成密鑰對
gpg --list-keys 查看公鑰
gpg --keyserver 服務器地址 --send-keys 公鑰ID 將公鑰發佈到 PGP 密鑰服務器
gpg --keyserver 服務器地址 --recv-keys 公鑰ID 查詢公鑰是否發佈成功

2.3 生成秘鑰

gpg --gen-key
按照提示輸入姓名/郵箱,然後按O即可生成。

如果遇到問題,可詳見”遇到的問題一節”。

2.4 查看本地秘鑰

gpg --list-keys

結果類似如下:

➜ ~ gpg --list-keys
/Users/itmuch.com/.gnupg/pubring.kbx
------------------------------------
pub rsa2048 2019-04-20 [SC] [有效至:2021-04-19]
[xxxxxxxxx]
uid [ 絕對 ] itmuch.com <eacdy0000>
sub rsa2048 2019-04-20 [E] [有效至:2021-04-19]
/<eacdy0000>

三、配置Maven

TIPS

可參考官方文檔配置https://central.sonatype.org/pages/apache-maven.html

1 修改項目的pom.xml,添加如下內容:

<licenses>
<license>
<name>The Apache Software License, Version 2.0/<name>

http://www.apache.org/licenses/LICENSE-2.0.txt
/<license>
/<licenses>
<developers>
<developer>
<name>itmuch/<name>
<email>[email protected]/<email>
https://github.com/eacdy
/<developer>
/<developers>

https://github.com/eacdy/light-security
<connection>https://github.com/eacdy/light-security.git/<connection>
<developerconnection>https://github.com/eacdy/<developerconnection>

<distributionmanagement>
<snapshotrepository>
ossrh
https://oss.sonatype.org/content/repositories/snapshots
/<snapshotrepository>
<repository>
ossrh
https://oss.sonatype.org/service/local/staging/deploy/maven2/
/<repository>
/<distributionmanagement>
<profiles>
<profile>
release
<build>
<plugins>
<plugin>
<groupid>org.sonatype.plugins/<groupid>

<artifactid>nexus-staging-maven-plugin/<artifactid>
<version>1.6.7/<version>
<extensions>true/<extensions>
<configuration>
<serverid>ossrh/<serverid>
<nexusurl>https://oss.sonatype.org//<nexusurl>
<autoreleaseafterclose>true/<autoreleaseafterclose>
/<configuration>
/<plugin>
<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-release-plugin/<artifactid>
<version>2.5/<version>
<configuration>
<autoversionsubmodules>true/<autoversionsubmodules>
<usereleaseprofile>false/<usereleaseprofile>
<releaseprofiles>release/<releaseprofiles>
<goals>deploy/<goals>
/<configuration>
/<plugin>
<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-source-plugin/<artifactid>
<version>2.2.1/<version>
<executions>
<execution>
attach-sources
<goals>
<goal>jar-no-fork/<goal>
/<goals>
/<execution>
/<executions>
/<plugin>
<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-javadoc-plugin/<artifactid>
<version>2.9.1/<version>
<executions>
<execution>
attach-javadocs
<goals>
<goal>jar/<goal>
/<goals>
/<execution>
/<executions>
/<plugin>

<plugin>
<groupid>org.apache.maven.plugins/<groupid>
<artifactid>maven-gpg-plugin/<artifactid>
<version>1.5/<version>
<executions>
<execution>
sign-artifacts
<phase>verify/<phase>
<goals>
<goal>sign/<goal>
/<goals>
/<execution>
/<executions>
/<plugin>
/<plugins>
/<build>
/<profile>
/<profiles>

可參考我的配置:Light Security Spring Boot Starter Pom.xml[5]

2 修改 $MAVEN_HOME/conf/settings.xml 文件(即你的Maven配置文件),添加如下內容:

<server>

ossrh

<username>賬號/<username>

<password>密碼/<password>
/<server>

四、修改項目版本

用如下命令,修改項目的版本,例如1.0.1-RELEASE 。

mvn versions:set -DnewVersion=1.0.1-RELEASE

當然也可手動修改版本,不過當項目比較複雜,module比較多時,手動修改就會比較麻煩,而且容易出錯。建議用命令修改。

五、發佈

執行如下命令即可將依賴發佈到中央倉庫。

mvn clean install deploy -P release

不出意外,構建會報xxx服務器無法找到GPG的異常。原因是前文生成的秘鑰尚未發佈到key server。keyserver的地址會在異常中打印出來。我的項目報的是 http://keys.gnupg.net:11371/ 。於是執行

gpg --keyserver http://keys.gnupg.net:11371/ --send-keys [xxxxxxxxx]
其中的[xxxxxxxxx],可用gpg --list-keys顯示出來。

然後再次執行如下命令:

mvn clean install deploy -P release

此時即可發佈成功。發佈完使用如下命令重置為SNAPSHOT版本

mvn versions:set -DnewVersion=1.0.2-SNAPHOST

六、遇到的問題

6.1 執行 gpg --gen-key 報 Key generation failed: Timeout 的異常

解決方案:

rm -rf ~/.gnupg
gpg --gen-key

6.2 執行mvn clean install deploy -P release 時,報gpg: signing failed: Inappropriate ioctl for device

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

解決方案:

export GPG_TTY=$(tty)
mvn clean install deploy -P release

6.3 連不上 https://oss.sonatype.org

•科學上網(在某些城市有被查水錶、罰款的風險),自己找梯子吧;

•飛到香港、澳門或者海外等能沒有牆的地方,然後發佈應用,發佈完再回國(一種人傻錢多的方式);

•移民(更徹底的解決方案,但如果想看抗日神劇或者聽某些國內音樂,可能要用梯子翻回來)……

TIPS

如果你在發佈時遇到其他問題,也可添加我的微信 jumping_me ,我儘量幫助到你。

乾貨分享

最近將個人學習筆記整理成冊,使用PDF分享。關注我,回覆如下代碼,即可獲得百度盤地址,無套路領取!

•001:《Java併發與高併發解決方案》學習筆記;

•002:《深入JVM內核——原理、診斷與優化》學習筆記;

•003:《Java面試寶典》

•004:《Docker開源書》

•005:《Kubernetes開源書》

•006:《DDD速成(領域驅動設計速成)》

References

[1] light-security: https://www.github.com/eacdy/light-security

[2] Sonartype: https://issues.sonatype.org/

[3] Sonartype Dashboard: https://issues.sonatype.org/secure/Dashboard.jspa

[4] OSSRH-47914: https://issues.sonatype.org/browse/OSSRH-47914

[5] Light Security Spring Boot Starter Pom.xml: https://github.com/eacdy/light-security/blob/master/light-security-spring-boot-starter/pom.xml


分享到:


相關文章: