【发布时间】:2017-03-23 04:25:16
【问题描述】:
我有一个运行这样的任务的构建文件。
任务 1 (unpackWar):将 war 文件解压缩到 Temp 文件夹
任务 2 (copyWarFilesToWebContent):将文件复制到包含一些排除项的 WebContent 文件夹
任务 3 (copyRequiredJarFilesToWebContent):将几个 jar 文件从 Temp/WEB-INF/lib 解压缩到 TempJarDir
任务 4 (explodeProductJars):将我们想要的文件从 TempJarDir 复制到 WebContent 文件夹
有一个prepare 任务使用dependsOn 运行这些任务中的每一个,并且我已将mustRunAfter 命令添加到每个任务中,以便它们按顺序执行。还要为每个任务设置 upToDateWhen = false。
似乎发生的事情是任务 1 运行良好并解压缩了战争。然后任务 2 使用来自 Temp 的文件并将所需的文件正确添加到 WebContent。
任务 3 和任务 4 总是以最新状态返回,因为在指定的目录中似乎没有文件可以使用。
如果我在 Temp 文件夹存在时重新运行准备,任务 3 和 4 将正确运行。
我不确定这是由于 fileTree 的工作原理还是我做错了什么。我大约一周前拿起了 gradle,现在仍在掌握它。
任务如下所示:
task prepare(dependsOn: ['unpackWar', 'copyWarFilesToWebContent', 'copyRequiredJarFilesToWebContent'])
prepare.outputs.upToDateWhen {false}
task unpackWar(type: Copy) {
description = 'unzips the war'
outputs.upToDateWhen { false }
def warFile = file(warFileLocation)
from zipTree(warFile)
into "Temp"
}
task copyWarFilesToWebContent(type: Copy) {
mustRunAfter unpackWar
description = 'Moves files from Temp to WebContent Folder'
outputs.upToDateWhen { false }
from ('Temp') {
exclude "**/*.class"
}
into 'WebContent'
}
task explodeProductJars(type: Copy) {
outputs.upToDateWhen { false }
FileTree tree = fileTree(dir: 'Temp/WEB-INF/lib', includes: ['application*-SNAPSHOT-resources.jar', 'services*-SNAPSHOT-resources.jar'])
tree.each {File file ->
from zipTree(file)
into "TempJarDir"
}
}
task copyRequiredJarFilesToWebContent(type: Copy, dependsOn: explodeProductJars) {
mustRunAfter copyWarFilesToWebContent
outputs.upToDateWhen { false }
from ("TempJarDir/META-INF/resources") {
include '**/*.xml'
}
into "WebContent/WEB-INF"
}
我感觉它与 fileTree 有关,但不确定到底发生了什么。
【问题讨论】:
-
您是否考虑过使用
--debug或--info运行 gradle 任务,看看会发生什么?也许在您的问题中包含日志? -
我已经使用 --debug --info 和 --stacktrace 运行过。这些消息有点神秘,因为我记得它说没有要处理的文件或某种程度的东西。戴维斯在下面的回答澄清了为什么它没有看到文件。我相信这是因为当我运行任务时,配置阶段会同时处理每个任务,因此 Temp 文件夹还不存在,这意味着这些任务被标记为最新的。不过可以对此进行更正