最近我在工作中遇到需要給項目中每個模塊配置Sonarqube,來看看測試用例覆蓋的情況。在這個過程中遇到了一些問題,也查了很多資料。現在記錄一下具體應該怎么配置。
先展示一下實現的效果:
圖一
It will show:
圖二
現在來看看具體配置:
要enable Sonarqube, 需在 job --> configure --> Post-build Actions 配置Branch 和 JDK 信息:
圖三
配置好了以后,SonarQube 可以在頁面上顯示出來了,點擊進去,效果圖:【與圖二相比,缺少了Coverage信息】
圖四
如何利用cobertura插件實現sonar頁面顯示測試覆蓋率:
需要在pom.xml文件中配置一下信息:
<plugin>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-maven3-plugin</artifactId>
<version>3.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
<aggregate>true</aggregate>
<instrumentation>
<includes>
<include>**/*.class</include>
</includes>
</instrumentation>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
並在job --> configure --> Build 配置 Goals and options:
圖五
保存配置,重新build這個job,就行了。