65

I have a custom compile task.

task compileSpeedTest(type: JavaCompile) {
    classpath = files('build')
    source = fileTree('src/test/java/speed')
    destinationDir = file('bin')
}

Gradle doesn't try to download dependencies before its execution. I cannot find anywhere a task name which does it to add it on list dependsOn.

1
  • What dependencies are you referring to? Your build script doesn't define any dependencies. Please add the relevant code so we can connect the dots. Feb 17, 2014 at 2:33

9 Answers 9

61

Downloading java dependencies is possible, if you actually really need to download them into a folder.

Example:

apply plugin: 'java'

dependencies {
  runtime group: 'com.netflix.exhibitor', name: 'exhibitor-standalone', version: '1.5.2'
  runtime group: 'org.apache.zookeeper',  name: 'zookeeper', version: '3.4.6'
}

repositories { mavenCentral() }

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

Download the dependencies (and their dependencies) into the folder runtime when you execute gradle getDeps.

2
37

For Intellij go to View > Tool Windows > Gradle > Refresh All Projects (the blue circular arrows at the top of the Gradle window. enter image description here

3
  • 9
    Took me forever to figure this out. I find the word "Refresh" to be very counterintuitive. It does not suggest that it will download new dependencies. Maven's "install" is much clearer IMHO.
    – Chris Neve
    Sep 29, 2018 at 15:54
  • It seems like IDE integration issue. For me Refresh fails with Caused by: java.lang.NoClassDefFoundError: org/gradle/api/internal/plugins/DefaultConvention. Gradle users on Gitlab github.com/gradle/gradle/issues/11769 say that Intellij must have actual version to make this work
    – JeSa
    Aug 26, 2020 at 12:12
  • Or mvn dependency:resolve which is quite self explanatory.
    – Dark Star1
    May 12 at 8:05
25

I have found this answer https://stackoverflow.com/a/47107135/3067148 also very helpful:

gradle dependencies will list the dependencies and download them as a side-effect.

2
  • 9
    It seems to download only the meta information (pom) but not the actual jar file (tested with Gradle 6). Dec 9, 2019 at 12:42
  • does not include unit test dependencies at all, just the meta-information.
    – tosi
    Feb 26, 2020 at 11:27
23

A slightly lighter task that doesn't unnecessarily copy files to a dir:

task downloadDependencies(type: Exec) {
    configurations.testRuntime.files
    commandLine 'echo', 'Downloaded all dependencies'
}

Updated for kotlin & gradle 6.2.0, with buildscript dependency resolution added:

fun Configuration.isDeprecated() = this is DeprecatableConfiguration && resolutionAlternatives != null

fun ConfigurationContainer.resolveAll() = this
  .filter { it.isCanBeResolved && !it.isDeprecated() }
  .forEach { it.resolve() }

tasks.register("downloadDependencies") {
  doLast {
    configurations.resolveAll()
    buildscript.configurations.resolveAll()
  }
}
4
  • 4
    My understanding is that Configuration.files returns a lazily evaluated collection of the File objects pointing to the dependencies in the local .gradle cache. If you reference it it will have to check they are all there, and if they are not, download them. Mar 19, 2018 at 14:27
  • 2
    This is perfect if you are just interested in caching the dependencies.
    – jfloff
    Mar 28, 2018 at 15:17
  • If you're only using this to cache dependencies from repositories, then this has an added benefit of not requiring artifacts from upstream configurations to be generated. This is especially relevant in multi-project builds where inputs and dependsOn directives (including from for a Copy task) would force Gradle to build all upstream artifacts. This is a perfect solution when you want to download dependencies only (without necessarily preparing upstream artifacts). Thank you for this.
    – Mike Hill
    Feb 26, 2020 at 18:24
  • 1
    Seems that in gradle 7+ testRuntime should be runtimeClasspath
    – moffeltje
    Oct 6, 2022 at 20:23
10

This version builds on Robert Elliot's, but I'm not 100% sure of its efficacy.

// There are a few dependencies added by one of the Scala plugins that this cannot reach.
task downloadDependencies {
  description "Pre-downloads *most* dependencies"
  doLast {
    configurations.getAsMap().each { name, config ->
      println "Retrieving dependencies for $name"
      try {
        config.files
      } catch (e) {
        project.logger.info e.message // some cannot be resolved, silentlyish skip them
      }
    }
  }
}

I tried putting it into configuration instead of action (by removing doLast) and it broke zinc. I worked around it, but the end result was the same with or without. So, I left it as an explicit state. It seems to work enough to reduce the dependencies that have to be downloaded later, but not eliminate them in my case. I think one of the Scala plugins adds dependencies later.

1
  • 2
    Worked for me on Gradle 2.13... Needed this for Dockerfized builds... Thank you! Sep 14, 2018 at 2:14
7

You should try this one :

task getDeps(type: Copy) {
    from configurations.runtime
    into 'runtime/'
}

I was was looking for it some time ago when working on a project in which we had to download all dependencies into current working directory at some point in our provisioning script. I guess you're trying to achieve something similar.

1

Building on top of Robert Elliot's answer. For whatever reason, if one is interested in downloading the dependencies to Gradle cache then copying to a local repository like maven's (by default ~/.m2/repository):

task downloadDependencies(type: Exec) {
    configurations.implementation.files + configurations.runtimeOnly.files
    finalizedBy "cacheToMavenLocal"
    commandLine "echo", "Downloaded all dependencies and copied to mavenLocal"
}

task cacheToMavenLocal(type: Copy) {
    from new File(gradle.gradleUserHomeDir, "caches/modules-2/files-2.1")
    into repositories.mavenLocal().url
    eachFile {
        List<String> parts = it.path.split("/")
        it.path = [parts[0].replace(".","/"), parts[1], parts[2], parts[4]].join("/")
    }
    includeEmptyDirs false
}

The task cacheToMavenLocal was copied and adapted from @Adrodoc55's answer on Gradle forum.

0

It is hard to figure out exactly what you are trying to do from the question. I'll take a guess and say that you want to add an extra compile task in addition to those provided out of the box by the java plugin.

The easiest way to do this is probably to specify a new sourceSet called 'speedTest'. This will generate a configuration called 'speedTest' which you can use to specify your dependencies within a dependencies block. It will also generate a task called compileSpeedTestJava for you.

For an example, take a look at defining new source sets in the Java plugin documentation

In general it seems that you have some incorrect assumptions about how dependency management works with Gradle. I would echo the advice of the others to read the 'Dependency Management' chapters of the user guide again :)

-9

There is no task to download dependencies; they are downloaded on demand. To learn how to manage dependencies with Gradle, see "Chapter 8. Dependency Management Basics" in the Gradle User Guide.

10
  • 2
    They say gradle is flexible as ant. Feb 16, 2014 at 17:32
  • 10
    I did. I read "Gradle beyond the Basics" and "Building and Testing with Gradle". But they don't help to much because I cannot trigger an elementary action to download dependencies. Many pages are dedicated to talk about super object task graph and everything is task. And there isn't any sentence retrieving dependencies is not a task. Feb 16, 2014 at 17:42
  • 1
    I disagree with the down vote. The question is quite obvious. I have a Gradle task. I provided it. I look for another task to add it to its dependency list. Feb 16, 2014 at 17:51
  • 1
    I guess the question simply is: How to ensure that for a task that depends on the dependencies (doh), the dependencies are really retrieved first. In my case, I need to implement some post-processing on retrieved artifacts (unzipping ZIPped artifacts to the working tree), but a simple dependsOn: configurations.compile for my unzipDepenencies task does not seem to do the trick. If I just call that task, dependencies are not retrieved first if they're not there. Phrased differently: How does Gradle know when there is a demand for the dependencies?
    – sschuberth
    Aug 18, 2014 at 7:45
  • 1
    Here's some interesting read about why depending on configurations.compile did / does not work.
    – sschuberth
    Aug 18, 2014 at 8:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.