阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

利用GitHub搭建个人Maven仓库

161次阅读
没有评论

共计 4022 个字符,预计需要花费 11 分钟才能阅读完成。

缘起

之前看到有开源项目用了 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>

其中 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

利用 GitHub 搭建个人 Maven 仓库

配置使用本地仓库

想要使用本地 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

GitHub 教程系列文章

通过 GitHub 创建个人技术博客图文详解  http://www.linuxidc.com/Linux/2015-02/114121.htm

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

GitHub 的详细介绍:请点这里
GitHub 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130197.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4022字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中