强制Gradle/Maven刷新缓存并重新从Nexus下载依赖jar包

本文介绍了解决Gradle和Maven在搭建Nexus私服时遇到的依赖问题的方法,包括如何处理本地缓存缺失导致的下载失败,并提供了解决方案。

原文  http://www.jackyshen.com/2015/10/18/force-gradle-maven-to-refresh-cache-or-download-dependency-jar-from-nexus/?utm_source=tuicool&utm_medium=referral


最近需要搭建一个Nexus私服,完全不能连接外网的那种,各种Jar包都是手动拷过来的。

问题

第一次上传某个jar包(比如junit-4.12.jar)到Nexus上,然后调用 gradle build 可以正确下载到依赖包。但如果手动删掉了本地缓存的jar包(在 ~/.gradle 下),这时从Nexus的下载过程中断,或者Nexus上暂时不存在这个jar包,那么即使Nexus恢复了正常下载,下次执行 gradle build 时就一直提示不能够找到jar包。

FAILURE: Build failed with an exception.

- What went wrong:
Could not resolve all dependencies for configuration ':testCompile'.
> Could not find junit:junit:4.12.
  Searched in the following locations:
      http://localhost:8081/nexus/content/groups/public/junit/junit/4.12/junit-4.12.pom
      http://localhost:8081/nexus/content/groups/public/junit/junit/4.12/junit-4.12.jar
  Required by:
      org.codehaus.sonar:example-java-maven:1.0-SNAPSHOT

解决方案

回到自己的工作目录下,带参数执行gradle强制刷新 ~/.gradle 下的文件缓存

gradle build --refresh-dependencies

参考 Gradle manual

The –refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts. A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded.

如果是Maven遇到不能刷新本地依赖的话,需要做两件事情:

  1. 到 ~/.m2 下,看看jar包路径是不是带有”.lastUpdated”,有的话就删除之。
  2. 带参数强制刷新 mvn build -U

另外,如果从Nexus将Jar包删除,或重新上传时(需要设置Configuration->Deployment Policy为Allow Redeploy),有时需要在Browse Storage中,右键Jar包路径,先”Update Index”再”Expire Cache”。注意这里是刷新Nexus自身的缓存和索引,不是开发目录本地的缓存!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值