利用github搭建个人maven仓库

ygwang2010 8年前
   <h2>缘起</h2>    <p>之前看到有开源项目用了github来做maven仓库,寻思自己也做一个。研究了下,记录下。</p>    <p>简单来说,共有三步:</p>    <ol>     <li>deploy到本地目录</li>     <li>把本地目录提交到gtihub上</li>     <li>配置github地址为仓库地址</li>    </ol>    <h2>配置local file maven仓库</h2>    <h3>deploy到本地</h3>    <p>maven可以通过http, ftp, ssh等deploy到远程服务器,也可以deploy到本地文件系统里。</p>    <p>例如把项目deploy到 /home/hengyunabc/code/maven-repo/repository/ 目录下:</p>    <pre>  <distributionManagement>      <repository>        <id>hengyunabc-mvn-repo</id>        <url>file:/home/hengyunabc/code/maven-repo/repository/</url>      </repository>    </distributionManagement></pre>    <p>通过命令行则是:</p>    <pre>  mvn deploy -DaltDeploymentRepository=hengyunabc-mvn-repo::default::file:/home/hengyunabc/code/maven-repo/repository/</pre>    <p>推荐使用命令行来deploy,避免在项目里显式配置。</p>    <p><a href="/misc/goto?guid=4959670520455279577" rel="nofollow,noindex">https://maven.apache.org/plugins/maven-deploy-plugin/</a></p>    <p><a href="/misc/goto?guid=4959670520574301702" rel="nofollow,noindex">https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html</a></p>    <h2>把本地仓库提交到github上</h2>    <p>上面把项目deploy到本地目录 home/hengyunabc/code/maven-repo/repository 里,下面把这个目录提交到github上。</p>    <p>在Github上新建一个项目,然后把 home/hengyunabc/code/maven-repo 下的文件都提交到gtihub上。</p>    <pre>  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</pre>    <p>最终效果可以参考我的个人仓库:</p>    <p><a href="/misc/goto?guid=4959670520677877703" rel="nofollow,noindex">https://github.com/hengyunabc/maven-repo</a></p>    <h2>github maven仓库的使用</h2>    <p>因为github使用了 raw.githubusercontent.com 这个域名用于raw文件下载。所以使用这个maven仓库,只要在pom.xml里增加:</p>    <pre>  <repositories>          <repository>              <id>hengyunabc-maven-repo</id>              <url>https://raw.githubusercontent.com/hengyunabc/maven-repo/master/repository</url>          </repository>      </repositories></pre>    <h3>目录查看和搜索</h3>    <p>值得注意的是,github因为安全原因,把raw文件下载和原来的github域名分开了,而 raw.githubusercontent.com 这个域名是不支持目录浏览的。所以,想要浏览文件目录,或者搜索的话,可以直接到github域名下的仓库去查看。</p>    <p>比如这个文件 mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar :</p>    <p>浏览器地址是:</p>    <p><a href="/misc/goto?guid=4959670520776964315" rel="nofollow,noindex">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</a></p>    <p>它的maven仓库url是:</p>    <p><a href="/misc/goto?guid=4959670520878791169" rel="nofollow,noindex">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</a></p>    <h2>maven仓库工作的机制</h2>    <p>下面介绍一些maven仓库工作的原理。典型的一个maven依赖下会有这三个文件:</p>    <p><a href="/misc/goto?guid=4959670520967815729" rel="nofollow,noindex">https://github.com/hengyunabc/maven-repo/tree/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT</a></p>    <pre>  maven-metadata.xml  maven-metadata.xml.md5  maven-metadata.xml.sha1</pre>    <p>maven-metadata.xml 里面记录了最后deploy的版本和时间。</p>    <pre>  <?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>    <p>其中md5, sha1校验文件是用来保证这个meta文件的完整性。</p>    <p>maven在编绎项目时,会先尝试请求 maven-metadata.xml ,如果没有找到,则会直接尝试请求到jar文件,在下载jar文件时也会尝试下载jar的md5, sha1文件。</p>    <p>maven-metadata.xml 文件很重要,如果没有这个文件来指明最新的jar版本,那么即使远程仓库里的jar更新了版本,本地maven编绎时用上-U参数,也不会拉取到最新的jar!</p>    <p>所以并不能简单地把jar包放到github上就完事了,一定要先在本地Deploy,生成 maven-metadata.xml 文件,并上传到github上。</p>    <p>参考: <a href="/misc/goto?guid=4959670521074917541" rel="nofollow,noindex">http://maven.apache.org/ref/3.2.2/maven-repository-metadata/repository-metadata.html</a></p>    <h2>maven的仓库关系</h2>    <p><a href="/misc/goto?guid=4959670521172086934" rel="nofollow,noindex">https://maven.apache.org/repository/index.html</a></p>    <p><img src="https://simg.open-open.com/show/adc12a66f0f8a87a0ac3c02817fcda74.png"></p>    <h2>配置使用本地仓库</h2>    <p>想要使用本地file仓库里,在项目的pom.xml里配置,如:</p>    <pre>  <repositories>          <repository>              <id>hengyunabc-maven-repo</id>              <url>file:/home/hengyunabc/code/maven-repo/repository/</url>          </repository>      </repositories></pre>    <h2>注意事项</h2>    <p>maven的repository并没有优先级的配置,也不能单独为某些依赖配置repository。所以如果项目配置了多个repository,在首次编绎时,会依次尝试下载依赖。如果没有找到,尝试下一个,整个流程会很长。</p>    <p>所以尽量多个依赖放同一个仓库,不要每个项目都有一个自己的仓库。</p>    <p><a href="/misc/goto?guid=4959670521260833796" rel="nofollow,noindex">http://stackoverflow.com/questions/14013644/hosting-a-maven-repository-on-github/14013645#14013645</a></p>    <p><a href="/misc/goto?guid=4959670521360398646" rel="nofollow,noindex">http://cemerick.com/2010/08/24/hosting-maven-repos-on-github/</a></p>    <p>来源:http://www.importnew.com/19178.html</p>