Generate Checkstyle HTML report with Gradle

Deadly 提交于 2019-12-04 00:21:40
JB Nizet

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

Looks like I'm late to the party. But still posting this thinking it might help someone else with the same issue.

Gradle 2.10 supports html file report generation. Just make sure you have version configured properly in your gradle-wrapper.properties file.

After that in your build.gradle file you should have a configuration like the below one.

apply plugin: 'checkstyle'

checkstyle {
    toolVersion = '6.4.1'
    sourceSets = [sourceSets.main]
    configFile = rootProject.file("config/checkstyle/checkstyle.xml");
    showViolations = true
    ignoreFailures = true
}

checkstyleTest {
    enabled = false
}

tasks.withType(Checkstyle) {
  reports {
    html.destination rootProject.file("build/reports/checkstyle.html")
  }
}

Here the config file is the file which has the checkstyle modules that you want to use and html.destination is the location where you want your html report to be generated.

For Gradle 2.10, add the following code to your build.gradle:

tasks.withType(Checkstyle) {
  reports {
    html.enabled = true
  }
}

Here is a plugin that will make setting up checkstyle a breeze. It automatically sets up all the required configuration for checkstyle as per your liking and generates a HTML report in the end.

All you need to do is add a few lines to your build.gradle and thats it. No need to create separate xml files.

The plugin is called estilo. You can find more details on how to use it here -- https://github.com/anshulverma/gradle-estilo-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!