【问题标题】:Gradle equivalent to Maven's "copy-dependencies"?Gradle 相当于 Maven 的“复制依赖”?
【发布时间】:2014-11-02 09:48:18
【问题描述】:

在 Maven-land 中,每当我想简单地拉下特定 POM 文件的传递依赖项时,我只需打开一个 shell,导航到 POM 所在的位置,然后运行:

mvn dependency:copy-dependencies

boom,Maven 在当前目录中创建了一个target/ 目录,并将所有可传递获取的 JAR 放置到该位置。

我现在尝试切换到 Gradle,但 Gradle 似乎没有相同的功能。所以我问:Gradle 是否有等同于 Maven 的 copy-dependencies? 如果有,有人可以提供一个例子吗?如果不是,其他开发者会觉得这是对 Gradle 社区的有价值的贡献吗?

【问题讨论】:

    标签: maven gradle dependency-management


    【解决方案1】:

    在 gradle 中没有 copy-dependencies 的等价物,但这里有一个任务:

    apply plugin: 'java'
    
    repositories {
       mavenCentral()
    }
    
    dependencies {
       compile 'com.google.inject:guice:4.0-beta5'
    }
    
    task copyDependencies(type: Copy) {
       from configurations.compile
       into 'dependencies'
    }
    

    做贡献值得吗? AS 你可以看到这很容易做到,所以我不这么认为。

    编辑

    从 gradle 4+ 开始:

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

    【讨论】:

    • 对于任何 Gradle 新手,请使用此代码 sn-p 并将其放入您机器上某个位置的 build.gradle 文件中。然后打开一个 shell,导航到您创建 build.gradle 的位置并输入 gradle copyDependencies。 Gradle 执行后,将出现一个 dependencies 目录,其中包含所有传递的 deps。您现在可以编辑此build.gradle 以包含您喜欢的任何依赖项,并确保在每次运行gradle copyDependencies 之前删除dependencies。
    • 如果您想复制运行时依赖项,请将from 目标更改为configurations.runtime
    • 对于 compile,runtime 和 compile 都将被复制。
    • @Opal 正好相反 - 对于runtime compile 和 runtime 将被复制 docs.gradle.org/current/userguide/…
    • 我刚刚完成了使用 Gradle 进行高级依赖管理的编写指南。请看stackoverflow.com/q/31662781/746347
    【解决方案2】:

    compile 的依赖配置在 gradle 4.x 中被弃用了。您需要将其替换为默认值。于是上面的代码-sn-p就变成了:

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

    【讨论】:

    • 使用Android Gradle Plugin 4.0.1(即Gradle 6.1.1)不再有效。该任务根本不做任何事情。使用 --info 开关运行 Gradle 会产生以下消息:Skipping task ':app:copyDependencies' as it has no source files and no previous output files. Do you know what can be done with Gradle >= 6.1.1?
    • @ackh 找到解决方案了吗?
    • @Mahdi-Malv 是的,我找到了一个解决方案。请参阅我的回答here。如果您有更好的方法,请告诉我。
    【解决方案3】:

    这是等效的 Kotlin DSL 版本(添加了 buildDir 前缀以使其复制构建文件夹中的依赖项):

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

    【讨论】:

      【解决方案4】:

      在升级到适用于 Gradle 6.1.1 的 Android Gradle 插件 4.0.1 后,此处提供的答案对我不起作用。这是我目前使用的版本:

      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)
              }
            }
          }
        }
      }
      

      它一点也不优雅,但至少它是有效的。更简单的版本非常受欢迎:-)

      【讨论】:

        【解决方案5】:

        使用 Kotlin DSL 与 gradle 6.8+ 一起工作的版本

        task("copyDependencies", Copy::class) {
            configurations.compileClasspath.get()
                .filter { it.extension == "jar" }
                .forEach { from(it.absolutePath).into("$buildDir/dependencies") }
        }
        

        运行./gradlew copyDependencies 会将所有jar 从编译类路径复制到./build/dependencies 目录中。 这可以很容易地修改为使用不同的类路径(或组合多个类路径)并根据需要包含其他过滤器。

        【讨论】:

          【解决方案6】:

          这是包含依赖项的另一种方法。这也可能是战争而不是网络档案的 jar。

          dependencies {
              implementation 'my.group1:my-module1:0.0.1'
              implementation 'my.group2:my-module2:0.0.1'
          }
          
          jar {
              from {
                  configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2017-03-30
            • 2013-07-17
            • 1970-01-01
            • 1970-01-01
            • 2016-08-01
            • 1970-01-01
            • 2016-09-07
            • 2014-01-26
            • 2017-06-22
            相关资源
            最近更新 更多