才尝试更新pom文件,重新下载依赖,或者,mvn clean compile重新编译的时候,会提示类似下面的错误信息:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.477 s
[INFO] Finished at: 2023-03-29T09:02:46+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project app-demo: Could not resolve dependencies for project markvivv:app-demo:jar:3.0.0.RELEASE: The following artifacts could not be resolved: simpleteam:simpleteam:jar:1.0.0 (present, but unavailable): simpleteam:simpleteam:jar:1.0.0 was not found in http://192.168.0.18:8081/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of maven-default-http-blocker has elapsed or updates are forced -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
另一种可能的错误提示:
simpleteam:simpleteam:jar:1.0.0 was not found in http://192.168.0.18:8081/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of maven-default-http-blocker has elapsed or updates are forced
尝试使用 -U 标记(强制更新快照)运行 Maven 导入
错误的提示都指向 maven-default-http-blocker
这是由于Maven官方为了解决CVE-2021-26291安全问题,在3.8.1版本开始发布这个特性,默认禁止从不安全的仓库下载依赖。
maven官方对CVE-2021-26291说明:
所以一切为了安全,maven3.8.1版本开始加上了限制。
文件位置在 ${maven.home}/conf/settings.xml
或 ${user.home}/.m2/settings.xml,具体需要删除的内容如下:
maven-default-http-blocker
external:http:*
Pseudo repository to mirror external repositories initially using HTTP.
http://0.0.0.0/
true
但是,这样子删除之后违反了maven添加这样一个特性的初衷,引入了风险项。并且这种设置会要求使用到私仓的客户端都进行修改,如果推广培训不好,每个人都会需要查一遍这个问题,然后才知道要做对应的配置,下面安排第二种方法。
在项目的根目录添加一个.mvn的目录,并配置maven.config和local-settings.xml两个文件。
maven 3.8.1以上,maven 3.9以下maven.config文件内容如下:
--settings ./.mvn/local-settings.xml
maven 3.9及以上版本的maven.config文件内容如下,注意配置项要放到新的一行,否则配置不生效:
--settings
./.mvn/local-settings.xml
local-settings.xml配置文件内容,注意mirrorOf的ID值需要和pom.xml中的私仓ID一致:
my-repository-http-unblocker
markvivv-repos
http://192.168.7.18:8081/repository/maven-releases
pom.xml中私仓的示例配置,注意ID和.mvn/local-settings.xml中的mirrorOf配置的ID一致:
ovit-repos
markvivv-repos
http://192.168.7.18:8081/repository/maven-releases
true
true
把.mvn目录提交到代码仓库,这样项目的其他开发人员下载到源码的时候,就包含这个配置,不需要再做额外配置。