Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gradle download dependencies without actually building things

Tags:

On a new environment gradle build takes quite a while because all dependencies have to be downloaded.

Is there a way to only download dependencies in order to speed up the following build?

That way we could for example already prefill a CI build environment.

like image 823
michas Avatar asked Nov 03 '17 18:11

michas


People also ask

Why is Gradle not downloading dependencies?

System caches the dependent jars so it won't be downloaded again and again. If your goal is to just see the downloads of the dependencies then you can force it to redownload. Save this answer.

How do I get dependencies in Gradle?

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.


2 Answers

Edit: Updated for Gradle 6+.

Some notes:

  • This new approach downloads jars into a folder, and then deletes the folder. So the result of having the jars in the Gradle cache is a side-effect.
  • It currently uses jars configured for the main source-set but could be generalized.
  • Even though it is neither efficient nor elegant, it can be useful if you actually want the jars (and transitive dependencies): simply comment-out the deletion of the runtime folder.

This solution can be handy when you want the jars (and transitive dependencies), as you simply have to comment-out deleting the folder.

Consider this build.gradle (as an arbitrary, concrete example):

apply plugin: 'java'

dependencies {
    implementation 'org.apache.commons:commons-io:1.3.2'
    implementation 'org.kie.modules:org-apache-commons-lang3:6.2.0.Beta2'
}

repositories { 
   jcenter()
}

task getDeps(type: Copy) {
    from sourceSets.main.runtimeClasspath
    into 'runtime/'

    doFirst {
        ant.delete(dir: 'runtime')
        ant.mkdir(dir: 'runtime')
    }

    doLast {
        ant.delete(dir: 'runtime')
    }
}

Example run:

$ find /Users/measter/.gradle/caches -name "commons-io*1.3.2.jar"

$ gradle getDeps

$ find /Users/measter/.gradle/caches -name "commons-io*1.3.2.jar"
/Users/measter/.gradle/caches/modules-2/files-2.1/commons-io/commons-io/1.3.2/[snip]/commons-io-1.3.2.jar
like image 103
Michael Easter Avatar answered Sep 28 '22 00:09

Michael Easter


I've found ./gradlew dependencies (as suggested by this user) to be very handy for Docker builds.

like image 39
Samuele Pilleri Avatar answered Sep 28 '22 00:09

Samuele Pilleri