michas
michas

Reputation: 26545

How to make gradle download dependencies without actually building things

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.

Upvotes: 32

Views: 22398

Answers (4)

Daley
Daley

Reputation: 71

You can create a custom task that resolves all the configurations( in doing so, it will also download the dependencies without building the project)

task downloadDependencies { 
   doLast { 
       configurations.findAll{it.canBeResolved}.each{it.resolve()}
   }
}

Run command ./gradlew downloadDependencies

Upvotes: 7

Samuele Pilleri
Samuele Pilleri

Reputation: 764

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

Upvotes: 12

Juliano Suman Curti
Juliano Suman Curti

Reputation: 416

My answer will favor the gradle plugins and built-in tasks.

I would use "gradle assemble" in the command-line.

It is a minor version of "gradle build".

This way, you may reduce the time of your preparations before running or building anything.

Check the link bellow for the documentation: https://docs.gradle.org/current/userguide/java_plugin.html#lifecycle_tasks

In general, what is my recipe when I clone a new repository:

-gradle assemble

-do some coding

-gradle run (and basically test until done)

-gradle build (to make distributable files)

note: this last step may have adicional configurations for .jar files as outputs (depends on you).

Upvotes: 0

Michael Easter
Michael Easter

Reputation: 24468

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

Upvotes: 16

Related Questions