目录
这几个工具的用途:
checkstyle:检测代码规范
findbugs:检测代码不明显的语法错误,比如使用“==”比较字符串
pmd:扫描潜在问题,如未用过的局部变量,未用过的导入声明,方法名问题等
jacoco:代码覆盖率检查
首先先说一些,在网上会看到有些会配置这个东西,是为了避免项目环境不一致问题。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.8</compiler.source>
<compiler.target>1.8</compiler.target>
</properties>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
1.配置checkstyle
在build中配置了checkstyle中配置了生效时期段后,会在相应的周期执行,执行失败,则编译失败
如果要跳过checkstyle:mvn clean package -Dcheckstyle.skip=true
checkstyle 内置了四种规范
1. sun_checks.xml -> 默认规范
2.avalon_checks.xml
3.maven_checks.xml
4.turbine_cheks.xml
<build>
<plugins>
<plugin>
<!--插件-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!--执行ID唯一,必须-->
<id>validate</id>
<!--绑定编译的生命周期:验证-->
<phase>validate</phase>
<!--对执行配置-->
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<!--在给阶段执行的命令-->
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
</reporting>
它有有4个命令:
-
checkstyle:checkstyle 执行checkstyle分析并生成违规报告
-
checkstyle:check 分析并向控制台输出不规范计数,可能会导致失败
-
checkstyle:checkstyle-aggregate 多模块分析汇总报告
详细可见官网: http://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html
<phase>validate</phase>这个属性可以不用配置,那么不配置的话,就需要我们用上面的命令去执行,比如:mvn checkstyle:checkstyle;或是到idea右边的maven插件列表里执行。
存在问题:
这个问题没有找到原因,类似的问题我只尝试过checkstyle,不知道其他插件是否也存在,还是我配置有问题。
把checkstyle的配置放到目录里,子模块不能读取
目录如下:
|--project
|-- checkstyle -- 这里多一层目录,里面是checkstyle的配置
|-- checkstyle_ali.xml
|-- parent
|-- pom.xml -- 这个只是配置依赖和配置属性,让service继承
|-- service
|-- pom.xml
|-- build
|-- pom.xml
|-- data
|-- pom.xml
如上,service的pom继承parent的pom,在parent里配置checkstyle的本地路径为checkstyle/checkstyle_ali.xml,
然而这样的话,service里的应用就读取不到checkstyle_ali.xml,
但如果是不要checkstyle目录,就可以读取,如下目录
|--project
|-- checkstyle_ali.xml -- 不要目录,直接文件
|-- parent
|-- pom.xml -- 这个只是配置依赖和配置属性,让service继承
|-- service
|-- pom.xml
|-- build
|-- pom.xml
|-- data
|-- pom.xml
2. 配置findbugs
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<configuration>
<!-- 设置分析工作的等级,可以为Min、Default和Max -->
<effort>Low</effort>
<!-- Low、Medium和High (Low最严格) -->
<threshold>Medium</threshold>
<failOnError>true</failOnError>
<includeTests>true</includeTests>
<!--findbugs需要忽略的错误的配置文件-->
<excludeFilterFile>findbugs-exclude-bugs.xml</excludeFilterFile>
</configuration>
<executions>
<execution>
<id>run-findbugs</id>
<phase>package</phase>
<goals>
<!--检查不通过会构建失败,使用findbugs,失败也会构建-->
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
findbugs有以下几个命令:
-
check:检查代码,没通过,就失败
-
findbugs:检查代码,没通过,不会失败,会生成结果文件
-
gui:启动gui界面显示结果
同样可以手动触发
如下,可以再执行mvn命令时指定过滤文件,或者在pom里指定。
mvn clean package org.codehaus.mojo:findbugs-maven-plugin:3.0.5:findbug
s -Dmaven.test.fail.ignore=true -DskipRepackage=true -Dfindbugs.excludeFilterFile=findbugs-exclude-bugs.xml
3. 配置pmd
这个pmd一开始我看不太懂的,很多配置文件,第一个反应就是,这配置文件哪里有、怎么配、要自己写吗?
好在官方都有默认的,具体见官网:http://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html
官网提醒:pmd 3.0 到 5.0 绑定的规则路径由 /rulesets/xyz.xml 更改为 /rulesets/java/xyz.xml
pmd 3.9.0 后,规则按类别分类:如: /category/java/bestpractices.xml,具体的默认规则看官网
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<rulesets>
<ruleset>/category/java/bestpractices.xml</ruleset>
</rulesets>
<printFailingErrors>true</printFailingErrors>
</configuration>
<executions>
<execution>
<id>pmd-check-verify</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>pmd-pmd-site</id>
<phase>site</phase>
<goals>
<goal>cpd</goal>
</goals>
</execution>
</executions>
<!-- p3c依赖 这里面有pmd依赖的默认配置,要加入 -->
<dependencies>
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
</plugin>
pmd有以下几个命令:
-
check:代码检测,没通过,就编译失败
-
cpd:执行会创建一个详细报表
-
cpd-check:没通过,就编译失败
-
pmd:生成详细报表
4. 配置jacoco
<!--检测代码覆盖率的插件jacoco-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>
${project.build.directory}/jacoco.exec
</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!--
在程序的verify阶段,执行report测试的程序。
文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec.
参数 includes和excludes可用来选定report中过滤的类。
-->
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
<!--这个配置规则会影响编译结果,一般也不会要求这么严格,可以把他注释掉-->
<configuration>
<!-- rules里面指定覆盖规则 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- 指定方法覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定指令覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定行覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定类覆盖到100%,不能遗失任何类 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</plugin>
<!--
使用 maven-surefire-plugin来执行单元测试。
将surefireArgLine赋值给argLine参数,以保证在测试执行时Jacoco agent处于运行状态。
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<!--添加我们需要的测试类规则-->
<includes>
<include>Sample.java</include>
<include>%regex[.*[Cat|Dog].*Test.*]</include>
</includes>
<!--测试执行失败后是否忽略,忽略则正常执行,不忽略就抛异常,结束-->
<testFailureIgnore>true</testFailureIgnore>
<argLine>
${surefireArgLine}
</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
</configuration>
</plugin>
maven-surefire-plugin是用来执行单元测试的,可以定义我们要执行的测试类,支持通配符,和正则,
默认规则:
**/Test*.java
**/*Test.java
**/*TestCase.java
具体详细的介绍可以看: https://www.cnblogs.com/pixy/p/4718176.html
覆盖率为0的情况: 自动化配置四 Jenkins配置sonar 配置多模块覆盖率为0问题
如果是多个模块,可以把这些依赖放到父pom里,然后在运行的使用使用命令来执行:
子模块虽然继承了插件但是在父目录打包的时候子模块不会执行绑定生命周期的命令
mvn clean package checkstyle:checkstyle findbugs:findbugs pmd:pmd org.jacoco:jacoco-maven-plugin:prepare-agent -Dmaven.test.failure.ignore=true
在使用这些命令的时候使用 -Dmaven.test.failure.ignore=true忽略单元测试失败问题
配置后的效果,可以在site下面,打开index.html查看报表显示。
如果需要配置sonar的:
5.执行指定版本
最近碰到一种情况,给一个不是自己搭的项目做代码质量扫描,jenkins 配置的maven 是3.1,自己本身用的是3.6 然后想findbugs使用3.0.1会报错:
findbugs for parameter pluginArtifacts: Cannot assign configuration entry ‘pluginArtifacts’ with value ‘${plugin.artifacts}’ of type java.util.Collections.UnmodifiableRandomAccessList to property of type java.util.ArrayList
这时版本和maven版本不匹配造成的,
因为是公司项目,只做本地测试,就只在本地升级了findbugs的版本为3.0.5,解决了这个问题。
那么还有一种情况:
父级pom配置的是findbugs:3.0.5,但是在执行的时候回出现某些模块是findbugs:3.0.1,全局搜索找不到哪里还有引用,头疼,碰到这种问题,就指定版本去执行,没有解决根源问题
mvn org.codehaus.mojo:findbugs-maven-plugin:3.0.5:findbugs
同样的其他插件也可以这样指定版本。