1.maven配置jacoco、checkstyle、findbugs、pmd
这几个工具的用途:
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>
2.配置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插件列表里执行。
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>
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的: