mvn -Dmaven.test.skip=false -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -U clean verify sonar:sonar -Dsonar.projectKey=code-diff -Dsonar.host.url=https://test-sonarqube.internal.com -Dsonar.token=sqa_37c31**** -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
下面的pom 內容需要加入到項目的pom中,否則不會上傳生成覆蓋率數(shù)據(jù)
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
需要切到根目錄下,再執(zhí)行腳本

image.png
數(shù)據(jù)將會上傳到sonarqube

image.png
測試用例 如果是junit包 需要改成
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.5</version>
<scope>test</scope>
</dependency>
不然可能會不運行測試用例,實際的測試執(zhí)行可能是0
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = CodeDiffApplication.class)
public class CodeDiffApplicationTest {
@Test
void contextLoads() {
}
}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<skip>false</skip>
<skipTests>false</skipTests>
</configuration>
</plugin>