利用github搭建個人maven倉庫,githubmaven
原文出處: hengyunabc
緣起
之前看到有開源項目用了github來做maven倉庫,尋思自己也做一個。研究了下,記錄下。
簡單來說,共有三步:
配置local file maven倉庫deploy到本地
maven可以通過http, ftp, ssh等deploy到遠程伺服器,也可以deploy到本地檔案系統裡。
例如把項目deploy到/home/hengyunabc/code/maven-repo/repository/目錄下:
| 123456 |
<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上。
| 123456 |
cd /home/hengyunabc/code/maven-repo/git initgit add repository/*git commit -m 'deploy xxx'git remote add origin git@github.com:hengyunabc/maven-repo.gitgit push origin master |
最終效果可以參考我的個人倉庫:
https://github.com/hengyunabc/maven-repo
github maven倉庫的使用
因為github使用了raw.githubusercontent.com這個網域名稱用於raw檔案下載。所以使用這個maven倉庫,只要在pom.xml裡增加:
| 123456 |
<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
| 123 |
maven-metadata.xmlmaven-metadata.xml.md5maven-metadata.xml.sha1 |
maven-metadata.xml裡面記錄了最後deploy的版本和時間。
| 1234567891011121314 |
<?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裡配置,如:
| 123456 |
<repositories> <repository> <id>hengyunabc-maven-repo</id> <url>file:/home/hengyunabc/code/maven-repo/repository/</url> </repository> </repositories> |
注意事項
maven的repository並沒有優先順序的配置,也不能單獨為某些依賴配置repository。所以如果項目配置了多個repository,在首次編繹時,會依次嘗試下載依賴。如果沒有找到,嘗試下一個,整個流程會很長。
所以盡量多個依賴放同一個倉庫,不要每個項目都有一個自己的倉庫。QQ技術交流群290551701