{

How can I 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 snapshot and release dependencies in a Gradle project.

Gradle force update dependency

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, if the build fails and some dependencies are not downloaded due to network issues.
  • Sometimes, the dependency version has been changed, 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 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 stores under your project .gradle/caches/ folder.

In windows, This folder is located in C:\Users%USERNAME%.gradle\cache In Linux, you can find 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 download dependencies from scratch.

on Linux, You can use below run command

rm -rf $userhome/.gradle/caches/

The only drawback is, 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 into 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 below configuration

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

And my dependencies with change attribute in gradle configuration is 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.