之前的文章介紹瞭如何編寫一個Maven插件。那插件的使用上呢?在本地,我們可以安裝到本地倉庫,使用沒問題;在SIT環境中,我們可以安裝到公司內部的私有倉庫中。但如果公司的Maven包尚未形成規模,不需要搭建私有倉庫,且私有倉庫需要有專人進行維護,如果只有少量包,可能過一段時間都忘了還有這麼個東西。等到出問題了找半天,又是一個麻煩事。
上傳到Maven中央庫也許是一個非常好的選擇,任何人在任何地方都可以使用該包,爽歪歪。
爲了方便,使用OSSRH的發佈方案。
步驟介紹
整個步驟大致有如下三步
- 使用JIRA申請創建一個新的Repository,用於在OSSRH申請空間。
- 向項目的pom.xml中增加必要的內容
- 部署
其中第一步需要人工審覈,時間耗費比較久,按照個人經驗來說半天可以搞定。
說明:如下步驟是OSSRH Guide的實踐。
倉庫及空間申請
創建JIRA賬號
登錄Snaptype JIRA
收到郵件代表創建成功。
新建工單
再次登錄Snaptype JIRA
依次填入概要、Group Id、Project URL(項目URL,可填github項目主頁)、SCM url(項目版本控制鏈接)
工單創建成功後需要等待郵件確認,反覆幾個來回後才能正常發佈。下圖是截止申請成功的所有交流。可以看到會有如下幾個步驟
- 創建工單
- 在自己的github主頁下創建一個它要求的repository以完成驗證
- 驗證完成,允許用戶發佈組件到倉庫
- 申請的Group Id激活,用戶發佈的組件能夠被同步到中央倉庫(但要花兩個小時)
GPG簽名準備
組件發佈之前,需要進行GPG簽名,發佈到服務器後,服務器會使用公鑰對組件進行校驗。關於GPG的介紹和詳細解讀,大家可以參考這篇文章。這裏涉及到幾個點
-
安裝gnupg工具
# 安裝 $ sudo apt install gnupg # 安裝成功與否的驗證 $ gpg --version gpg (GnuPG) 2.2.4 libgcrypt 1.8.1 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Home: /home/floyd/.gnupg 支持的算法: 公鑰: RSA, ELG, DSA, ECDH, ECDSA, EDDSA 密文: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256 散列: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 壓縮: 不壓縮, ZIP, ZLIB, BZIP2
-
生成密鑰對
# 執行如下命令,提示填入真實姓名和郵箱,並設置密碼 $ gpg --gen-key # 查看剛纔生成的密鑰 $ gpg --list-key /home/floyd/.gnupg/pubring.kbx ------------------------------ pub rsa3072 2020-02-29 [SC] [有效至:2022-02-28] EB3877C670A794E0238E66E1D4420641AADB285E uid [ 絕對 ] Zou Guodong <[email protected]> sub rsa3072 2020-02-29 [E] [有效至:2022-02-28]
上面生成的祕鑰,其中EB3877C670A794E0238E66E1D4420641AADB285E是用戶ID,記住,下一步會使用
-
發送公鑰到 key server
這一步非常重要,否則OSSRH會因爲無法驗證發佈的組件而導致發佈失敗。
gpg --send-key EB3877C670A794E0238E66E1D4420641AADB285E
如果發送成功,你可能會收到opengpg發送的郵件
但這一步很可能會遇到問題,這在後面的問題集中有介紹 -
在pom.xml中配置插件(在POM修改中有對應說明)
-
在setting.xml中配置要使用的gpg工具 (在setting.xml修改中有對應說明)
pom.xml修改
添加POM必備項
-
項目基礎信息
<groupId>com.example.applications</groupId> <artifactId>example-application</artifactId> <version>1.4.7</version> <name>${project.groupId}:${project.artifactId}</name> <description>A application used as an example on how to set up pushing its components to the Central Repository.</description> <url>http://www.example.com/example-application</url>
-
證書
<licenses> <license> <name>The Apache License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license> </licenses>
-
開發者信息
<developers> <developer> <name>Manfred Moser</name> <email>[email protected]</email> <organization>Sonatype</organization> <organizationUrl>http://www.sonatype.com</organizationUrl> </developer> </developers>
-
SCM信息
即版本控制信息
<scm> <connection>scm:git:git://github.com/simpligility/ossrh-demo.git</connection> <developerConnection>scm:git:ssh://github.com:simpligility/ossrh-demo.git</developerConnection> <url>http://github.com/simpligility/ossrh-demo/tree/master</url> </scm>
添加發布相關插件
-
Javadoc 和 源碼打包插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <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> <id>attach-javadocs</id> <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> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin>
-
Nexu暫存插件
<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>
Setting.xml修改
JIRA賬號
設置準備階段設置的賬號密碼,以便mvn能夠連接到服務器
<settings>
<servers>
<server>
<id>ossrh</id>
<username>your-jira-id</username>
<password>your-jira-pwd</password>
</server>
</servers>
</settings>
GPG
添加如下配置,指定加密工具
<settings>
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>the_pass_phrase</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
我的配置
作爲參考,給出我的配置,看這裏
發佈
上述內容準備好後,可以執行如下命令進行發佈。
mvn clean deploy
需要注意的是,如果我們發佈的版本是release版本,即版本號不以-SNAPSHOT結尾,且剛纔配置的Nexus暫存插件的autoReleaseAfterClose
設置爲true時,執行上述命令後,OSSRH會自動發送到中央庫。
至此,發佈完成。更多步驟請參考官方文檔。官方文檔中提供了完整的發佈開發版、release版的方式,可供參考。
效果
執行成功,等待大約兩個小時,你就能在中央庫查詢到自己的組件了。例如搜索我剛纔發佈的組件。
點擊進入,可以查看詳情,這裏
而作爲一個插件,可以通過如下方式使用它
<plugin>
<groupId>com.github.zou8944</groupId>
<artifactId>json-loader-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-json-to-properties</goal>
</goals>
<configuration>
<files>
<file>hello.json</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
踩過的坑
-
在snatype上無法找到對應的profile
該問題是由於pom的GroupId和第一步申請JIRA賬號時指定的GroupId不一致導致,修改本地使得一致即可 -
組件上傳成功,但服務端簽名校驗失敗
該問題在於沒有上傳本地生成的密鑰對。執行gpg --send-key <ID>
生成即可。上傳後,收到opengpg發來的郵件才能確認上傳成功。 -
pgp上傳祕鑰失敗
前後報過兩個錯,分別是上傳失敗和服務器拒絕連接。網上找了很多解決方案對我都無效。最終使用如下終極方法
# 刪除整個.gnupg目錄 sudo rm -r ~./gnupg # 重新生成祕鑰,不能加sudo gpg --gen-key # 執行如下命令,這是解決服務器拒絕連接的關鍵 pkill dirmngr # 再次發送 gpg --send-key <ID>
總結
本文主要以實踐爲主,介紹瞭如果將本地項目發佈到Maven中央庫。儘管沒有什麼原創的內容,但價值在於踩坑。