简体   繁体   English

使用Gradle生成Checkstyle HTML报告

[英]Generate Checkstyle HTML report with Gradle

I'd like to get the output of running Checkstyle via Gradle as an HTML report. 我想通过Gradle将Checkstyle的输出作为HTML报告。

I've found nothing in the Checkstyle plugin documentation . 我在Checkstyle插件文档中找不到任何内容

I've added the following to my build.gradle file. 我已将以下内容添加到build.gradle文件中。

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

but this yielded 但这产生了

What went wrong: A problem occurred evaluating root project 'MyProject'. 出了什么问题:评估根项目'MyProject'时出现问题。

Could not find method checkstyleTask() for arguments [build_1vu33nc0ekgtoo19jt e86o8o42$_run_closure8@1d8ee20] on root project 'MyProject'. 无法在根项目“MyProject”上找到参数[build_1vu33nc0ekgtoo19jt e86o8o42 $ _run_closure8 @ 1d8ee20]的方法checkstyleTask()。

Is there a way to generate Checkstyle HTML reports using Gradle? 有没有办法使用Gradle生成Checkstyle HTML报告?

Thanks. 谢谢。

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. 这要求您将checkstyle-noframes-sorted.xsl文件存储在项目的config目录中,该文件来自checksyle二进制分发的contrib目录。

If you can afford running a SonarQube server, using the sonar plugin leads to a much better user experience, though. 如果您能负担得起运行SonarQube服务器,使用声纳插件可以带来更好的用户体验。

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. Gradle 2.10支持生成html文件报告。 Just make sure you have version configured properly in your gradle-wrapper.properties file. 只需确保在gradle-wrapper.properties文件中正确配置了版本。

After that in your build.gradle file you should have a configuration like the below one. build.gradle文件中,您应该具有如下配置。

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. 这里的config file是包含您要使用的checkstyle模块的文件, html.destination是您希望生成html报告的位置。

For Gradle 2.10, add the following code to your build.gradle : 对于Gradle 2.10,将以下代码添加到build.gradle

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

Here is a plugin that will make setting up checkstyle a breeze. 是一个插件,可以轻松设置checkstyle。 It automatically sets up all the required configuration for checkstyle as per your liking and generates a HTML report in the end. 它会根据您的喜好自动为checkstyle设置所有必需的配置,并最终生成HTML报告。

All you need to do is add a few lines to your build.gradle and thats it. 您需要做的就是在build.gradle添加几行,就是这样。 No need to create separate xml files. 无需创建单独的xml文件。

The plugin is called estilo . 该插件名为estilo You can find more details on how to use it here -- https://github.com/anshulverma/gradle-estilo-plugin 您可以在此处找到有关如何使用它的更多详细信息 - https://github.com/anshulverma/gradle-estilo-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM