“最新”没有输出时的Gradle任务状态 [英] "Up To Date" Gradle task status when it has no output

查看:103
本文介绍了“最新”没有输出时的Gradle任务状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当任务不产生任何输出时,如何正确地将Gradle任务标记为最新?只要最后一次运行成功并且输入自那时起没有改变,任务应该保持最新。 Gradle指南在 15.9.2 部分之前列出,如下所示:

 没有定义输出的任务永远不会被认为是最新的。 

在这种情况下,如何将任务标记为最新?看来,Gradle需要知道上次成功运行的时间,然后将其与输入的上次修改时间进行比较。作为一种解决方法,脚本可以创建/触摸一个空文件以将任务标记为完成?是否还有其他建议的解决方法?

解决方案

仅仅考虑不同的情况...




  • 没有任何输入或输出的任务。这些都在运行。这可能只是包装现有的执行某些可执行文件。


  • 输入输出的任务。这些在输入或输出改变时运行。这可能是一个编译器。


  • 只有输出的任务。这些只有在输出已经改变/不存在时才运行。这可能是下载内容的东西。 (我认为这些在现实中是非常罕见的,我会计算URL作为输入下载。)

  • 带有输入和不带输出的任务。我在练习中没有遇到过这些问题。




就像你所说的,你可以用输出文件来欺骗最新的检查空。内置的Gradle 测试任务与最你正在描述,并且它有一个报告作为其输出。我想你可能也会有类似的东西。它可以像捕获任务的标准输出/标准错误并将其放入文件一样简单。这对于所有东西都通过的时候并不是很有用,但是对于什么时候失败会很有用。



当然,其中的任何一个都可以用一个自定义的 upToDateWhen 代码位。例如,您有一个启动Web服务器的任务,并且在Web服务器已经运行时它是最新的。我认为这不符合你在这里描述的内容。



首先,我试试:

outputs.files文件($ {buildDir} / reports / $ {name} .out)



我认为这样做可以或不需要在文件中添加东西。


How can you correctly mark a Gradle task as being "up to date" when the task doesn't produce any output? The task should remain "up to date" provided the last run was successful and the inputs haven't changed since then. The Gradle guide states just before section 15.9.2, the following:

"A task with no defined outputs will never be considered up-to-date."

How is it possible to mark tasks as up to date in this case? It appears that Gradle needs to know the time of the last successful run and then compare that to the last modified time of the inputs. As a workaround the script could create / touch an empty file to mark the task as complete? Are there any other suggested workarounds?

解决方案

To just think through the different scenarios...

  • Tasks without any inputs or outputs. These run all the time. This might be just wrapping an existing "do something" executable.

  • Tasks with inputs and outputs. These run when either the inputs or outputs change. This might be a compiler.

  • Tasks with just outputs. These run only when the outputs have changed/don't exist. This might be something that downloads something. (I think these are pretty rare in reality, I'd count the URL to download as an input.)

  • Tasks with inputs and no outputs. I haven't run into these in practice.

Like you've said, you could cheat the up-to-date checks with an output file that is just empty. The built-in Gradle Test task is most similar to what you're describing and it has a "report" as its output. I think you would probably have something similar too. It could be as simple as capturing the stdout/stderr of the task and putting that into a file. That's not too useful for when everything passes, but it would be useful for when things fail.

Of course, any of these could be supplemented with a custom upToDateWhen bit of code. e.g., you have a task that starts a webserver and it's "up-to-date" when the webserver is already running. I don't think that's a good fit with what you're describing here.

To start out with, I'd try:

outputs.files file("${buildDir}/reports/${name}.out")

I think that'd work with or without actually putting something in the file.

这篇关于“最新”没有输出时的Gradle任务状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆