欢迎关注微信公众号:数据科学与艺术 作者WX:superhe199
用GitHub构建个人Maven仓库 Maven是一个出色的项目管理工具,它的依赖管理功能极其方便。但是对于个人开发者而言,发布jar包到中央仓库略显麻烦,有时候一些jar包也不适合发布到中央仓库,这时便可以利用GitHub来发布jar包,并利用它的raw服务提供对外下载功能。
你需要: 一个配置好ssh-key的GitHub账户 https://github.com git运行环境 http://git-scm.com maven运行环境 http://maven.apache.org
创建一个新的GitHub仓库,记下地址:git@github.com:liuhuanting/maven.git 进入你主机的maven本地仓库.m2/repository,初始化git本地仓库,添加远程地址: cd ~/.m2/repositorygit initgit remote add origin git@github.com:liuhuanting/maven.git 创建.gitignore文件并提交: 123echo *>>.gitignoregit add .gitignoregit commit -m ‘add .gitignore’ 创建分支并提交: 123git branch snapshotgit push origin snapshotgit checkout snapshot 找到你要发布的.jar文件,将它部署到本地Maven仓库: 1mvn install:install-file -Dfile=timo-parser-1.0.0.jar -DgroupId=com.github.liuhuanting -DartifactId=timo-parser -Dversion=1.0.0 -Dpackaging=jar 将本地Maven仓库对应的文件提交到GitHub: 1234cd ~/.m2/repositorygit add -f com/github/liuhuanting/timo-parser/1.0.0git commit -m 'snapshot of timo-parser-1.0.0’git push origin snapshot 好了,仓库的搭建和jar包的发布都已经完成了。
你可以在项目的pom.xml文件中使用该依赖了:
<project>
<repositories>
<repository>
<id>liuhuanting-maven-snapshot-repository</id>
<name>liuhuanting-maven-snapshot-repository</name> <url>https://raw.github.com/liuhuanting/maven/snapshot/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<artifactId>timo-parser</artifactId>
<groupId>com.github.liuhuanting</groupId>
<version>1.0.0</version>
</dependency> </dependencies>
</project>
之前看到有开源项目用了github来做maven仓库,寻思自己也做一个。研究了下,记录下。 简单来说,共有三步: deploy到本地目录 把本地目录提交到gtihub上 配置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里增加:
hengyunabc-maven-repo https://raw.githubusercontent.com/hengyunabc/maven-repo/master/repository
值得注意的是,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
想要使用本地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 http://cemerick.com/2010/08/24/hosting-maven-repos-on-github/