16

I have a non-standard project layout.

How can I use Gradle to just download a bunch of JAR dependencies into a lib directory, under the same directory where build.gradle is? For now, I don't need it to do anything else.

Here is what I have so far:

apply plugin: "java"

repositories {
    mavenCentral()
}

buildDir = "."
libsDirName = "lib"

dependencies {
    runtime group: "...", name: "...", version: "..."
}

If I run gradle build, it builds an empty JAR file in the lib directory, instead of downloading my dependencies there.

Switching "lib" with "." in the properties does not work either.

Tobia
  • 17,856
  • 6
  • 74
  • 93

1 Answers1

31

Something like the following should work:

apply plugin: 'base'

repositories {
    mavenCentral()
}

configurations {
    toCopy
}

dependencies {
    toCopy 'com.google.guava:guava:18.0'
    toCopy 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
}

task download(type: Copy) {
    from configurations.toCopy 
    into 'lib'
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Type the command `gradle download` to execute the created task – Maicon Mauricio Aug 25 '21 at 15:19
  • Note that this also downloads transitive dependencies which may, or may not, be a redundant overhead. – Alex Nov 04 '21 at 17:41
  • 2
    @Alex if you want to avoid transitive dependencies, this can easily be handled by setting `{ transitive = false }` in the dependency configuration. Such as shown [here](https://docs.gradle.org/current/userguide/resolution_rules.html#sec:disabling_resolution_transitive_dependencies) – kevinmm Feb 09 '22 at 23:08