• Maven 没使用本地依赖,报错依赖找不到 Failure to find xxx


    问题

    有这么一种情况,Maven编译时总报错依赖找不到,但是直接去本地仓库的查看,发现对应的依赖是存在的,这是怎么回事?

    原因

    这可能是Maven的异常缓存导致的。Maven在依赖下载不下来时,会在对应依赖的目录下生成.lastUpdated 文件(记录失败时间),就算你手工将依赖拷贝进去,Maven只要检测到依赖下存在 .lastUpdated 文件,就认为此依赖不可用,而且不会重新去下载。(其实是有一个间隔阈值,就是距离上一次失败一段时间后,才会去重新下载,这是为了防止每次都要去尝试下载依赖)。

    解决方案:删除 .lastUpdated 文件等临时文件

    简易版

    打开命令行窗口,先进入到仓库根目录,再执行如下命令

    • Window环境
    for /r %i in (*.lastUpdated) do del %i
    for /r %i in (_remote.repositories) do del %i
    
    • 1
    • 2
    • Linux环境
    find . -name "*.lastUpdated" | xargs rm -fr
    find . -name "_remote.repositories" | xargs rm -fr
    
    • 1
    • 2

    高级版(保存成文件,支持传路径,也方便后续使用)

    • Window环境
      保存成文件 xxx.bat,执行命令 xxx.bat [repo_path]repo_path是仓库地址,可不写,默认是当前用户目录下的.m2\repository
      • 脚本中的@echo on用于开启日志,所有搜索到的文件都会打印出来,若不想要,删掉即可。
    @echo off
    :: usage: xxx.bat [repo_path]
    
    set REPO_PATH=%USERPROFILE%\.m2\repository
    
    if "%~1" NEQ "" set REPO_PATH=%1
    echo repo_path = %REPO_PATH%
    
    if not exist %REPO_PATH% (
        echo path not exist
        goto :end
    )
    
    echo remove cache file...
    @echo on
    for /r %REPO_PATH% %%i in (*.lastUpdated) do del "%%i"
    for /r %REPO_PATH% %%i in (*_remote.repositories) do del "%%i"
    @echo off
    echo ok
    
    :end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • Linux环境
      保存成文件xxx.sh,执行命令sh xxx.sh [repo_path]repo_path是仓库地址,可不写,默认是 ~/.m2/repository
      • 脚本中find命令的-print用于开启日志,所有搜索到的文件都会打印出来,若不想要,删掉即可。
    #!/bin/bash
    # usage: sh xxx.sh [repo_path]
    
    REPO_PATH=~/.m2/repository
    
    if [ "$1" != "" ]; then REPO_PATH=$1; fi
    echo repo_path = ${REPO_PATH}
    
    if [ ! -d "${REPO_PATH}" ]; then
        echo path not exist
        exit 0
    fi
    
    echo remove cache file...
    find "${REPO_PATH}" -name "*.lastUpdated" -print -exec rm {} \;
    find "${REPO_PATH}" -name "_remote.repositories" -print -exec rm {} \;
    echo ok
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    栈与队列:设计循环队列
    讯飞离线语音合成新版(Aikit)-android sdk合成 demo(Java版本)
    枚举&包装类
    lv3 嵌入式开发-8 linux shell脚本函数
    @SpringBootApplication注解SpringBoot深度理解(课时八)
    VM虚拟机 13.5 for Mac
    代码随想录二刷Day 15
    渗透测试-xss的三种类型讲解
    不得不会的MySQL数据库知识点(三)
    每日一题:Spring 框架中都用到了哪些设计模式❓
  • 原文地址:https://blog.csdn.net/qq_31772441/article/details/126559614