Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Gradle Dependencies to a Directory

How can I save all the dependent jars for a module to a directory? I have an application that runs in IntelliJ IDEA, but I want to run it on another computer so I need to copy all the JAR files there.

like image 733
TrevorHayes Avatar asked Oct 08 '17 08:10

TrevorHayes


Video Answer


2 Answers

Firstly, please consider using Gradle (or Gradle Wrapper) to do such things like getting/downloading dependencies of a project on another computer. But if you need to copy dependencies for any other reason you can define a task similar to:

task copyDependencies(type: Copy) {
    from configurations.runtime
    into "lib"
}

When you run:

gradle copyDependencies

runtime dependencies will be copied to a lib/ folder.

Example

build.gradle

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.1'
    compile group: 'com.h2database', name: 'h2', version: '1.4.196'

    testCompile group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4'

}
task copyDependencies(type: Copy) {
    from configurations.runtime
    into "lib"
}

Command:

gradle copyDependencies

And lib/ directory contains:

lib
├── groovy-all-2.3.11.jar
├── h2-1.4.196.jar
└── jackson-core-2.9.1.jar

Use Gradle Wrapper

As I mentioned earlier, please consider using Gradle Wrapper so you don't have to worry about if there is a Gradle distribution installed on another computer. As stated in the documentation you can easily add Gradle Wrapper and then you can run

./gradlew [task]

by using wrapper instead of Gradle installed on your OS. In your case running

./gradlew build 

will download all dependencies and build the project. It's way better than copying dependencies manually, Gradle was invented to do it for us.

like image 158
Szymon Stepniak Avatar answered Sep 28 '22 15:09

Szymon Stepniak


following solution worked for me

configurations.implementation.setCanBeResolved(true) 
configurations.api.setCanBeResolved(true)
task copyDependencies(type: Copy) {
    from configurations.implementation  
    into "libs" 
}
like image 29
Manish Avatar answered Sep 28 '22 15:09

Manish