Gradle 获取父目录的文件集合

Gradle get file collection of parent directories

我正在尝试配置一个任务,该任务将从一组二进制 junit 测试结果中生成单个报告,但我无法创建一个包含所有结果文件所在路径的 FileCollection位于。

我的任务是这样定义的

task aggregateTestREports(type: org.gradle.api.tasks.tests.TestSupport) {
   destinationDir = reportDir
   testResultsDirs = ???
}

在哪里???是我没有开始工作的部分。

我可以使用以下代码获取构建结构中 output.bin 文件的列表,但我需要将其转换为文件所在目录的列表。

fileTree(dir: '.', includes: ['**/test-results/binary/test/output.bin'])

我试过像这样从基础 class 创建自定义 class 并将该行的结果传递给 testOutputBinFiles 参数并动态计算文件

class AggregateTestReport extends TestReport {
   @Input
   def testOutputBinFiles 

   def getTestResultDirs() {
      def parents = []
      testOutputBinFiles.each {
         File file -> parents << file.parentFile.absoluteFile
      }
      parents
   }
}

但这给我一个错误,即 return 值与 FileCollection

不兼容

FileCollection 的文档指出获得新的唯一方法是使用 files() 函数,但在自定义 class 中不可用获取新文件。

您可以使用 'reportOn' 属性 列出要聚合的 Test 个任务来声明类型 TestReport 的任务:

task allTests(type: TestReport) {
    destinationDir = file("${buildDir}/reports/allTests")
    reportOn test, myCustomTestTask
}

Benjamin Muschko 在 "Gradle in Action" 的第 7.3.3 节中概述了这种方法(强烈推荐阅读)。

由于我的项目中测试的编写方式存在问题,采用 dnault 的回答引入的依赖性导致我们的测试失败。最终,当我们解决导致此问题的项目中的问题时,他的解决方案将起作用,因此我接受了该答案。为了完整起见,我的权宜之计解决方案最终成为

def resultPaths(FileCollection testOutputBinFiles) {
  def parents = []
  testOutputBinFiles.each {
     File file -> parents << file.parentFile.absoluteFile
  }
  parents
}

task aggregateTestReports(type: org.gradle.api.tasks.testing.TestReport) {
  destinationDir = reportDir
  reportOn resultPaths(fileTree(dir: '.', includes: ['**/test-results/binary/test/output.bin']))
}