> 文档中心 > Maven本地仓库有jar包却提示找不到 / 生成.lastUpdated文件

Maven本地仓库有jar包却提示找不到 / 生成.lastUpdated文件


Maven本地仓库有jar包却提示找不到 / 生成.lastUpdated文件(Maven仓库_remote.repositories文件的作用)

存在的问题

使用Maven管理项目时,如果连不到远程仓库,但是明明本地仓库中有对应的jar包,此时还是报找不到对应的包(Could not resolve dependencies for project)
原因是maven3.x版本在从远程仓库下载资源后,会在本地仓库的目录下生成对应的_remote.repositories文件,标示该资源的来源,如果你有这个文件_remote.repositories,那就不能直接访问本地仓库的jar包了,必须远程仓库上有才行,否则就会报错。

解决方法是将本地仓库对应依赖目录下的_remote.repositories文件删除,亲测有效

_remote.repositories文件的作用:
Maven修改setting文件后本地有jar或pom, 还是会去远程仓库获取
本来的作用是为了保证你修改仓库配置后, 从原来的远程仓库获取的依赖还能从原来的仓库获取
但却导致了很多人从别人那边拷贝的本地仓库, 在连不上远程仓库的情况下不能正常使用
此时可以通过删除仓库中的_remote.repositories文件, 解决由于更换仓库带来的jar包不兼容问题

_remote.repositories文件说明

#Thu Jan 24 11:42:29 CST 2019#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.#Wed Mar 09 17:47:48 CST 2022fastjson-1.2.70.pom>newclaim-main=fastjson-1.2.70.pom>nexus-aliyun=fastjson-1.2.70.jar>newclaim-main=fastjson-1.2.70-sources.jar>newclaim-main=fastjson-1.2.70.jar>nexus-aliyun=#这里的nexus-aliyun表示setting.xml中配置的私服id

其中
这个文件存储的是每次从私服或者中央仓库下载的jar包的信息。
该依赖既有jar, 也有pom
第4行:表示该pom第一次从newclaim-main(私服id)来的, 将其写入到_remote.repositories
第5行:表示没有从私服, 而是从远程仓库nexus-aliyun(阿里云镜像), 拉取pom, 同样写入_remote.repositories
第6/8行:分别从两个仓库拉取jar包的记录
第7行: 从私服拉取source源码的记录

只有存在该资源的镜像或私服才会将私服或镜像的id写入到_remote.repositories

假设我们更换了私服地址为nexus-abc,并且该私服不存在该资源,那么就会生成fastjson-1.2.70.jar.lastUpdatedfastjson-1.2.70.pom .lastUpdated文件, 代表获取maven依赖失败, 项无法成功构建;
也就是说: _remote.repositories文件,标示该资源的来源,如果在这个_remote.repositories文件存在的情况下,那在访问本地依赖的同时,还必须确保远程仓库上有对应的文件(这里的远程是setting文件中配置的镜像或远程仓库, 用来进行关联,否则就会报错;

常见的Maven报错信息:

Could not resolve dependencies for project com-hallo-mybatis:generator:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: com.tencent.tsf:spring-cloud-tsf-dependencies:pom:1.1.1-RELEASE, com.tencent.tsf:spring-cloud-tsf-sleuth:jar:1.1.1-RELEASE, com.tencent.tsf:spring-cloud-tsf-auth:jar:1.1.1-RELEASE: Failure to find com.tencent.tsf:spring-cloud-tsf-dependencies:pom:1.1.1-RELEASE in <span style="color:#ab4642">http://***:8081/repository/public/ was cached in the local repository, resolution will not be reattempted until the update interval of public has elapsed or updates are forced

批处理脚本

顺便我写了一个一键清理.lastUpdated文件和_remote.repositories文件的批处理脚本
可以从下面的链接下载
https://download.csdn.net/download/q863672107/85252684

参考

https://blog.csdn.net/zzb5682119/article/details/89217780
https://blog.csdn.net/lovepeacee/article/details/103094247

狗狗宠物资料大全