是这样的!最近遇到一个非常棘手的难题,我搞了大概2周时间才把他弄出来,因为自己搭了个私服的maven仓库,他不像maven官方仓库一样,可以跟nginx一样转的,所以遇到好几个难点!
第一点:就是maven镜像冲突
解决思路
build:
stage: build
image: maven:3.8.4-openjdk-8
tags: [build]
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- .m2/repository/
before_script:
# 创建本地仓库目录
- mkdir -p .m2/repository
# 复制已有缓存
- if [ -d "你项目/m2" ]; then cp -r 你项目/m2/* .m2/repository/ || true; fi
将你本地的.m2/repository的下面所以得依赖包上传到你项目上,我是在我项目下面建立了一个m2文件夹,上传到这里,然后在CICD的yml, build 下的这个before_script 提前复制到,然后建立一个setting.xml文件,用这个跑mvn install命令
- mvn $MAVEN_CLI_OPTS -f pom.xml -s settings.xml -f pom.xml clean install -X
这个setting文件要写成这样
<!-- src/main/resources/nexussettings.xml -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.dir}/.m2/repository</localRepository>
<servers>
<server>
<id>nexus-mirror</id>
<username>账号</username>
<password>密码</password>
</server>
</servers>
<!-- 配置镜像,替换中央仓库为 Nexus -->
<mirrors>
<mirror>
<id>nexus-mirror</id>
<url>http://nexus.com:8081/repository/group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
这个mirrors是指,你maven依赖的所有包的请求必须走这个你设置好的mirror的URL
还有这个localRepository是你项目在git上跑CICD的依赖包本地路径
一般都是这样写的!。如果不知道位置,你可以去看你跑CICD 有一个Repository的显示路径,我这个是相对路径,如果要绝对路径也行,自己弄进去!哈哈哈,这样maven不同版本冲突就解决了