Checkstyle是什么

自从做了程序员,关于格式化的讨论就不曾中断过,到底什么才是正确的,什么才是错误的,到现在也没有完整的定论。但随着时间发展,渐渐衍生出一套规范出来。没有什么绝对的正确和错误,关键在于规范的定义。最出名的就是google style guide. Checkstyle就是以这种风格开发出的一个自动化插件,来辅助判断代码格式是否满足规范。

为什么要用,我们需要吗

最初,我使用checkstyle是因为公司要求,在经历过一段痛苦的格式重构阶段后,渐渐习惯了这样的格式。到后来,我新建项目就会把这个加进去,最多改几条规则。前一段时间,由于赶项目进度,其他人不熟悉,只好先关掉了。到后面发现真的每个人的风格都不一样,都看着代码怪怪的。所以,又加回来了。对于多人合作的项目,还是建议采用这样的规范,内容不必拘泥于google-checks,找出适合你们团队的风格就好。

如何使用

在gralde中使用

在gralde中用法比较简单。示例项目: https://github.com/Ryan-Miao/springboot-security-demo/blob/master/config/checkstyle/checkstyle.xml

新建一个checkstyle.gradle

/**
* The Checkstyle Plugin
*
* Gradle plugin that performs quality checks on your project's Java source files using Checkstyle
* and generates reports from these checks.
*
* Tasks:
* Run Checkstyle against {rootDir}/src/main/java: ./gradlew checkstyleMain
* Run Checkstyle against {rootDir}/src/test/java: ./gradlew checkstyleTest
*
* Reports:
* Checkstyle reports can be found in {project.buildDir}/build/reports/checkstyle
*
* Configuration:
* Checkstyle is very configurable. The configuration file is located at {rootDir}/config/checkstyle/checkstyle.xml
*
* Additional Documentation:
* https://docs.gradle.org/current/userguide/checkstyle_plugin.html
*/ apply plugin: 'checkstyle' checkstyle {
// The version of the code quality tool to be used.
// The most recent version of Checkstyle can be found at https://github.com/checkstyle/checkstyle/releases
toolVersion = "8.8" // The source sets to be analyzed as part of the check and build tasks.
// Use 'sourceSets = []' to remove Checkstyle from the check and build tasks.
// sourceSets = [project.sourceSets.main, project.sourceSets.test] // Whether or not to allow the build to continue if there are warnings.
ignoreFailures = false // Whether or not rule violations are to be displayed on the console.
showViolations = true
}

然后,在build.gradle中

apply from: 'checkstyle.gradle'

应该庆幸gradle的繁荣,checkstyle都支持的到8.0以上了。后面用maven的时候遇到版本问题,完美主义追求最新版的心态会难受死。

然后,关键的地方是指定checkstyle.xml规则配置文件了。默认会读取根目录下

config/checkstyle/checkstyle.xml

文件的内容来自:https://sourceforge.net/projects/checkstyle/files/checkstyle/

即,去maven中找到对应版本的checkstyle, 然后下载jar,解压,里面有google-checks.xml, 复制里面的内容即可。

还可以直接用最新版本,参见Github

运行任务

./gradlew check

然后可以在以下目录找到检查结果

build/reports/checkstyle/main.html

更多细节,参考gradle 官网.

idea自动格式化

对于检查的结果,手动修改可能比较慢,同样有人提供了idea对应的插件来格式化。

https://github.com/google/styleguide/edit/gh-pages/intellij-java-google-style.xml

用法是,intelij idea - settting - Editor - code style - scheme, 点击旁边的齿轮设置按钮,选择import scheme, intelij idea code style xml.

导入后,使用快捷键可以快速格式化。

contrl + alt + l

在maven中使用

maven中对于单模块应用参见官网

pom中

<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
...
</project>

运行maven site即可在以下目录找到结果

target/site/checkstyle.html

也可以单独执行命令

mvn checkstyle:checkstyle

checkstyle plugin与checkstyle的版本对应关系

http://maven.apache.org/plugins/maven-checkstyle-plugin/history.html

Maven多模块的checkstyle配置

大多数情况下,我们会把项目的逻辑按照模块拆分出来,便于分离和解耦,项目脉络也更加清晰。在这种情况下,我们为每个模块创建checkstyle任务,需要放到parent的pom里。

示例项目: https://github.com/Ryan-Miao/springboot-starter-feign/blob/master/pom.xml

在父项目,parent pom里:


<build> <pluginManagement>
<plugins> <!--compiler在maven声明周期内置,所以后面不用声明也可使用-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <!--公共checkstyle标准配置,可以在子模块中覆盖,修改自定义选项-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<configLocation>config/checkstyle/google-checks-6.18.xml</configLocation>
<consoleOutput>true</consoleOutput>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<skip>false</skip>
<violationSeverity>error</violationSeverity>
</configuration>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement> <!--所有子模块都要执行的plugin-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins> </build> <reporting>
<!--所有子模块都要执行的报告-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin> </plugins>
</reporting>

这里,maven-compiler-plugin不是必须的,事实上,maven会在项目生命周期中自动执行,我添加这个插件的原因是在idea里的java编译级别需要根据这里来指定。

checkstyle plugin的配置有点多,需要仔细理解一下maven中plugin的含义。

build

在maven指令执行的时候会读取这个节点的配置,决定哪个plugin应该执行,怎么执行。

pluginManagement

这个是版本和共同配置的节点, 同dependencyManagement, 为了约束子项目使用共同的配置。不同的是,这个是指plugin。

plugin

这个表示一个插件,maven执行命令都可以用插件来理解。

plugin>configuration

对于plugin的配置,具体有哪些配置项要看具体的plugin。

executions>execution

plugin应该什么时候执行

<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>

id可以随意,phase则是需要绑定到lifecycle的phase中的哪个命令上,这里是绑定到install上,即当执行maven install的时候会执行本plugin。

goals>goal

一个plugin有多个goals,即任务,是指绑定执行哪个任务。这里之绑定checkstyle

checkstyle的错误级别

在checkstyle.xml的配置文件中,有

<property name="severity" value="error"/>

这里是说当前这个配置是什么错误级别。如果配置的是error,那么扫描到不符合条件的,就是打印error。对于配置了

<failsOnError>true</failsOnError>

则会打断命令执行,错误的时候会停止。否则,错误会生成报告,但不会阻止命令执行。如果配置了info

[INFO] --- maven-checkstyle-plugin:3.0.0:checkstyle (install) @ j-d2-job ---
[INFO] Starting audit...
[INFO] E:\workspace\parent-demo\demo-job\src\main\java\com\demo\Service.java:147:34: WhitespaceAround: '+' is not preceded with whitespace. [WhitespaceAround]
Audit done.

会显示是info,不是error,不会阻断任务执行。最开始查了很久都没找到原因,原来在这里配置的,-_-||。

checkstyle里允许的错误级别有error, warning, info. 只有error并配置了failsOnError会打断命令的执行。打断执行后会在对应的子模块的target下生成

target/checkstyle-result.xml

但不能生成html,或者可以选择声明plain,这个更不好看。纠结了半天就妥协了,就自己看xml结果吧,知道具体的class之后再结合idea checkstyle插件来修改就行。

checkstyle遇到的错误

checkstyle缓存

修改checkstyle.xml后发现还报错,原因是没生效,过一会就好了。

EmptyCatchBlockCheck

如果try-catch捕获了异常但却没有任何操作,则会报警。当然,你确实认为不需要任何操作,可以忽略,忽略配置方案

 <module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="ignore|expected;/>
</module>

此时,只要把捕获的excavation变量名称修改为ignore或者expected就可以避免审查。

比如

try {
throw new RuntimeException();
} catch (RuntimeException expected) {
} try {
throw new RuntimeException();
} catch (RuntimeException ignore) {
}

还可以配置注释,只要添加注释即可忽略

<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="ignore|expected"/>
<property name="commentFormat" value="ignore"/>
</module>

比如

try {
throw new RuntimeException();
} catch (RuntimeException ex) {
//ignore
}

AbbreviationAsWordInName

变量名称不允许连续大写,遵循驼峰命名规范。

<module name="AbbreviationAsWordInName">
<property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/>
<property name="ignoreStatic" value="false"/>
<property name="allowedAbbreviationLength" value="1"/>
<property name="allowedAbbreviations" value="XML,URL"/>
</module>

结语

没有尝试和努力解决之前,总是以为很难不可能做到。等像打怪升级一样把项目过一遍之后,你发现checkstyle貌似也不难。永远不要用做不到来限制你的未来。

使用checkstyle来规范你的项目的更多相关文章

  1. PYTHON风格规范-Google 开源项目风格指南

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  2. 创建一个规范的django项目

    1. 创建项目 2. 创建static目录及配置 1.创建放css, javascript,img的目录 2.在settings.py中将static绝对路径保存到变量STATICFILES_DIRS ...

  3. 为了增强团队的协作和高效开发,提升代码质量,TGideas团队一起制订的代码规范。主要包括五部分内容:PC规范、移动端规范、性能优化、CP规范、其他项目规范

    http://tguide.qq.com/main/index.htm

  4. 添加PMD插件扫描潜在的bug

    上一节使用checkstyle来规范你的项目主要解决了代码编码规范问题,比如缩进换行等.这次继续代码健康工具类PMD. 什么是PMD PMD真的不像checkstyle这样的东西所见即所得啊,去官网找 ...

  5. 转!!Java代码规范、格式化和checkstyle检查配置文档

    为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...

  6. CheckStyle, 强制你遵循编码规范

    如今代码静态检查越来越重要,已经成为构建高质量软件的不可或缺的一个验证步骤.如果你使用的是java语言,那么CheckStyle则是一个利器. CheckStyle能够帮助程序员检查代码是否符合制定的 ...

  7. Java代码规范、格式化和checkstyle检查配置文档

    http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...

  8. 玩转Eclipse — 自动代码规范检查工具Checkstyle

    大项目都需要小组中的多人共同完成,但是每个人都有自己的编码习惯,甚至很多都是不正确的.那么如何使小组所有开发人员都遵循某些编码规范,以保证项目代码风格的一致性呢?如果硬性地要求每个开发人员在提交代码之 ...

  9. java项目命名规范

    一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...

随机推荐

  1. STM32 Timer : Auto-reload register register

    Auto-reload register (TIMx_ARR) The auto-reload register is preloaded. Writing to or reading from th ...

  2. TCP编程的迷惑

    server : ip -- 192.168.96.132 client: ip--192.168.96.131 在服务端,accept函数的其中一个入参是listen-socket,会返回一个新的c ...

  3. linux socket TCP UDP bind 同义IP和port

    //TCP and UDP can bind to the same IP & port. #include <sys/types.h> #include <sys/sock ...

  4. 如何:声明、实例化和使用委托(C# 编程指南)

    委托的声明如下所示: C#   public delegate void Del<T>(T item); public void Notify(int i) { } C#   Del< ...

  5. Revit API画垂直于风管的风管

    start /// <summary> /// 选择风管与风管外一点,画与风管垂直的风管. /// </summary> [Transaction(TransactionMod ...

  6. lodash用法系列(1),数组集合操作

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  7. 使用EF Code First搭建一个简易ASP.NET MVC网站,允许数据库迁移

    本篇使用EF Code First搭建一个简易ASP.NET MVC 4网站,并允许数据库迁移. 创建一个ASP.NET MVC 4 网站. 在Models文件夹内创建Person类. public ...

  8. C#编程(二十三)----------实现继承

    原文链接:http://blog.csdn.net/shanyongxu/article/details/46593809 如果要声明派生自另一个类的一个类,可以使用下面的语法: class Deri ...

  9. ArcGIS Pro 切割打印

    ArcGIS Pro 切割打印

  10. 在Android工程中加入AIDL文件时,gen目录生成的文件报错-问题解决

    from://http://blog.csdn.net/watt520/article/details/10099047 今天在弄清除缓存的东东,按照网上别人的方法,创建了一个AIDL文件,这个时候发 ...