利用github搭建个人maven仓库
緣起
之前看到有開源項目用了github來做maven倉庫,尋思自己也做一個。研究了下,記錄下。
簡單來說,共有三步:
配置local file maven倉庫
deploy到本地
maven可以通過http, ftp, ssh等deploy到遠程服務器,也可以deploy到本地文件系統里。
例如把項目deploy到/home/hengyunabc/code/maven-repo/repository/目錄下:
| 1 2 3 4 5 6 | <distributionManagement> ??<repository> ????<id>hengyunabc-mvn-repo</id> ????<url>file:/home/hengyunabc/code/maven-repo/repository/</url> ??</repository> </distributionManagement> |
通過命令行則是:
| 1 | 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上。
| 1 2 3 4 5 6 | 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里增加:
| 1 2 3 4 5 6 | <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
| 1 2 3 | maven-metadata.xml maven-metadata.xml.md5 maven-metadata.xml.sha1 |
maven-metadata.xml里面記錄了最后deploy的版本和時間。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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參數,也不會拉取到最新的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里配置,如:
| 1 2 3 4 5 6 | <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/
原文出處:?hengyunabc
from:?http://www.importnew.com/22992.html
總結
以上是生活随笔為你收集整理的利用github搭建个人maven仓库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给Eclipse提速的7个技巧
- 下一篇: Maven实战:pom.xml与sett