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. 20172330 2017-2018-1 《Java程序设计》第六周学习总结

    学号 2017-2018-2 <程序设计与数据结构>第六周学习总结 教材学习内容总结 这一章主要是对数组的学习: 数组是一种简单而功能强大的编程语言结构,用于分组和组织数据.在java中, ...

  2. ICE::Handle 使用崩溃问题

    简单例子如下: #include "Ice/Ice.h" #include "IceUtil/IceUtil.h" #include "Printer ...

  3. golang 引用相对路径package

    My $GOPATH is "/Users/peter/goworkspace" Golang 版本是: go version go1.6 darwin/amd64 在这个work ...

  4. Win7电脑开启局域网连接和共享过程中出现的"您可能没有权限使用网络资源"的解决办法

    Win7电脑开启局域网连接和共享 http://bbs.ithome.com/thread-334567-1-1.html http://jingyan.baidu.com/article/6dad5 ...

  5. 【转】iPhone易被窃听应用三分钟即可获取所有信息

    2011年8月9日10:19 “你有iPhone吗?这下你麻烦了!”昨天香港<东方日报>封面文章用这样的语气报道说,一种iPhone等智能手机窃听程序,正引爆香港. 该报记者亲自试验,发现 ...

  6. 缩放到被选择的部分: ICommand Cmd = new ControlsZoomToSelectedCommandClass();

    AddItem("esriControls.ControlsZoomToSelectedCommand"); //ICommand Cmd = new ControlsZoomTo ...

  7. 在Android.mk文件中输出打印消息

    http://www.xuebuyuan.com/1947880.html 在进行Android NDK的开发当中有时想看看Android.mk文件当中某个变量的值,可以再Android.mk文件当中 ...

  8. .Net中集合排序

    public class StockQuantity { public StockQuantity(string status, DateTime dateTime, int quantity) { ...

  9. 数据访问:Implementing Efficient Transactions

    An OLTP scenario is characterized by a large number of concurrent operations that create, update, an ...

  10. linux配置nginx

    相关命令: nginx -s reload  :修改配置后重新加载生效 nginx -s reopen  :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试ngin ...