溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用github搭建個人maven倉庫的方法步驟

發(fā)布時間:2020-10-06 12:26:47 來源:腳本之家 閱讀:220 作者:橫云斷嶺 欄目:編程語言

緣起

之前看到有開源項目用了github來做maven倉庫,尋思自己也做一個。研究了下,記錄下。

簡單來說,共有三步:

  • deploy到本地目錄
  • 把本地目錄提交到gtihub上
  • 配置github地址為倉庫地址

配置local file maven倉庫

deploy到本地

maven可以通過http, ftp, ssh等deploy到遠程服務器,也可以deploy到本地文件系統(tǒng)里。

例如把項目deploy到/home/hengyunabc/code/maven-repo/repository/目錄下:

 <distributionManagement>
 <repository>
  <id>hengyunabc-mvn-repo</id>
  <url>file:/home/hengyunabc/code/maven-repo/repository/</url>
 </repository>
 </distributionManagement>

通過命令行則是:

復制代碼 代碼如下:
mvn deploy -DaltDeploymentRepository=hengyunabc-mvn-repo::default::file:/home/hengyunabc/code/maven-repo/repository/

推薦使用命令行來deploy,避免在項目里顯式配置。

https://maven.apache.org/plugins/maven-deploy-plugin/

https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html

把本地倉庫提交到github上

上面把項目deploy到本地目錄home/hengyunabc/code/maven-repo/repository里,下面把這個目錄提交到github上。

在Github上新建一個項目,然后把home/hengyunabc/code/maven-repo下的文件都提交到gtihub上。

cd /home/hengyunabc/code/maven-repo/
git init
git add repository/*
git commit -m 'deploy xxx'
git remote add origin git@github.com:hengyunabc/maven-repo.git
git push origin master

最終效果可以參考我的個人倉庫:

https://github.com/hengyunabc/maven-repo

github maven倉庫的使用

因為github使用了raw.githubusercontent.com這個域名用于raw文件下載。所以使用這個maven倉庫,只要在pom.xml里增加:

 <repositories>
  <repository>
   <id>hengyunabc-maven-repo</id>
   <url>https://raw.githubusercontent.com/hengyunabc/maven-repo/master/repository</url>
  </repository>
 </repositories>

目錄查看和搜索

值得注意的是,github因為安全原因,把raw文件下載和原來的github域名分開了,而raw.githubusercontent.com這個域名是不支持目錄瀏覽的。所以,想要瀏覽文件目錄,或者搜索的話,可以直接到github域名下的倉庫去查看。

比如這個文件mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar:

瀏覽器地址是:

https://github.com/hengyunabc/maven-repo/blob/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT/mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar

它的maven倉庫url是:

https://raw.githubusercontent.com/hengyunabc/maven-repo/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT/mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar

maven倉庫工作的機制

下面介紹一些maven倉庫工作的原理。典型的一個maven依賴下會有這三個文件:

https://github.com/hengyunabc/maven-repo/tree/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT

maven-metadata.xml
maven-metadata.xml.md5
maven-metadata.xml.sha1

maven-metadata.xml里面記錄了最后deploy的版本和時間。

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
 <groupId>io.github.hengyunabc</groupId>
 <artifactId>mybatis-ehcache-spring</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <versioning>
 <snapshot>
  <timestamp>20150804.095005</timestamp>
  <buildNumber>1</buildNumber>
 </snapshot>
 <lastUpdated>20150804095005</lastUpdated>

 </versioning>
</metadata>

其中md5, sha1校驗文件是用來保證這個meta文件的完整性。

maven在編繹項目時,會先嘗試請求maven-metadata.xml,如果沒有找到,則會直接嘗試請求到jar文件,在下載jar文件時也會嘗試下載jar的md5, sha1文件。

maven-metadata.xml文件很重要,如果沒有這個文件來指明最新的jar版本,那么即使遠程倉庫里的jar更新了版本,本地maven編繹時用上-U參數(shù),也不會拉取到最新的jar!

所以并不能簡單地把jar包放到github上就完事了,一定要先在本地Deploy,生成maven-metadata.xml文件,并上傳到github上。

參考:http://maven.apache.org/ref/3.2.2/maven-repository-metadata/repository-metadata.html

maven的倉庫關系

https://maven.apache.org/repository/index.html

利用github搭建個人maven倉庫的方法步驟

配置使用本地倉庫

想要使用本地file倉庫里,在項目的pom.xml里配置,如:

 <repositories>
  <repository>
   <id>hengyunabc-maven-repo</id>
   <url>file:/home/hengyunabc/code/maven-repo/repository/</url>
  </repository>
 </repositories>

注意事項

maven的repository并沒有優(yōu)先級的配置,也不能單獨為某些依賴配置repository。所以如果項目配置了多個repository,在首次編繹時,會依次嘗試下載依賴。如果沒有找到,嘗試下一個,整個流程會很長。

所以盡量多個依賴放同一個倉庫,不要每個項目都有一個自己的倉庫。

參考
http://stackoverflow.com/questions/14013644/hosting-a-maven-repository-on-github/14013645#14013645
http://cemerick.com/2010/08/24/hosting-maven-repos-on-github/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI