简单gradle复制任务的奇怪行为

简单gradle复制任务的奇怪行为,gradle,Gradle,我完全被将任务复制到多个目录中的渐变行为所迷惑 我打算将所有文件从src/common复制到 target/dir1/ps\u模块 target/dir2/ps\u模块 target/dir3/ps\u模块 下面是我的build.gradle外观: project.ext.dirs = ["dir1", "dir2", "dir3"] // ensures that ps_modules directory is created before copying def ensurePsMod

我完全被
任务复制到多个目录中的渐变行为所迷惑

我打算将所有文件从
src/common
复制到

  • target/dir1/ps\u模块
  • target/dir2/ps\u模块
  • target/dir3/ps\u模块
下面是我的
build.gradle
外观:

project.ext.dirs = ["dir1", "dir2", "dir3"]

// ensures that ps_modules directory is created before copying
def ensurePsModulesDir() {
  dirs.each {
    def psModules = file("target/$it/ps_modules")
    if (!(psModules.exists())) {
      println("Creating ps_modules directory $psModules as it doesn't exist yet")
      mkdir psModules
    }
  }
}

task copyCommons(type: Copy) {
  doFirst {
    ensurePsModulesDir()
  }

  from("src/common")
  dirs.each {
    into "target/$it/ps_modules"
  }
}
运行命令
/gradlew copycomons
的结果非常奇怪

文件夹创建按预期工作,但内容/文件仅复制到
target/dir3/ps\u modules
目录中。其余两个目录仍然为空

任何帮助都将不胜感激

以下是作业运行后目标目录树的屏幕抓取:


您可以将单个
配置为
类型的任务
复制
。在这个特定的示例中,gradle的行为与预期的一样。由于
dir3
是列表中的最后一个元素,因此它最终被配置为目标。请看一看问题-你会发现它很有用。另外,线程可能也会有所帮助。

我认为您需要执行以下操作:

task copyCommons(type: Copy) {
    dirs.each {
        with copySpec {
            from "src/common"
            into "target/$it/ps_modules"
        }
    }
}
我认为您可以通过此更改摆脱
ensurePsModulesDir()

*编辑*

复制任务似乎迫使我们设置目标目录。您可能认为设置
destinationDir='。
可以,但它用于最新检查,因此任务可能永远不会被认为是最新的,因此将始终运行。我建议您使用
project.copy(…)
而不是
copy
任务。乙二醇

task copyCommons {
   // setup inputs and outputs manually 
   inputs.dir "src/common"
   dirs.each {
       outputs.dir "target/$it/ps_modules"
   }

   doLast {
       dirs.each { dir ->
           project.copy {
               from  "src/common"
               into "target/$dir/ps_modules"
           } 
       }
   }
}

已建议进行编辑,否则将失败,并出现错误:“未为属性'destinationDir'指定任何值。”