利用github搭建個人maven倉庫

ygwang2010 8年前發布 | 36K 次閱讀 Maven 項目構建 Github

緣起

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

簡單來說,共有三步:

  1. deploy到本地目錄
  2. 把本地目錄提交到gtihub上
  3. 配置github地址為倉庫地址

配置local file maven倉庫

deploy到本地

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

例如把項目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></pre>

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

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

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

配置使用本地倉庫

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

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

注意事項

maven的repository并沒有優先級的配置,也不能單獨為某些依賴配置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/

來源:http://www.importnew.com/19178.html

 本文由用戶 ygwang2010 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!