How to force Gradle to redownload dependencies?

Sometimes We want to redownload dependencies in a Gradle project. How can you force grade dependencies to download freshly?

This post describes how to do the command line to force snapshots and release dependencies in a Gradle project.

In development, We have many use cases where we have to redownload dependencies.

  • when you are doing Gradle build the first time, It downloads all dependencies For suppose, the build fails and some dependencies are not downloaded due to network issues.
  • Sometimes, the dependency version has been changed, and You have to re-download the new version

Gradle downloads the dependencies from remote repositories like Nexus. My java application build failed as spring-core loaded partially and was not downloaded completely.

You can check another post on maven force redownload dependencies.

Clean Gradle cache or single dependency

It is straightforward to remove all Gradle cache from a build.

Gradle dependencies and metadata are stored under your project .gradle/caches/ folder.

In windows, This folder is located in C:\Users\%USERNAME%\.gradle\cache In Linux, you can find it in the user home directory $userhome/.gradle/caches/

on Windows, You can manually delete the .gradle/caches/ folder and do a fresh Gradle build and it downloads dependencies from scratch.

on Linux, You can use the below run command

rm -rf $userhome/.gradle/caches/

The only drawback is, that if your Gradle project has a lot of dependencies, You have to download all dependencies for the first time and it is time taking process.

Let’s move to another approach.

How to force update dependencies of a Gradle project command line?

It is an inbuilt option provided by Gradle with a command line.

--refresh-dependencies command line parameter tells Gradle to build with ignore cache folder dependencies and do freshly download all dependencies from a remote repository.

Here is a command-line option

In windows, You can use the below commands.

gradlew build --refresh-dependencies

if it is a spring boot project, You can use the below command

gradlew bootRun --refresh-dependencies

In Linux and Mac systems, You can use the below commands.

./gradlew build --refresh-dependencies

if it is a spring boot project, You can use the below command

./gradlew bootRun --refresh-dependencies

build Gradle configuration with cacheChangingModulesFor and change attribute

build.gradle file, You can add the below configuration

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

And my dependencies with change attribute in Gradle configuration are as follows for gradle version <6.3

implementation('com.package:artifact:1.0.1-SNAPSHOT') {
    changing = true
}

for gradle version >6.3

implementation('com.package:artifact:1.0.1-SNAPSHOT') {
    isChanging = true
}

Conclusion

Listed out different approaches to update dependencies in the Gradle project.