You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Jenkins运行Job时仅使用本地Maven仓库?

解决方案

方案1:命令行加离线参数(最便捷,无需修改配置文件)

执行Maven命令时追加-o参数开启离线模式,Maven会完全禁止访问远程仓库,仅读取本地仓库内容:

mvn clean package -o

如果需要指定自定义本地仓库路径,额外添加-Dmaven.repo.local参数即可:

mvn clean package -o -Dmaven.repo.local=C:/Users/你的用户名/.m2/repository

方案2:修改Maven配置文件(全局生效,适合所有Jenkins任务)

  • 找到Jenkins所用Maven的配置文件:从Jenkins任务控制台输出中找到Maven home对应的目录,进入该目录下的conf文件夹,打开settings.xml文件
  • 配置全局镜像拦截所有远程请求到本地仓库,在<mirrors>节点下添加:
<mirror>
  <id>local-only</id>
  <mirrorOf>*</mirrorOf>
  <url>file:///你的本地仓库绝对路径</url>
</mirror>
  • 禁用所有远程仓库的拉取权限,在<profiles>节点下添加:
<profile>
  <id>disable-remote</id>
  <repositories>
    <repository>
      <id>central</id>
      <url>http://invalid.url</url>
      <releases><enabled>false</enabled></releases>
      <snapshots><enabled>false</enabled></snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <url>http://invalid.url</url>
      <releases><enabled>false</enabled></releases>
      <snapshots><enabled>false</enabled></snapshots>
    </pluginRepository>
  </pluginRepositories>
</profile>
  • 激活上述配置,在<activeProfiles>节点下添加:
<activeProfile>disable-remote</activeProfile>

方案3:修改Jenkins全局Maven配置

  • 进入Jenkins后台,依次点击「Manage Jenkins」→「Global Tool Configuration」,找到对应Maven版本的配置项
  • 把你本地Windows可以正常打包的settings.xml文件上传到Jenkins的Maven配置中,或者指定正确的配置文件路径
  • 确认Jenkins服务器上的Maven本地仓库已经包含你所有的私有依赖:你可以把本地Windows的.m2/repository下对应私有依赖的文件夹,完整复制到Jenkins的Maven本地仓库路径下

注意:如果你的项目pom.xml中单独配置了远程仓库地址,需要先删除pom内的远程仓库配置,或把对应仓库的enabled属性设为false,避免全局配置被覆盖。

内容的提问来源于stack exchange,提问作者ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 12:45:04