用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本地仓库,添加远程地址:
1 2 3 | cd ~/.m2/repository git init git remote add origin git@github.com:liuhuanting/maven.git |
创建.gitignore文件并提交:
1 2 3 | echo *>>.gitignore git add .gitignore git commit -m 'add .gitignore' |
创建分支并提交:
1 2 3 | git branch snapshot git push origin snapshot git checkout snapshot |
找到你要发布的.jar
文件,将它部署到本地Maven仓库:
1
| mvn 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:
1 2 3 4 | cd ~/.m2/repository git add -f com/github/liuhuanting/timo-parser/1.0.0 git commit -m 'snapshot of timo-parser-1.0.0' git push origin snapshot |
好了,仓库的搭建和jar包的发布都已经完成了。
开始使用
你可以在项目的pom.xml
文件中使用该依赖了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <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> |