有这么一种情况,Maven编译时总报错依赖找不到,但是直接去本地仓库的查看,发现对应的依赖是存在的,这是怎么回事?
这可能是Maven的异常缓存导致的。Maven在依赖下载不下来时,会在对应依赖的目录下生成.lastUpdated
文件(记录失败时间),就算你手工将依赖拷贝进去,Maven只要检测到依赖下存在 .lastUpdated
文件,就认为此依赖不可用,而且不会重新去下载。(其实是有一个间隔阈值,就是距离上一次失败一段时间后,才会去重新下载,这是为了防止每次都要去尝试下载依赖)。
打开命令行窗口,先进入到仓库根目录,再执行如下命令
for /r %i in (*.lastUpdated) do del %i
for /r %i in (_remote.repositories) do del %i
find . -name "*.lastUpdated" | xargs rm -fr
find . -name "_remote.repositories" | xargs rm -fr
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
xxx.sh
,执行命令sh xxx.sh [repo_path]
,repo_path
是仓库地址,可不写,默认是 ~/.m2/repository
-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