Gradle equivalent to Maven's "copy-dependencies"?

Solution 1:

There's no equivalent of copy-dependencies in gradle but here's a task that does it:

apply plugin: 'java'

repositories {
   mavenCentral()
}

dependencies {
   compile 'com.google.inject:guice:4.0-beta5'
}

task copyDependencies(type: Copy) {
   from configurations.compile
   into 'dependencies'
}

Is it worthwhile to do a contribution? AS You can see it's really easy to do, so I don't think so.

EDIT

From gradle 4+ it will be:

task copyDependencies(type: Copy) {
  from configurations.default
  into 'dependencies'
}

Solution 2:

the dependency configuration of compile is deprecated in gradle 4.x. You need to replace that with default. So the above code-snippet becomes:

dependencies {
  implementation 'com.google.inject:guice:4.0-beta5'
}
task copyDependencies(type: Copy) {
  from configurations.default
  into 'dependencies'
}

Solution 3:

This is the equivalent Kotlin DSL version (added the buildDir prefix to make it copy the dependencies in the build folder):

task("copyDependencies", Copy::class) {
    from(configurations.default).into("$buildDir/dependencies")
}

Solution 4:

The answers provided here stopped working for me after upgrading to the Android Gradle Plugin 4.0.1 that works with Gradle 6.1.1. Here's the version that I'm currently using:

task copyPluginDependencies {
  doLast {
    def dependencies = []
    buildscript.configurations.classpath.each { dependency ->
      dependencies.add(dependency)
    }
    dependencies.unique().each { dependency ->
      println(dependency.absolutePath)
      copy {
        from dependency.absolutePath
        into 'app/dependencies'
        eachFile { details ->
          String dependencyPath = dependency.absolutePath
          Pattern regexPattern = Pattern.compile("(^.*caches)(.*)")
          Matcher regexMatcher = regexPattern.matcher(dependencyPath)
          if(regexMatcher.find()) {
            // results in the dependency path starting from Gradle's caches folder
            dependencyPath = regexMatcher.group(2)
          }
          details.setRelativePath new RelativePath(true, dependencyPath)
        }
      }
    }
  }
}

It's not elegant at all but at least it is working. Simpler versions are highly welcome :-)