原文出處: hengyunabc
緣起
之前看到有開源項目用了github來做maven倉庫,尋思自己也做一個。研究了下,記錄下。
簡單來說,共有三步:
deploy到本地目錄
把本地目錄提交到gtihub上
配置github地址爲倉庫地址
配置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
:
瀏覽器地址是:
它的maven倉庫url是:
maven倉庫工作的機制
下面介紹一些maven倉庫工作的原理。典型的一個maven依賴下會有這三個文件:
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,在首次編繹時,會依次嘗試下載依賴。如果沒有找到,嘗試下一個,整個流程會很長。
所以儘量多個依賴放同一個倉庫,不要每個項目都有一個自己的倉庫。
QQ技術交流羣290551701