文章内容
一、maven仓库的结构
maven 仓库的结构可以通过打开本地仓库查看,一般在 ~/.m2 目录下。

示例中 maven 仓库根目录为 repository,alipay.alipay-trade-sdk 为包名,2.0.0 为版本号。
实际上只要是能够远程访问的地址,并且能按 “/包名/…/版本号/maven结构文件” 返回数据的都可以用作 maven 仓库。
二、使用 github 或者 gitee(码云)当作 maven 仓库的途径
github 有专门的插件可以实现这个功能,码云的 api 与 github 不同,目前没有找到有大神实现这种插件。.而且国内github的速度不给力,所以个人偏向于使用码云gitee平台。
三、使用码云gitee搭建个人maven仓库
1、搭建步骤
使用码云搭建一个maven仓库,共有三步:
- deploy到本地目录
- 把本地目录提交到码云上
- 配置git地址为仓库地址
2、配置local file maven仓库,deploy到本地
maven可以通过http, ftp, ssh等deploy到远程服务器,也可以deploy到本地文件系统里。例如:把项目deploy到C:/gitFiles/maven/repository/目录下:
1 2 3 4 5 6 | <distributionManagement> <repository> <id>ntan-maven</id> <url>file:C:/gitFiles/maven/repository/</url> </repository> </distributionManagement> |
通过命令行则是:
1 | mvn deploy -DaltDeploymentRepository=ntan-maven::default:: file :C:gitFiles /maven/repository/ |
推荐使用命令行来deploy,避免在项目里显式配置。官方文档:https://maven.apache.org/plugins/maven-deploy-plugin/、https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
3、把本地仓库提交到github上
把这个目录提交到gitee上。在码云上新建一个项目maven,然后把C:/gitFiles/maven/下的文件都提交到码云上。
1 2 3 4 5 6 | cd C:gitFiles /maven git init git add repository/* git commit -m 'deploy xxx' git remote add origin git@gitee.com:ntan520 /maven .git git push origin master |
最终效果可以参考:https://gitee.com/ntan520/ntan-core-processor
4、 码云仓库路径
因为码云使用了gitee.com/${user_account}/${project_name}/tree/${branch}这个域名用于 tree 文件下载。所以maven仓库为:https://gitee.com/ntan520/maven/tree/master/repository
四、将当前 git 项目与 maven 仓库合二为一
1、在 pom.xml 中指定项目信息及打包方式
1 2 3 4 5 | < groupId >com.ntan520.core</ groupId > < artifactId >ntan-core-processor</ artifactId > < version >1.0</ version > < packaging >jar</ packaging > < name >ntan-core-processor</ name > |
关键参数为 groupId、artifactId、version,这三个唯一确定一个 maven 依赖包。packaging 参数指定了打为 jar 包。
2、将远程仓库目录配置到本项目中
在与 <dependencies> 标签平级的地方添加:
1 2 3 4 5 6 | < distributionManagement > < repository > < id >local-maven</ id > < url >file:${project.basedir}/release</ url > </ repository > </ distributionManagement > |
配置完成后,执行 maven 的 deploy 方法将会在当前工程目录下生成 release 目录,里面就是 maven 仓库。
3、将 release 添加到 git 项目中,并提交到远程 git 仓库,如 github 或 gitee
在 git 平台文件页面中,进入 release 目录,并记录下其路径。
例如:码云的路径:https://gitee.com/ntan520/ntan-core-processor/tree/master/release
五、第三方工程引用位于 git 仓库中的 maven 依赖
1、在项目中配置仓库属性,也就是码云路径,与 <dependencies> 标签平级
1 2 3 4 5 6 | < repositories > < repository > < id >gitee-maven</ id > </ repository > </ repositories > |
2、在 <dependencies> 标签中添加依赖
1 2 3 4 5 | <dependency> <groupId>com.ntan520.core< /groupId > <artifactId>ntan-core-processor< /artifactId > <version>1.0< /version > < /dependency > |
3、更新项目
重新导入一下 maven 即可
六、注意事项
- 以上步骤默认是 public 访问权限仓库,如果是 private 仓库,需要在本地 setting.xml 文件中配置仓库的用户名和密码
- 需要删除本地仓库中的代码才能进行测试,或者直接使用第二台电脑
- 当前项目的 jar 包就在当前项目的 release 目录下固然不错,但也导致了每引入一个此类型的 jar 包,都需要重新配置一个仓库
- 查看目录时,可以先进入 release 目录中,打开一个文本文件,再点击原始数据,从浏览器的地址中可以截取