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的: