【发布时间】:2014-10-15 17:09:03
【问题描述】:
我想打开我在检查失败时使用的几个“检查”和“测试”插件的报告文件。我知道我can use "finalizedBy 执行另一个任务,无论原始任务是否执行。仅当相应任务(在此示例中为 checkstyle)失败时,我才使用该知识尝试以下方法打开报告:
task showCheckStyleResultsInBrowser(type: Exec) {
ext.htmlFileName = "main.html"
executable 'open'
args 'file:///' + checkstyleMain.reports.xml.destination.parent + "/" + ext.htmlFileName
}
task showCheckStyleResultsIfFailed {
ext.aCheckFailed = true
doLast {
if (ext.aCheckFailed) {
showCheckStyleResultsInBrowser.execute()
}
}
}
checkstyleMain {
finalizedBy 'showCheckStyleResultsIfFailed'
doLast {
// convert the xml output to html via https://stackoverflow.com/questions/20361942/generate-checkstyle-html-report-with-gradle
ant.xslt(in: reports.xml.destination,
style: new File('config/checkstyle/checkstyle-noframes-sorted.xsl'),
out: new File(reports.xml.destination.parent, showCheckStyleResultsInBrowser.htmlFileName))
showCheckStyleResultsIfFailed.aCheckFailed = false
}
}
解释(据我所知):
-
showCheckStyleResultsInBrowser是实际打开报告的任务。您可以忽略它的实际作用,但如果检查任务失败,则应该执行它 -
showCheckStyleResultsIfFailed任务声明属性aCheckFailed并将其初始化为真。当它被执行时,它会检查它是否仍然为真(这意味着检查没有成功完成),如果是,则使用showCheckStyleResultsInBrowser打开报告。 -
checkstyleMain是执行实际检查的任务。我对它的结果很感兴趣。但是我不知道如何到达它。因此,在checkStyleMain任务结束时,我将aCheckFailed属性设置为false,因为只有在之前的检查均未失败时才会执行最后一步。 -
showCheckStyleResultsIfFailed设置为在checkstyleMain之后执行,无论finalizedBy是什么。这样即使checkstyleMain失败,它也会执行。它使用aCheckFailed属性来确定checkstyleMain是否成功完成。
如果我进行完整的构建,这可以正常工作。但是如果我只是进行部分重建并且 checkstyleMain 任务没有运行,因为它的所有结果都已经是最新的,我最终会得到 aCheckFailed 为真,因为 checkstyleMain 没有运行,这使它看起来好像真的出了什么问题。
那么当且仅当 checkstyleMain 任务失败时,我如何执行我的 showCheckStyleResultsInBrowser 任务?此外,即使我的解决方案确实实现了,我的解决方案也感觉相当麻烦和笨拙。有没有更简单的方法?
【问题讨论】:
标签: gradle build.gradle