我的GitHub | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|
baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
目录
常见的 Java 代码检查工具
Checkstyle
简介
Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard 符合编码标准. It automates the process 使...过程自动化 of checking Java code to spare 使...免于 humans of this boring (but important) task. This makes it ideal 理想 for projects that want to enforce 实施 a coding standard.
Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration files are supplied supporting the Sun Code Conventions, Google Java Style.
Checkstyle can check many aspects 方面 of your source code. It can find class design problems, method design problems. It also has the ability to check code layout and formatting issues.
AS 插件 ---
gradle 插件
The Checkstyle plugin
performs quality checks on your project’s Java source files using Checkstyle and generates reports from these checks.
相关文档:
插件源码
插件相关源码:
- CheckstylePlugin.java
- Checkstyle.java
- CheckstyleExtension.java
- CheckstyleInvoker.groovy
- CheckstyleReports.java
- CheckstyleReportsImpl.java
Tips:
- gradle 中使用的 checkstyle 插件实际定义在 plugins/quality 包中
- 当前 gradle 使用的默认 checkstyle 版本为:8.37
配置选项
gradle 中的 Checkstyle 插件可以使用的配置选项有:
config
、configDir(废弃)
、configDirectory
、configFile
、configProperties
:指定要使用的自定义规则集ignoreFailures
:有警告时是否允许构建继续进行maxErrors
、maxWarnings
:停止构建之前允许的最大失败数/警告数
,默认值为0/Integer.MAX_VALUE
showViolations
:是否将结果写入标准输出中,设为true时可以通过点击链接快速定位到问题代码位置classpath
:包含要使用的 Checkstyle 库的类路径checkstyleClasspath
:包含要分析的 compiled classes 的类路径reports
:生成的报告文件存放位置reports.html.enabled
:生成xml还是html,只能有一个为truereports.xml.enabled
reports.html.destination
:对应的输出地址reports.xml.destination
从父类继承过来的选项有:
source
:需要进行代码检测的源码路径include
:需要检查的文件。可以多次调用此方法以追加新的规范exclude
:忽略检查的文件。可以多次调用此方法以追加新的规范
最佳集成案例
在 app 的build.gradle
增加如下代码:
plugins {
id 'com.android.application'
id 'checkstyle'
}
checkstyle {
toolVersion = "8.37" //使用指定的checkstyle版本,当前 gradle 使用的默认版本为 8.37
}
def ruleFilePath = "${project.rootDir}/codeCheck/" //检查规则文件路径
def sourcePath = "${project.rootDir}/app/src" //需要进行代码检测的源码路径
def reportFilePath = "${project.rootDir}/codeCheck/reports/" //生成的报告文件存放位置
task checkstyle(type: Checkstyle, group: "codeCheck") {
configFile = file(ruleFilePath + "checkstyle_rule.xml") //使用自定义规则集文件
ignoreFailures = true //有警告时是否允许构建继续进行
showViolations = true //是否将结果写入标准输出中,设为true时可以通过点击链接快速定位到问题代码位置
source = files(sourcePath) //需要进行代码检测的源码路径
classpath = files() //包含要使用的 Checkstyle 库的类路径
include '**/*.java' //需要检查的文件
exclude '**/*.xml' //忽略检查的文件
reports {
xml.enabled = false //生成xml还是html,只能有一个为true
html.enabled = true
xml.destination = file(reportFilePath + "checkstyle_result.xml") //对应的生成的报告文件存放位置
html.destination = file(reportFilePath + "checkstyle_result.html")
}
}
检测结果案例
'method def modifier' 缩进了1个缩进符,应为4个。 26
Parameter name 'ABC' must match pattern '^[a-z][a-zA-Z0-9]*'.
'method def modifier' 缩进了8个缩进符,应为4个。 12
'method def' 子元素缩进了16个缩进符,应为8个。