11

The local gradle cache stores copies of maven/gradle dependencies. How to clear gradle cache? covers how to clear the whole cache, but not individual packages.

Is there a simple way to remove one package from the local gradle cache? This would be useful, for example, when actively developing a library. To test a minor library change, I currently have to clear the entire cache from the filesystem so an old cached version of the library is not used.

I understand it is also possible to use the gradle ResolutionStrategy described in How can I force gradle to redownload dependencies?. I would prefer not to change the gradle configuration because most of the time and for most developers, the default caching behavior is fine.

Community
  • 1
  • 1
mattm
  • 5,851
  • 11
  • 47
  • 77
  • 1
    is there a reason you do not want to use `--refresh-dependencies` ? – RaGe Apr 08 '16 at 19:53
  • @RaGe Because this ignores ALL cache entries. I want to ignore one entry, not all of them. – mattm Apr 08 '16 at 20:02
  • understood, although the blanket ignore is probably the simplest way to (over)do this. – RaGe Apr 08 '16 at 20:10
  • You said you also do not want to make changes to build.gradle, would you be open to running a different gradle script? – RaGe Apr 08 '16 at 20:11
  • @RaGe Yes, though hopefully that does not involve forking the existing gradle script. – mattm Apr 08 '16 at 20:13

2 Answers2

9

So here's a quick script I whipped up:

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{
    doLast {
        configurations.findanddelete.each{ 
            println 'Deleting: '+ it
            delete it.parent
        }
    }
}

You can invoke this script by running gradle -b seekanddestroy.gradle


Demo of how it works: if your build.gradle looks like this:

apply plugin:'java'

repositories{
    jcenter()
}

dependencies{

    compile 'org.apache.commons:commons-math3:3.2'
}

First time build, includes a download of the dependency:

λ gradle clean build | grep Download
Download https://jcenter.bintray.com/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar

Second clean build, uses cached dependency, so no download:

λ gradle clean build | grep Download

Now run 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

Next build, downloads dependency again:

λ gradle clean build | grep Download
Download https://jcenter.bintray.com/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar
RaGe
  • 22,696
  • 11
  • 72
  • 104
3

Works great, but for newer versions of gradle, use this instead:

task seekAndDestroy{
    doLast {
        configurations.findanddelete.each{ 
            println 'Deleting: '+ it
            delete it.parent
        }
    }
}
dhaag23
  • 6,106
  • 37
  • 35
  • 1
    Thanks! The only problem is why does gradle not implementing this for us, instead of expecting us to implement it ourselves. Removing a dependency is such a common thing that it seems weird to miss out this feature in gradle. – Nick Wills Nov 17 '20 at 17:45