Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Checkstyle HTML report with Gradle

I'd like to get the output of running Checkstyle via Gradle as an HTML report.

I've found nothing in the Checkstyle plugin documentation.

I've added the following to my build.gradle file.

checkstyleTask {
    reports {
        html {
            destination "build/reports/checkstyle.html"
        }
    }
}

but this yielded

What went wrong: A problem occurred evaluating root project 'MyProject'.

Could not find method checkstyleTask() for arguments [build_1vu33nc0ekgtoo19jt e86o8o42$_run_closure8@1d8ee20] on root project 'MyProject'.

Is there a way to generate Checkstyle HTML reports using Gradle?

Thanks.

like image 268
Matthias Braun Avatar asked Dec 03 '13 21:12

Matthias Braun


People also ask

How do I add checkstyle to Gradle?

To use checkstyle in Gradle you have add the plug-in to your build. gradle and provide a config\checkstyle\checkstyle. xml checkstyle configuration file.

How do I create a checkstyle report?

To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the <reporting> section of your pom. xml . Then, execute the site phase to generate the report.

What is Gradle checkstyle?

The Checkstyle plugin performs quality checks on your project's Java source files using Checkstyle and generates reports from these checks.

How do I disable checkstyle in Gradle?

Also you can use '-x checkstyleTest' to exclude the task via command line.


1 Answers

Here's how I do it in a smal project of mine:

checkstyleMain << {
    ant.xslt(in: reports.xml.destination,
             style: new File('config/checkstyle-noframes-sorted.xsl'),
             out: new File(reports.xml.destination.parent, 'main.html'))
}

This requires that you store the checkstyle-noframes-sorted.xsl file, from the contrib directory of the checksyle binary distribution, in the config directory of your project.

If you can afford running a SonarQube server, using the sonar plugin leads to a much better user experience, though.

EDIT: The above won't work if there are violations. This should in all cases:

task checkstyleHtml << {
    ant.xslt(in: checkstyleMain.reports.xml.destination,
             style: file('/config/checkstyle-noframes-sorted.xsl'),
             out: new File(checkstyleMain.reports.xml.destination.parent, 'main.html'))
}

checkstyleMain.finalizedBy checkstyleHtml
like image 182
JB Nizet Avatar answered Sep 18 '22 14:09

JB Nizet