1.在pom.xml中的配置
1.1 指定sonar地址
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.host.url>http://sonar.puhuitech.cn/</sonar.host.url>
</properties>
1.2添加jacoco依賴
<dependency>
<groupId>org.sonarsource.java</groupId>
<artifactId>sonar-jacoco-listeners</artifactId>
<version>3.8</version>
<scope>test</scope>
</dependency>
1.3添加profile支持jacoco的插件
<profiles>
<profile>
<id>coverage-per-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>org.sonar.java.jacoco.JUnitListener</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
2.在工程目錄下運行測試並分析覆蓋率
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test
3.代碼掃描上傳到sonar服務器
mvn sonar:sonar