maven配置JaCoCo
- jacoco 文檔 https://www.eclemma.org/jacoco/trunk/doc/maven.html
- https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html
jacoco支持對單元測試的結果生產覆蓋率報告。下面看在項目中配置。
pom.xml 文件添加如下 jacoco插件引用。
```
其中包含(includes)或排除(excludes)字段的值應該是相對於目錄/ classes /的編譯類的類路徑(而不是包名),使用標准通配符語法:
排除規則
- Match zero or more characters
** Match zero or more directories
? Match a single character
com/ / //可以設置只分析某部分對代碼com/src/
生成jacoco覆蓋率文件
在執行mvn命令時,加上“org.jacoco:jacoco-maven-plugin:prepare-agent”參數即可。 示例:
mvn clean test org.jacoco:jacoco-maven-plugin:0.8.3:prepare-agent install -Dmaven.test.failure.ignore=true
其中,jacoco-maven-plugin后面跟的是jacoco的版本; 【-Dmaven.test.failure.ignore=true】建議加上,否則如果單元測試失敗,就會直接中斷,不會產生.exec文件
生產jacoco.exec文件 命令
mvn clean test org.jacoco:jacoco-maven-plugin:0.8.3:prepare-agent install -Dmaven.test.failure.ignore=true
注:當與 jenkins集成時,也是依賴項目中當exec文件生成覆蓋率報告。
生成覆蓋率文件 html (必須前一步執行成功,有jacoco.exec文件) 打開上面的html 文件
mvn org.jacoco:jacoco-maven-plugin:0.8.3:reportQ1: 執行時 jacoco 覆蓋率文件沒有生成。
Skipping JaCoCo execution due to missing execution data file.
原因是 jacoco 插件適用了參數 argline.和 maven-surefire-plugin argLine沖突。
解決1: 刪除maven-surefire-plugin插件下的 argLine配置。
解決2: maven-surefire-plugin插件下的 argLine配置.放在全局位置。