Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Maven-land, anytime I want to simply pull down the transitive dependencies for a particular POM file, I just open a shell, navigate to where the POM is located, and run:

mvn dependency:copy-dependencies

And boom, Maven creates a target/ directory inside the current one and places all the transitively-fetched JARs to that location.

I am now trying to make the switch over to Gradle, but Gradle doesn't seem to have the same feature. So I ask: Does Gradle have an equivalent to Maven's copy-dependencies? If so, can someone provide an example? If not, would other devs find this to be a worthwhile contribution to the Gradle community?

like image 543
smeeb Avatar asked Nov 02 '14 09:11

smeeb


People also ask

What is flatDir in Gradle?

repositories { flatDir { dirs("lib") } flatDir { dirs("lib1", "lib2") } } This adds repositories which look into one or more directories for finding dependencies. This type of repository does not support any meta-data formats like Ivy XML or Maven POM files.

Which Gradle download dependencies?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file).

Does Gradle have pom?

When it comes to managing dependencies, both Gradle and Maven can handle dynamic and transitive dependencies, to use third-party dependency caches, and to read POM metadata format. You can also declare library versions via central versioning definition and enforce central versioning.


3 Answers

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'
}
like image 116
Opal Avatar answered Oct 16 '22 21:10

Opal


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'
}
like image 19
Indroneel Das Avatar answered Oct 16 '22 21:10

Indroneel Das


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")
}
like image 16
TheImplementer Avatar answered Oct 16 '22 21:10

TheImplementer