作為 Java 開發(fā)者(偽),工作中一定離不開 Maven。
偶爾也需要發(fā)布自己的構(gòu)件到 Maven 中央倉庫中(https://oss.sonatype.org/)。
但是經(jīng)常有這樣那樣的坑(因為總是換電腦 XD),在這里記錄一下,以備后續(xù)查閱。
上傳步驟
將項目發(fā)布到 maven 中央倉庫的一般步驟如下:
注冊Sonatype的賬戶。地址:https://issues.sonatype.org/secure/Signup!default.jspa
-
提交發(fā)布申請。(僅第一次)
- 創(chuàng)建 Issue:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
- 項目類型是
Community Support - Open Source Project Repository Hosting -
groupId對應(yīng)的域名你需要有所有權(quán)
-
使用 GPG 生成密鑰對。
Windows 安裝:http://gpg4win.org/
Mac 安裝:
brew install gpg-
gpg --version檢查是否安裝成功 -
gpg --gen-key生成密鑰對 -
gpg --list-keys查看公鑰 -
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公鑰ID將公鑰發(fā)布到 PGP 密鑰服務(wù)器 -
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公鑰ID查詢公鑰是否發(fā)布成功
-
-
配置 maven。
需要修改的 Maven 配置文件包括:setting.xml(全局級別)與pom.xml(項目級別)。-
setting.xml配置一覽<settings> ... <servers> <server> <id>snapshotRepository-id</id> <username>用戶名</username> <password>密碼</password> </server> </servers> ... </settings>使用自己注冊的 Sonatype 賬號的用戶名與密碼來配置以上 server 信息。
此處 id
snapshotRepository-id應(yīng)和下面pom.xml中 snapshotRepository 和 repository 里面的 id 保持一致。-
pom.xml配置一覽
<project> ... <name>your project's name</name> <description>your project's description</description> <url>http://www.chengww.com/</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>chengww</name> <email>chengww5217@163.com</email> </developer> </developers> ... <profiles> <profile> <id>release</id> <build> <plugins> <!-- Source --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <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> <version>2.9.1</version> <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> <version>1.5</version> <executions> <execution> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> <snapshotRepository> <id>snapshotRepository-id</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </snapshotRepository> <repository> <id>snapshotRepository-id</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> </profile> </profiles> ... </project>注意:以上
pom.xml必須包括:name、description、url、licenses、developers、scm 等基本信息,此外,使用了 Maven 的 profile 功能,只有在 release 的時候,創(chuàng)建源碼包、創(chuàng)建文檔包、使用 GPG 進(jìn)行數(shù)字簽名。此外,snapshotRepository 與 repository 中的 id 一定要與setting.xml中 server 的 id 保持一致。 -
-
上傳構(gòu)件到 OSS 中。
mvn clean deploy -P release在 OSS 中發(fā)布構(gòu)件。進(jìn)入 https://oss.sonatype.org/,點擊
Staging Repositories-> 在搜索欄輸入你的 groupId -> 勾選你的構(gòu)件并點擊 close -> 點擊 tab 欄的 release。通知 Sonatype 的工作人員關(guān)閉 issue。(僅第一次)
參考地址:https://my.oschina.net/huangyong/blog/226738
等待審批通過后,就可以在中央倉庫中搜索到自己發(fā)布的構(gòu)件了!
但是,事情并不是那么簡單的。總是會出現(xiàn)這樣那樣的坑。
碰到的坑一覽
GPG 生成密鑰對
消息提示亂碼
出現(xiàn)該步驟其實是輸入密碼的步驟。但是由于是中文的緣故,消息提示亂碼了。
只需要在下面橫線上輸入密碼之后,將光標(biāo)移動到下面的好,回車即可。注意,密碼需要輸入兩次,請保持兩次一致。
將公鑰發(fā)布到 PGP 密鑰服務(wù)器
gpg: 發(fā)送至公鑰服務(wù)器失?。篠erver indicated a failure
因安裝了新版的 gpg,在 gpg --list-keys 時顯示如下:
pub rsa2048 2019-04-12 [SC] [有效至:2021-04-11]
9A1640F7A2551131612D51B12D83594B7B29D86A
uid [ 絕對 ] chengww <chengww5217@163.com>
sub rsa2048 2019-04-12 [E] [有效至:2021-04-11]
發(fā)布公鑰到服務(wù)器時,填的公鑰 ID 為 9A1640F7A2551131612D51B12D83594B7B29D86A,終端上顯示為:
gpg --keyserver hkp://subkeys.pgp.net --send-keys 9A1640F7A2551131612D51B12D83594B7B29D86A
...
gpg: 正在發(fā)送密鑰 2D83594B7B29D86A 到 hkp://subkeys.pgp.net
gpg: 發(fā)送至公鑰服務(wù)器失?。篠erver indicated a failure
gpg: 發(fā)送至公鑰服務(wù)器失?。篠erver indicated a failure
只需要將公鑰 ID 從 9A1640F7A2551131612D51B12D83594B7B29D86A 修改成 2D83594B7B29D86A 即可:
gpg --keyserver hkp://subkeys.pgp.net --send-keys 2D83594B7B29D86A
配置 maven
全局級別 setting.xml 在哪里配置
settings.xml 文件一般存在于兩個位置:
全局配置: ${M2_HOME}/conf/settings.xml
用戶配置:${user.home}/.m2/settings.xml
如果實在是不清楚的,請自行 mvn -X 查看:
...
[DEBUG] Reading global settings from /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml
[DEBUG] Reading user settings from /Users/chengww/.m2/settings.xml
...
關(guān)于 setting.xml 相關(guān)講解參見:http://www.itdecent.cn/p/110d897a5442
上傳構(gòu)件到 OSS
Maven Sonatype Nexus return 401
401 錯誤,一般都是未在 setting.xml 中設(shè)置用戶名密碼所致(或用戶名密碼不正確)。
參見上述 4.配置 maven 配置下 setting.xml。
gpg: signing failed: Inappropriate ioctl for device
原因是新版 gpg 在當(dāng)前終端無法彈出密碼輸入頁面。
解決:
export GPG_TTY=$(tty)
在當(dāng)前終端中 export(臨時解決)
或者加入到 ~/.bash_profile,然后 source ~/.bash_profile
gpg: signing failed: Screen or window too small
執(zhí)行上述命令后在 IntelliJ IDEA 中的終端(Terminal)中還是不能彈出密碼輸入界面,且報上面的錯。
這個時候就要到系統(tǒng)的的終端,cd 到項目目錄,然后執(zhí)行 mvn clean deploy -P release
結(jié)束語
路漫漫其修遠(yuǎn)兮,愿天下沒有 BUG。