引言

近日一直忙着做持续集成,处于安全性考虑,需要在离线环境运行。项目依托Jenkins做Java/Python/Vue等工程的镜像构建,其中Java工程基本基于Maven,在外网条件下通过IDEA或者mvn命令均可正常打包,原本思路是将本地的repo全量拷贝到服务器,再执行同样的mvn命令,但实际出发jenkins构建任务时,经常build失败。哪怕在maven的setting.xml中硬性设置<offline>true</offline>,依旧不起作用。

离线环境解决maven编译外网下包问题

问题原因探索

maven在执行构建过程中,会按照localRepo->privateRepo->mirrorRepo->centralRepo依次去解决包缺失问题,并最终下载到localRepo。因为是全量拷贝windows的localRepo,里边存在了大量的_remote.repositories文件,此文件直接影响了部分jar的同步问题,导致哪怕localRepo明明已经有了jar包,还是固执地去上级仓库拉包。然而我们没有环境配置nexus和mirror,故只能向centralRepo找寻jar包,导致build失败。

行之有效的解决办法

1.在不设置nexus的情况下,直接伪造一个mirror源,指向服务器localRepo,需要在maven的setting.xml文件中,新增一个mirror,格式如下。

  <mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>central</name>
<url>file:///data/maven_repo/Mvn363</url>
</mirror>
</mirrors>

2.删除localRepo下所有的_remote.repositories文件

# 查询所有的_remote.repositories
find . -name _remote.repositories
# 删除当前目录下所有的_remote.repositories
find . -name _remote.repositories | xargs -I {} rm -fr {}

3.再次触发jenkins构建任务,无误。

离线环境解决maven编译外网下包问题

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。