【问题标题】:Is there a simple way to remove one dependency from the local gradle cache?有没有一种简单的方法可以从本地 gradle 缓存中删除一个依赖项?
【发布时间】:2016-07-30 03:51:34
【问题描述】:

本地 gradle 缓存存储 maven/gradle 依赖项的副本。 How to clear gradle cache? 涵盖如何清除整个缓存,但不包括单个包。

有没有一种简单的方法可以从本地 gradle 缓存中删除一个包?例如,在积极开发库时,这将很有用。为了测试一个小的库更改,我目前必须从文件系统中清除整个缓存,以便不使用旧缓存版本的库。

我知道也可以使用How can I force gradle to redownload dependencies? 中描述的gradle ResolutionStrategy。我宁愿不更改 gradle 配置,因为大多数时候并且对于大多数开发人员来说,默认缓存行为很好。

【问题讨论】:

  • 您是否有不想使用--refresh-dependencies 的原因?
  • @RaGe 因为这会忽略所有缓存条目。我想忽略一个条目,而不是全部。
  • 明白了,尽管完全忽略可能是(过度)执行此操作的最简单方法。
  • 您说您也不想对 build.gradle 进行更改,您愿意运行不同的 gradle 脚本吗?
  • @RaGe 是的,但希望这不涉及分叉现有的 gradle 脚本。

标签: caching gradle android-gradle-plugin


【解决方案1】:

所以这是我编写的一个快速脚本:

seekanddestroy.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

【讨论】:

    【解决方案2】:

    效果很好,但对于较新版本的 gradle,请改用它:

    task seekAndDestroy{
        doLast {
            configurations.findanddelete.each{ 
                println 'Deleting: '+ it
                delete it.parent
            }
        }
    }
    

    【讨论】:

    • 谢谢!唯一的问题是为什么 gradle 不为我们实现这个,而不是期望我们自己实现它。删除依赖是一件很常见的事情,以至于在 gradle 中错过这个功能似乎很奇怪。
    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-04-22
    相关资源
    最近更新 更多