不确定我是否正确理解了这个问题,或者您为什么提到构建缓存。我假设您不知道添加的谓词
upToDateWhen()
被认为
此外
其他最新支票,如
TaskOutputs.dir()
?
执行以下示例任务:
task foo {
def outDir = file('out')
outputs.dir(outDir)
outputs.upToDateWhen { outDir.listFiles().length == 1 }
doLast {
new File(outDir, 'foo.txt') << 'whatever'
}
}
只要输出目录中只有一个文件(通过配置
upToDateWhen
)
和
任务生成的文件(
out/foo.txt
)如果任务运行后未更改,则该任务将是最新的。如果更改/删除由输出目录中的任务创建的文件,或者向输出目录中添加更多文件,则该任务将再次运行。
根据评论中的更新问题更新答案:
task foo {
def outDir = file('out')
/* sample task action: */
doFirst {
def numOutFiles = new Random().nextInt(5)
for (int i = 1; i <= numOutFiles; i++) {
new File(outDir, "foo${i}.txt") << 'whatever'
}
}
/* up-to-date checking configuration: */
def counterFile = new File(buildDir, 'counterFile.txt')
outputs.dir(outDir)
outputs.upToDateWhen {
counterFile.isFile() \
&& counterFile.text as Integer == countFiles(outDir)
}
doLast {
counterFile.text = countFiles(outDir)
}
}
def countFiles(def dir) {
def result = 0
def files = dir.listFiles()
if (files != null) {
files.each {
result++
if (it.isDirectory()) {
result += countFiles(it)
}
}
}
result
}