如何把JAR發(fā)布到maven中央倉庫的幾種方法
詳細(xì)描述maven中央倉庫發(fā)布jar包的中間過程, 以及遇到的一些問題匯總, 盡量用文字描述清楚, 耐心看下去, 就一定會發(fā)布成功
----Sonatype篇----
名詞解釋:
Sonatype Nexus: Sonatype Nexus helps software development teams use open source so they can innovate faster and automatically control risk
maven社區(qū)唯一指定的倉庫地址為: https://search.maven.org/ 所以, 我們現(xiàn)在用的遠(yuǎn)程倉庫地址, 無論是哪個, 都是需要去這個倉庫同步index的, 然后在把依賴同步到自己的倉庫下面
整個maven倉庫網(wǎng)絡(luò)是一個拓?fù)湫图軜?gòu), 倉庫與倉庫之間可以互相依賴, 且互相索引
如果想上傳jar到公服倉庫, 那么首先要打通的就是sonatype, 我們首先去sonatype官網(wǎng)注冊個賬號, 訪問https://issues.sonatype.org/secure/Dashboard.jspa這個地址是sonatype
控制臺, 如果未登錄, 則會先跳轉(zhuǎn)到登錄界面, 點擊注冊, 填寫相關(guān)信息, 注冊個賬號然后登錄
1. 進入控制臺之后, 點擊新建按鈕, 新建一個問題(issue)
project 選擇community support -open source.... issue 選擇new project summary 填寫一些項目簡短描述 description 填寫項目的描述 group id 這個最重要了, 要與你的工程pom文件里的頂級group id 一致, 而且這個是不能亂填的, 一般來說, 是你自己的域名反寫 Project URL 進入到你項目的url, 假如你的項目在github上, 先進到你的項目中, 然后復(fù)制地址欄上的地址填進去就可以 SCM url 同上, 但是這個url要填寫你的git資源下載地址, 就是項目右側(cè)的clone&download那個按鈕彈出來的地址 Username(s) 用戶名 Already Synced to Central 默認(rèn)選NO就可以, 因為暫時我們還不需要同步到中央倉庫
2. 等待ISSUE審批, 一般來說, 2分鐘左右, 你就會收到審批結(jié)果(同時會給你的郵箱發(fā)郵件), 告訴你, 要驗證你的域名, 也就是你的
group id對應(yīng)的域名, 會通過以下幾種方式驗證這個域名是不是你的 Central OSSRH updated OSSRH-xxxxx: ---------------------------------- Status: Waiting for Response (was: Open) Do you own the domain gomyck.com? If so, please verify ownership via one of the following methods: * Add a TXT record to your DNS referencing this issue (Fastest) * Setup a redirect to your Github page (if it does not already exist) * Send an email to central@sonatype.com referencing this issue from a gomyck.com email address If you do not own this domain, please read: http://central.sonatype.org/pages/choosing-your-coordinates.html You may also choose a groupId that reflects your project hosting, in this case, something like
這個里面告訴你, 最快的辦法就是在你的域名解析中, 添加一條text記錄, 我當(dāng)時就是用的這種方式, 具體操作如下:
- 進入到域名商網(wǎng)站, 登錄之后選擇域名解析
- 點擊添加記錄, 記錄類型選擇text
- 主機記錄不要寫(默認(rèn)是@)
- 記錄值寫你的問題編號
- 其他都不需要改, 點擊確定
如果填的沒問題的話, 大概10分鐘左右, 你就會收到審核通過的消息, 告訴你可以上傳資源了
com.gomyck has been prepared, now user(s) gomyck,mzxc can: Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2 Promote staged artifacts into repository 'Releases' Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging please comment on this ticket when you promoted your first release, thanks
他告訴你, 如果你提交了版本, 最好告訴他一下, 不用管, 以后我們也不會理他的
----GPG篇----
使用 GPG 生成密鑰對
Windows下載 Gpg4win 軟件來生成密鑰對, 地址:https://www.gpg4win.org/download.html
我用的mac, 使用brew安裝的gpg brew install gnupg
使用以下命令來生成秘鑰對:
$ gpg --gen-key #按照提示輸入信息, 在輸入密碼的時候, 如果嫌麻煩就直接摁回車就可以, 這樣秘鑰就沒有密碼保護了, 密碼保護只有在你的私鑰泄露的時候才有用, 其他時候沒用 $ gpg --list-keys #這個指令會顯示你的秘鑰環(huán), 類似于下面這樣 #--------------------------------- # pub rsa4096 2018-09-25 [SC] # EABB59A7BFXXXXXX46604F95ED1503AA8CDxxxx (這個才是秘鑰ID) # uid [ 絕對 ] xxx (zhushi) <xxx@163.com> # sub rsa4096 2018-09-25 [E] $ gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 秘鑰ID #發(fā)送你的公鑰到秘鑰倉庫, 以后你的jar會使用私鑰簽名, maven中央倉庫會去幾個指定的秘鑰倉庫去找公鑰來驗證這個簽名, 如果不上傳是不能通過審核的 $ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 秘鑰ID #驗證你的公鑰是否上傳成功, 如果返回結(jié)果是 **未改變, 那就是成功了
----maven篇----
在你的maven頂級工程中加入以下配置
<groupId>com.gomyck</groupId>
<artifactId>gomyck-quickdev</artifactId>
<packaging>pom</packaging>
<version>1.1.0-SNAPSHOT</version>
<name>gomyck-quickdev</name>
<url>http://www.gomyck.com</url>
<description>gomyck 快速開發(fā)平臺</description>
<developers>
<developer>
<id>gomyck</id>
<name>haoy</name>
<url>www.gomyck.com</url>
<email>hao474798383@163.com</email>
</developer>
</developers>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<scm>
<connection>scm:git:git@github.com:mzxc/gomyck-fastdfs-spring-boot-starter.git</connection>
<developerConnection>scm:git:git@github.com:mzxc/gomyck-fastdfs-spring-boot-starter.git</developerConnection>
<url>git@github.com:mzxc/gomyck-fastdfs-spring-boot-starter.git</url>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 你的秘鑰ID -->
<keyname>${gpg.keyname}</keyname>
<passphraseServerId>${gpg.keyname}</passphraseServerId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>sonatype-nexus-staging</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
在你的settings.xml中加入以下配置
<server>
<id>sonatype-nexus-snapshots</id>
<username>sonatype賬號</username>
<password>sonatype密碼</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>sonatype賬號</username>
<password>sonatype密碼</password>
</server>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.keyname>你的秘鑰 ID</gpg.keyname>
</properties>
</profile>
</profiles>
運行deploy, 如果按照我的步驟一步一步走, 應(yīng)該沒有錯誤(如果報錯也是你的doc問題, 按照提示一步一步改好, 否則maven審核會失敗)
----nexus篇----
如果上一步?jīng)]有錯誤的話, 訪問網(wǎng)址: https://oss.sonatype.org/#stagingRepositories
點擊右上角登錄, 賬號密碼就是sonatype的賬號密碼, 登錄進去之后, 點擊Staging repositories
在右側(cè)搜索框輸入你的group id, 然后點擊refresh, 就會看到你的提交信息
選中, 點擊close, 這時當(dāng)前的紀(jì)錄就會變成一個小齒輪, 表示nexus在校驗?zāi)愕膉ar
按照劇本, 你的提交應(yīng)該會全部通過(圖標(biāo)顯示數(shù)字就是失敗, 反之則是成功)
再次選中當(dāng)前記錄, 點擊release, 就會上傳成功了, 這時sonatype會給你發(fā)郵件
以后你只需要按照maven篇deploy, 然后在使用nexus篇提交release就可以了
注意: 如果是snapshot版本, 則不需要審核, 直接deploy就可以直接引用, 但是在maven倉庫(https://search.maven.org/)是搜索不到的
----爬坑篇----
1.一開始總是提示文件驗證簽名失敗, 提示我說沒有在秘鑰倉庫找到對應(yīng)的公鑰, 但是我本地可以確定的是提交了且ID一致, 后來過了大概1小時, 驗證忽然就過了,
這期間, 我把nexus提示的秘鑰倉庫地址都復(fù)制出來, 挨個上傳我的公鑰, 最后不知道是哪個倉庫生效了, 但是我覺得是倉庫延遲問題
2.以后寫代碼一定要把doc寫好, 不然遇見這種場景, 簡直就是折磨, 尤其doc多的時候
3.如果你有多個gpg秘鑰, 一定要指定秘鑰ID, 不然gpg插件是秘鑰環(huán)的順序來對你的工程簽名的, 這會導(dǎo)致你上傳的公鑰不一定對應(yīng)的上簽名的私鑰, 秘鑰的ID請在settings.xml中配置profile, gpg插件的配置請到apache-gpg官網(wǎng)看
4.sonatype在國內(nèi)環(huán)境下, 訪問非常困難, 建議找個科學(xué)上網(wǎng)的方式, 來實踐本教程
到此這篇關(guān)于如何把JAR發(fā)布到maven中央倉庫的幾種方法的文章就介紹到這了,更多相關(guān)JAR發(fā)布到maven中央倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea2019.2安裝MybatisCodeHelper插件的超詳細(xì)教程
這篇文章主要介紹了idea2019.2安裝MybatisCodeHelper插件的教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式
這篇文章主要介紹了Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
RabbitMQ的ACK確認(rèn)機制保障消費端消息的可靠性詳解
這篇文章主要介紹了RabbitMQ的ACK確認(rèn)機制保障消費端消息的可靠性詳解,簡單來說,就是你必須關(guān)閉 RabbitMQ 的自動ack ,可以通過一個 api 來調(diào)用就行,然后每次你自己代碼里確保處理完的時候,再在程序里 ack 一把,需要的朋友可以參考下2023-12-12
使用IDEA啟動項目遇見ClassNotFoundException的解決方案
這篇文章主要介紹了使用IDEA啟動項目遇見ClassNotFoundException的正確解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Springboot之restTemplate的配置及使用方式
這篇文章主要介紹了Springboot之restTemplate的配置及使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

