简体   繁体   English

将Gradle依赖项保存到目录

[英]Saving Gradle Dependencies to a Directory

How can I save all the dependent jars for a module to a directory? 如何将模块的所有相关jar保存到目录? 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. 我有一个在IntelliJ IDEA中运行的应用程序,但我想在另一台计算机上运行它,所以我需要复制那里的所有JAR文件。

Firstly, please consider using Gradle (or Gradle Wrapper) to do such things like getting/downloading dependencies of a project on another computer. 首先,请考虑使用Gradle(或Gradle Wrapper)来执行诸如在另一台计算机上获取/下载项目的依赖项之类的操作。 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. 运行时依赖项将复制到lib/文件夹。

Example

build.gradle 的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/目录包含:

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

Use Gradle Wrapper 使用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. 正如我之前提到的,请考虑使用Gradle Wrapper,这样您就不必担心另一台计算机上是否安装了Gradle分发版。 As stated in the documentation you can easily add Gradle Wrapper and then you can run 文档中所述,您可以轻松添加Gradle Wrapper,然后就可以运行了

./gradlew [task]

by using wrapper instead of Gradle installed on your OS. 通过使用包装器而不是在操作系统上安装Gradle。 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. 它比手动复制依赖项更好,Gradle被发明为我们这样做。

following solution worked for me 以下解决方案为我工作

configurations.implementation.setCanBeResolved(true) 
configurations.api.setCanBeResolved(true)
task copyDependencies(type: Copy) {
    from configurations.implementation  
    into "libs" 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM