How can I force gradle to redownload dependencies?


Some times 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 first time, It downloads all dependencies For suppose, if build fails and some dependencies are not downloaded due to network issues
  • Some times, dependency version has been changed, You have to redownload new version

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

You can check other post on maven force redownload dependencies.

Clean Gradle cache or single dependency

This is simple and 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 in user home directory $userhome/.gradle/caches/

on Windows, You can manuall delete .gradle/caches/ folder and do fresh gradle build and it download dependencies from scratch.

on Linux, You can use below run command

rm -rf $userhome/.gradle/caches/

Only drawback with is, if your gradle project has lot of dependencies, You have to download all dependencies for first time and it is time taking process

Let’s move into other approach

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

This is an inbuilt option provided by gradle with command line

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

Here is an command line option

In windows, You can use below commands

gradlew build --refresh-dependencies

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

gradlew bootRun --refresh-dependencies

In Linux and mac systems, You can use below commands

./gradlew build --refresh-dependencies

if it is spring boot project, You can use 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 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.