本地gradle缓存存储Maven / gradle依赖项的副本。 How to clear gradle cache?介绍如何清除整个缓存,但不清除单个程序包。
是否有一种简单的方法可以从本地gradle缓存中删除一个软件包?例如,在积极开发库时,这将很有用。为了测试库的次要更改,我目前必须从文件系统中清除整个缓存,以便不使用该库的旧缓存版本。
我知道也可以使用gradle ResolutionStrategy中描述的How can I force gradle to redownload dependencies?。我宁愿不更改gradle配置,因为在大多数情况下,对于大多数开发人员而言,默认的缓存行为很好。
所以这是我整理的快速脚本:
defaultTasks 'seekAndDestroy'
repositories{ //this section *needs* to be identical to the repositories section of your build.gradle
jcenter()
}
configurations{
findanddelete
}
dependencies{
//add any dependencies that you need refreshed
findanddelete 'org.apache.commons:commons-math3:3.2'
}
task seekAndDestroy()<<{
configurations.findanddelete.each{
println 'Deleting: '+ it
delete it.parent
}
}
您可以通过运行gradle -b seekanddestroy.gradle
来调用此脚本
有关其工作原理的演示:如果您的build.gradle看起来像这样:
apply plugin:'java'
repositories{
jcenter()
}
dependencies{
compile 'org.apache.commons:commons-math3:3.2'
}
第一次构建,包括下载依赖项:
λ gradle clean build | grep Download
Download https://jcenter.bintray.com/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar
第二个干净的版本,使用缓存的依赖项,因此无需下载:
λ gradle clean build | grep Download
现在运行seekanddestroy:
λ gradle -b seekanddestroy.gradle -q
Deleting: .gradle\caches\modules-2\files-2.1\org.apache.commons\commons-math3\3.2\ec2544ab27e110d2d431bdad7d538ed509b21e62\commons-math3-3.2.jar
下一个版本,再次下载依赖项:
λ gradle clean build | grep Download
Download https://jcenter.bintray.com/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar
效果很好,但对于较新版本的gradle,请改用此:
task seekAndDestroy{
doLast {
configurations.findanddelete.each{
println 'Deleting: '+ it
delete it.parent
}
}
}