Groovy 将configurations.testRuntime与configurations.testCompile合并到副本中

Groovy 将configurations.testRuntime与configurations.testCompile合并到副本中,groovy,gradle,Groovy,Gradle,我想将一些依赖项(从maven检索)复制到我的build.gradle文件中的特定位置 如果我只迭代testRuntime,它可以正常工作。我的代码是: dependencies { testRuntime 'p6spy:p6spy:2.+' testRuntime 'com.h2database:h2:1+' } task foo { copy { from configurations.testRuntime.findAll { it.getAbsolute

我想将一些依赖项(从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"
    }
}
但是,如果存在
h2
依赖关系,我想选择
testCompile
而不是
testRuntime
。所以我试着:

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

我想问题在于我把这两个列表合并在一起了。仍然无法找到正确的方法。

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

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

您的解决方案将在每次生成调用时复制文件,即使未调用
foo
任务。下面是一个正确的解决方案:

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

试着用帕伦包装它
from([configurations.testRuntime,configurations.testCompile].flatte().findAll{it.getAbsolutePath().contains(“/p6spy/”)| it.getAbsolutePath().contains(“/h2/”)
解析器可能正在尝试访问一个名为from…@tim_yates:这解决了前面的异常,但现在我有了一个新方法:
无法为配置中的参数[]找到方法getAbsolutePath():testRuntime'
,所以听起来像是
flatte()
破坏了这里的东西,也许可以尝试:
从([configurations.testRuntime,configurations.testCompile].collectMany{it.findAll{it.getAbsolutePath().contains(“/p6spy/”)| | it.getAbsolutePath().contains(“/h2/”}})
@tim_-yates:这很有效,谢谢!同时,我测试了:
来自configurations.testRuntime.plus(configurations.testCompile).findAll{it.getAbsolutePath().contains(“/p6spy/”)| it.getAbsolutePath().contains(“/h2/”)}
,但我想这可能会导致
配置.testRuntime
被修改,这是我需要防止的。所以请把它作为一个答案贴出来,我以后会接受的。事实上,你的方式更容易理解;-)当您只是添加两个列表时,两个列表都不应通过调用
configurations来更改。testRuntime.plus(configurations.testCompile)
(configurations.testRuntime+configurations.testCompile)
相同。我会取消删除你的答案,我会投票支持:-)谢谢你的澄清,还有过滤背后的想法?我对这些作用域有更多的依赖关系,那么我如何才能只过滤到这2个呢?我已经更新了答案以显示一种过滤方式。