I would like to open a report file with several plugins "check" and "test", which I use when checking failed. I know that I can use "finalizedBy" to perform another task, regardless of whether the original task was completed. Using this knowledge, I tried to do the following in order to open a report only if the corresponding task fails (in this example, 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 } }
Explanation (as I understand it):
showCheckStyleResultsInBrowser is a task that actually performs report opening. You can ignore what it actually does, but the one that should be done if the check fails- The
showCheckStyleResultsIfFailed task declares showCheckStyleResultsIfFailed property and initializes it as true. When it is executed, it checks to see if it is still true (which means that the verification was not completed successfully), and, if so, opens the report using showCheckStyleResultsInBrowser . checkstyleMain is a task that performs an actual check. I am interested in its result. However, I do not know how to get to it. Thus, instead, at the end of the checkStyleMain task checkStyleMain I set the aCheckFailed property to false aCheckFailed that the last step will be performed only if none of the previous checks failed.showCheckStyleResultsIfFailed set to run after checkstyleMain regardless of whether finalizedBy executed. Thus, it will be executed even in the case of checkstyleMain . It uses the aCheckFailed property to determine if checkstyleMain completed checkstyleMain .
This works fine if I do a complete build. But if I just do a partial rebuild and the checkstyleMain task does not start because all its results have already been updated, I aCheckFailed true because checkstyleMain did not start, which makes it look like something actually went wrong.
So, how can I complete my showCheckStyleResultsInBrowser task if and only if the checkstyleMain task fails? In addition, my decision seems rather cumbersome and hacky even for what it really achieves. Is there an easier way?
source share