Joa*_*urz 8 gradle build.gradle
我想打开检查失败时使用的几个"检查"和"测试"插件的报告文件.我知道无论原始任务是否被执行,我都可以使用"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
}
}
Run Code Online (Sandbox Code Playgroud)
解释(据我所知):
showCheckStyleResultsInBrowser实际上是打开报告的任务.您可以忽略它实际执行的操作,但如果检查任务失败则应该执行该操作showCheckStyleResultsIfFailed任务声明一个属性aCheckFailed并将其初始化为true.执行时,它会检查它是否仍然为真(这意味着检查未成功完成),如果是,则使用打开报告showCheckStyleResultsInBrowser.checkstyleMain是执行实际检查的任务.我对它的结果很感兴趣.但是我不知道如何去做.因此,在checkStyleMain任务结束时,我将aCheckFailed属性设置为false依赖于以下事实:如果之前的检查都没有失败,则只执行最后一步.showCheckStyleResultsIfFailed设置为checkstyleMain无论什么时候执行finalizedBy.这样即使checkstyleMain失败也会执行.它使用该aCheckFailed属性来确定是否checkstyleMain已成功完成.如果我完成构建,这可以正常工作.但是如果我只是进行部分重建并且checkstyleMain任务没有运行,因为它的所有结果都已经是最新的,我最终aCheckFailed都是真的,因为checkstyleMain没有运行,这使得它看起来好像出了什么问题.
那么,showCheckStyleResultsInBrowser当且仅当checkstyleMain任务失败时,我该如何执行我的任务?此外,我的解决方案感觉相当麻烦和黑客,即使它实现了它.有没有更简单的方法?
Mar*_*ira 13
您可以询问任务状态以确定它是否失败.
task showCheckStyleResultsIfFailed {
onlyIf {
checkstyleMain.state.failure != null
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5789 次 |
| 最近记录: |