使用configurations.testCompile复制gradle merge configurations.testRuntime

时间:2014-04-11 08:10:34

标签: groovy gradle

我想将我的一些依赖项(从maven检索到)复制到我的build.gradle文件中的特定位置。

如果我只迭代testRuntime它可以正常工作。我的代码是:

dependencies {
  testRuntime 'p6spy:p6spy:2.+'
  testRuntime 'com.h2database:h2:1+'
}

task foo {
    copy {
        from configurations.testRuntime.findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
        into "outputdir"
    }
}

但是,如果testCompile依赖,我希望转到testRuntime而不是h2。所以我试过了:

dependencies {
  testRuntime 'p6spy:p6spy:2.+'
  testCompile 'com.h2database:h2:1+'
}

task foo {
    copy {
        from [ configurations.testRuntime, configurations.testCompile ].flatten().findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
        into "outputdir"
    }
}

然而,我得到错误:

No such property: from for class: org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated

我想麻烦在于我在这里合并2个列表。仍然无法找到正确的方法。

2 个答案:

答案 0 :(得分:1)

好吧,我自己找到了解决方案,记录下来了:

dependencies {
  testRuntime 'p6spy:p6spy:2.+'
  testCompile 'com.h2database:h2:1+'
}

task foo {
    copy {
        from configurations.testRuntime.plus(configurations.testCompile).findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
        into "outputdir"
    }
}

答案 1 :(得分:1)

即使未调用foo任务,您的解决方案也会在每次构建调用时复制文件。这是一个正确的解决方案:

task foo(type: Copy) {
    from configurations.testRuntime // includes configurations.testCompile  
    into "outputdir"
    include "**/p6spy/**" 
    include "**/h2/**"  
}