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

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/

利用github搭建个人maven仓库的更多相关文章

  1. 利用github搭建私人maven仓库

    一.背景 最近在做HBase的项目,不免会引用到一些工具类,如StringUtils,NumberUtils,DateUtils这些,公司底层有封装好可以直接使用. 但是项目完成,用maven打包部署 ...

  2. 使用Nexus搭建企业maven仓库(二)

    先阅读<使用Nexus搭建企业maven仓库(一)> http://blog.csdn.net/ouyida3/article/details/40747545 1.官网眼下最新的版本号是 ...

  3. Android业务组件化之Gradle和Sonatype Nexus搭建私有maven仓库

    前言: 公司的业务组件化推进的已经差不多三四个月的时间了,各个业务组件之间的解耦工作已经基本完成,各个业务组件以module的形式存在项目中,然后项目依赖本地的module,多少有点不太利于项目的并行 ...

  4. 【保姆级】利用Github搭建自己的个人博客,看完就会

    大家好,我是辰哥~ 作为一名喜欢技术的爱好者,平时喜欢把自己学习技术的心得或者一些踩坑.易错的过程记录下来,首选的是技术平台(博客),今天辰哥来教大家如何利用Github来搭建一个自己的个人博客平台. ...

  5. 利用gitHub搭建博客

    ##1.gitHub Page的的使用我觉得这边博文写的很清楚,方法.步骤.优缺点以及实例,所以就借用一下啦^_^ [搭建一个免费的,无限流量的Blog](http://www.ruanyifeng. ...

  6. 利用GitHub搭建Hexo博客并开启HTTPS

    Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. GitHub 是一个面向开源及私有软件项目的托管平台 ...

  7. 搭建局域网maven仓库

    第一步: 下载Nexus http://nexus.sonatype.org/downloads/下载最新版本 解压缩到任意目录,我的直接解压到了E盘---------->E:\nexus-2. ...

  8. 利用github搭建个人网站

    1.注册一个github  https://github.com/ 2.新建一个仓库  仓库名 用 Owner.github.io 的格式,然后点击创建 3.源码上传至github 安装github桌 ...

  9. 利用Github搭建自己的博客

    教程链接:搭建个人博客 嘿嘿嘿!!一直想自己搭建博客的,一直没机会,这次终于把博客搭了起来.虽然只是一个壳子..套了别人的模板~不过还是很令人兴奋哟!总的来说,就按照这个教程一直往下走,其中有一个坑就 ...

随机推荐

  1. UI设计初学者教程:色彩基础知识

    编辑:千锋UI设计 初学设计都会先认识三原色,通常我们说的三原色指的是颜料三原色:红.黄.蓝:其实三原色还有色光三原色:红.绿.蓝.我们通常说的红黄蓝就是减色法三原色,而红绿蓝是加色法三原色.可能这么 ...

  2. db2创建数据库

    1.在实例用户用户下执行 db2 create database <DBName>  on /home/db2inst1/<DBName>  using codeset UTF ...

  3. Valid Mountain Array LT941

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  4. default(T) 和 typeof 和 GetType()

    一.default(T) 在泛型编成中如果不限制T类型参数是值类型或引用类型的话 你程序内部可能会出现错误,因为值类型不允许NULL.所以default用来获取一个类型的默认值,对于值类型得到new ...

  5. Hbase常用操作(增删改查)

    Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区  作者:net19880504 [字体:大 中 小]     运行Eclipse,创建一个新的Java工程“HBa ...

  6. js网页上画图

    保存 1.d3.js  (http://www.d3.org/)使用svg技术,展示大数据量,动态效果很好,但是API暴露的不好,得靠自己摸索. 2.http://raphaeljs.com/refe ...

  7. 2019.02.07 bzoj4316: 小C的独立集(仙人掌+树形dp)

    传送门 题意:给出一个仙人掌森林求其最大独立集. 思路:如果没有环可以用经典的树形dpdpdp解决. fi,0/1f_{i,0/1}fi,0/1​表示第iii个点不选/选的最大独立集. 然后fi,0+ ...

  8. MySQL批量修改表前缀

    error_reporting(0); $old_pre = 'tdr_'; // 原表前缀 $new_pre = 'db_'; // 新表前缀 // 配置连接 $db = new mysqli('1 ...

  9. java Concurrent包学习笔记(一):ExecutorService

    一.介绍 ExecutorService是java.util.concurrent包中的一个线程池实现接口.其有两个实现类: 1)ThreadPoolExecutor:普通线程池通过配置线程池大小,能 ...

  10. JAVA技术路线2

    https://www.zhihu.com/question/56110328 1.JavaSE学习视频 http://pan.baidu.com/s/1bp3g6rd2.javaweb的学习视频 h ...