代码质量检测

maven的pmd

pom.xml的插件配置

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-pmd-plugin</artifactId>
   <version>3.10.0</version>
   <configuration>
     <failOnViolation>true</failOnViolation>
     <printFailingErrors>true</printFailingErrors>
     <excludeRoots>
       <excludeRoot>target/generated-sources</excludeRoot>
     </excludeRoots>
   </configuration>
   <executions>
     <execution>
       <goals>
         <goal>check</goal>
       </goals>
     </execution>
   </executions>
</plugin>

添加依赖,使用pmd检查
在这里插入图片描述

spotbugs-filter与使用

spotbugs是maven的一款插件,继承自findbugs,以检查代码中有风险的代码。

插件使用

spring-boot项目中,pom依赖:

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <version>3.1.11</version>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>  
    </plugins>
  </build>

添加pom依赖后,则可以在maven操作界面进行spotbugs的check与查看
在这里插入图片描述

配置过滤掉相关检查

如果想要某些部分的代码不进行spotbugs的检查,或者过滤掉业务必需无法通过检查的代码,可进行xml配置,以下是在springboot项目中的例子。
spotbugs-filter.xml放在项目根目录下

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter
  xmlns="https://github.com/spotbugs/filter/3.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
  <Match>
    <Package name="~.*.domain.*"/>  <!-- 过滤掉domain包里的所有文件 -->
  </Match>
  <Match>
    <Package name="~.*.dto.*"/> 
  </Match>
  <Match>
    <Class name="~.\S*Builder"/>  <!-- 过滤掉Builder类 -->
  </Match>
  <Match>
    <Class name="~.*Model"/><!-- 过滤掉Model结尾的类 -->
  </Match>
</FindBugsFilter>

添加该xml配置,同时要修改pom文件中的spotbugs依赖的地方为:

<plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <version>3.1.11</version>
        <configuration> <!--使用spotbugs过滤生效-->
          <excludeFilterFile>spotbugs-filter.xml</excludeFilterFile>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

这时,再使用spotbugs:check,过滤文件中配置的则不再进行检查。
点击spotbugs:gui,弹出具体文件相应bug位置,以及问题提示。
在这里插入图片描述

checkstyle

Checkstyle是用于检查Java源代码是否符合代码标准或一组验证规则(最佳实践)的工具。
Github地址:https://github.com/checkstyle/checkstyle
一般执行checkstyle:checkstyle,即可在taget中生成对应的检查结果xml,一般是checkstyle-result.xml,打开即可查看检查结果

pom依赖配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.0.0</version>
    <dependencies>
      <dependency>
        <groupId>com.puppycrawl.tools</groupId>
        <artifactId>checkstyle</artifactId>
        <version>8.20</version>
      </dependency>
    </dependencies>
    <configuration>
      <configLocation>google_checks.xml</configLocation>   // 检查标准xml,默认google_checks.xml
      <encoding>UTF-8</encoding>
      <failsOnError>false</failsOnError>
      <includeTestSourceDirectory>true</includeTestSourceDirectory>
    </configuration>
  </plugin>
</plugins>

常见应注意的格式

1.一行字符数到底多少合适,google_checkstyle.xml默认是100,idea默认提示的线是120,大家们建议的是80,您觉得呢?
这个不管多少,统一即可。
2.换行方式,对于不得不换行的代码,需要选择一个恰当的地方进行换行
考虑可读性
建议:有含义的符号之前,在逗号等表示部分结束的含义符号之后。
3.导入包顺序,以及包的分组需注意
4.Javadoc摘要注释
5…的包导入,可以使用idea相关设置在帮忙
如果不想使用包名.
,那就把这里的值设置的大点吧
在这里插入图片描述
6.每行前的缩进字符数, .editorconfig 设置,google_checkstyle检查的是4
在这里插入图片描述7.缺少空格,空行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值