一. 遇到的问题
公司前期开发用的个人电脑,虽然有本地代码库,但是迁移在内网环境的时候还是出现了问题,某些依赖会访问中央仓库。 访问中央仓库的请求会被防火墙屏蔽,所以需要设置一种强制使用本地依赖库的方法。
二. 具体代码
1. POM方式禁用远程依赖
需要在项目的父pom中加入这段代码,这段代码禁用了依赖仓库和插件仓库,可以根据我下面的注释来进行修改。
<project>
...
<!-- 仓库定义部分,这里可以指定项目依赖的库 -->
<repositories>
<!-- 定义一个仓库 -->
<repository>
<id>central</id> <!-- 仓库ID,这是一个唯一标识,用于区分不同的仓库 -->
<url>http://repo.maven.apache.org/maven2</url> <!-- 仓库的URL,指向Maven中央仓库 -->
<releases>
<enabled>false</enabled> <!-- 禁用从此仓库获取发布版本的依赖,即正式发布的库 -->
</releases>
<snapshots>
<enabled>false</enabled> <!-- 禁用从此仓库获取快照版本的依赖,快照版本是开发中的不稳定版本 -->
</snapshots>
</repository>
</repositories>
<!-- 插件仓库定义部分,这里可以指定项目构建时使用的插件的来源 -->
<pluginRepositories>
<!-- 定义一个插件仓库 -->
<pluginRepository>
<id>central</id> <!-- 插件仓库ID,这里也用central,通常与上面的仓库ID一致,表示同一个地址 -->
<url>http://repo.maven.apache.org/maven2</url> <!-- 插件仓库的URL,指向Maven中央仓库 -->
<releases>
<enabled>false</enabled> <!-- 禁用从此插件仓库获取发布版本的插件 -->
</releases>
<snapshots>
<enabled>false</enabled> <!-- 禁用从此插件仓库获取快照版本的插件 -->
</snapshots>
</pluginRepository>
</pluginRepositories>
...
</project>
2. 修改Maven的settings文件
个人不太推荐这种方式,修改文件比配置pom的复杂性要高一些
<settings>
<!-- settings 标签是 Maven 配置文件的根元素 -->
<profiles>
<!-- profiles 标签包含一系列的 profile 元素,每个 profile 配置一组特定的构建设置 -->
<profile>
<!-- profile 元素定义了一组特定的配置,可以被激活并应用到构建过程中 -->
<id>disable-remote-repositories</id>
<!-- id 标签指定了当前 profile 的唯一标识符 -->
<repositories>
<!-- repositories 标签定义了 Maven 项目用来查找依赖的仓库列表 -->
<repository>
<!-- repository 标签定义了单个仓库的配置 -->
<id>central</id>
<!-- id 标签给这个仓库一个唯一的名字,用于区分不同的仓库 -->
<url>http://repo.maven.apache.org/maven2</url>
<!-- url 标签指定了仓库的位置,这里是 Maven 中央仓库的 URL -->
<releases>
<!-- releases 标签定义了如何处理此仓库的正式发布版本的依赖 -->
<enabled>false</enabled>
<!-- enabled 标签设置为 false,表示禁用从这个仓库下载发布版本的依赖 -->
</releases>
<snapshots>
<!-- snapshots 标签定义了如何处理此仓库的快照版本的依赖 -->
<enabled>false</enabled>
<!-- enabled 标签设置为 false,表示禁用从这个仓库下载快照版本的依赖 -->
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!-- activeProfiles 标签定义了哪些 profile 是当前激活的 -->
<activeProfile>disable-remote-repositories</activeProfile>
<!-- activeProfile 标签指定了要激活的 profile 的 ID -->
</activeProfiles>
</settings>