使用checkstyle来规范你的项目
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貌似也不难。永远不要用做不到来限制你的未来。
关注我的公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Redis 主从配置(Windows版)
原文: Redis 主从配置(Windows版) 安装从库 1、复制一份 Redis 文件,当做从库。 2、修改从库文件中 redis.windows.conf 的端口号。 3、安装服务,需要重新设置名称。然后去服务中,开启“redis6380”(此时就可以连接6380的库了) redis-server --service-install redis.windows.conf --service-name Redis6380 配置主从服务器 Redis Slaveof 命令 Redis Slaveof 命令可以将当前服务器转变为指定服务器的从属服务器(slave server)。 如果当前服务器已经是某个主服务器(master server)的从属服务器,那么执行 slaveofhost port 将使当前服务器停止对旧主服务器的同步,丢弃旧数据集,转而开始对新主服务器进行同步。 另外,对一个从属服务器执行命令slaveof no one 将使得这个从属服务器关闭复制功能,并从从属服务器转变回主服务器,原来同步所得的数据集不会被丢弃。 利用『slaveof no one不会丢弃同步所得...
- 下一篇
flag -- 诡异的memcache标记
引子 打从去年一路北漂,进入无人货架行业,业务需求漫天飘,最近总算把工作都规划齐整。回望过去一年多的时间里,诸多东西值得整理,memcache就是其中一个。 看到java的工资高些,队伍中好些人都想学习java,美其名曰:技术多元化。奈何团队中并没有相关经验的人,也深知大家殷切的期盼,所以,只能先撸起袖子自己干,看看书、看看博客、看看视频,两个小项目就上线了,除memcache以外,过程还算顺利,于是就有了这篇文章。 正值高考,突然感怀,当年的失利,让自己更加坚强。 背景 因为目前大部分项目都是.net core ,使用了memcache做为缓存服务器,首先就是 spring boot里集成memcache(使用 spymemcached客户端),集成过程就不说了,添加依赖,编写帮助类,通过@Configuration注入就可以了。 如果以为这样就完了,那就没有这个文章了,真正的故事才刚刚开始..... 问题 配置完成后,就开始读取已经有缓存,然后就提示:Failed to decompress data,如下图,返回的内容就是null,但是在命令行能...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Hadoop3单机部署,实现最简伪集群
- CentOS8编译安装MySQL8.0.19
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Mario游戏-低调大师作品
- CentOS6,CentOS7官方镜像安装Oracle11G