曾经被问到这样一个问题:“你的代码风格是什么样的?” 那时候想到的是,“对函数进行封装,将出现两次以上的代码封装到函数中进行复用,函数的出口尽量少”。“函数长度不要超过30行”,“写好注释与文档”,“变量名要充分的体现参数的意思”,“写好测试代码”等等,后来回想一下,代码格式也很重要,尤其像引入checkstyle,SpotBugs,Jacoco这种代码风格及测试覆盖率的插件到项目中,同样是可以说的。这篇文章就介绍如何在项目中引入这几个插件。
1.在Maven项目中引入checkstyle插件并配置
先去官网看教程,根据自己的实际情况,配置checkstyle.xml文件以及设置绑定到maven的哪个阶段。贴出自己用的:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<configLocation>${project.basedir}/src/main/resources/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.38</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
进入checkstyle的官网,
官网上会教你如何配置checkstyle.xml文件。有需要的化可以直接参考Google的,但是肯定需要根据实际情况修改,例如:
修改之处1:
修改之处2:
配置完成后,在IDEAL中导入该配置文件,首先在IDEA中安装checstyle插件:
并新增自定义的checkstyle.xml文件:
通过checkstyle插件或者运行mvn validate
命令扫描项目:
2.引入SpotBugs插件检测代码中的Bug
在Maven项目中引入SpotBugs插件,根据官网提示,pom.xml配置如下:
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.1.3</version>
<dependencies>
<!-- overwrite dependency on spotbugs if you want to specify the version of spotbugs -->
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
</plugin>
配置完成后,即可使用mvn spotbugs:check
或者mvn spotbugs:gui(图形化的界面)
命令检查代码中的Bug了。
3.引入Jacoco插件校验测试代码覆盖率
在Maven项目中引入Jacoco,pom.xml配置可以去官网找,比如:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
再运行mvn verify
命令的时候,就可以看到日志中显示了jacoco的信息,并且在项目中的target/site/jacoco/index.html
文件中,可以看到项目的测试覆盖率报告(截图来自jacoco官网):
在实际使用中,如果Bean的get()/set()方法用的是lombok插件,那么jacoco还会把equals,hashcode方法当作需要测试的方法,所以需要在src目录下新建lombok.config文件,可参考stack overflow回答,修改pom.xml文件或者在lombok.config中增加:
lombok.addLombokGeneratedAnnotation = true
遇到需要排除的类,比如SpringBoot的启动类,则在pom.xml中配置exclude,举例:
<configuration>
<excludes>
<exclude>com/example/springboot/Application.class</exclude>
</excludes>
</configuration>
4.配置Github CI workflow
在Git项目仓库中Action栏中配置CI:
里面有许多模板,可根据实际修改,例如:
可使用Java With Maven
的模板,将maven.yml文件最后一句改为:
run: mvn verify
这样,分支主干的提交都需要执行maven verify
命令进行验证。