sonarqube 项目内maven插件使用
一般建议保持项目jdk和sonarqube的jdk版本呢一致,sonarqube支持的版本最低的也是jdk11,如果要使用sonarqube,建议项目尽量上jdk17。
sonar maven插件依赖
<!-- 属性配置-->
<properties>
<!--sonarqube配置-->
<sonar.projectKey>TestExample</sonar.projectKey>
<sonar.host.url>http://10.109.38.98:9000</sonar.host.url>
<!--token-->
<sonar.login>sqa_c353b5531044ea9421a00907a6b6b6fd768cea3b</sonar.login>
<!--代码覆盖率需要排除的目录-->
<sonar.coverage.exclusions>
**/advice/**,
**/annotation/**
</sonar.coverage.exclusions>
</properties>
<build>
<plugins>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.12.RELEASE</version>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中 -->
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<!--JaCoCo覆盖率测试报告生成工具-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<configuration>
<!--包含的类-->
<includes>
<!--相对src/main/java下的目录-->
<include>com/**</include>
</includes>
<!-- rules裏面指定覆蓋規則 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- 指定方法覆盖率到50% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
<!-- 指定分支覆盖到50% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
<!-- 指定类覆盖到100% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<!--jacoco生成覆盖率报告需要依赖surefire,没有它jacoco生成的报告覆盖率都会是0-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<skipTests>false</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.2.5</version>
</dependency>
</dependencies>
</plugin>
<!--sonarqube maven 插件,它的覆盖率依赖jacoco,没有jacoco就只会又静态代码扫描没有覆盖率的门禁 -->
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.11.0.3922</version>
</plugin>
</plugins>
</build>
运行sonar
mvn clean compile sonar:sonar,
或者 mvn clean verify sonar:sonar也行
覆盖率为0的几种情况
能耍的时候就一定要耍,不能耍的时候一定要学。
--天道酬勤,贵在坚持posted on 2024-09-03 11:13 zhangyukun 阅读(190) 评论(0) 编辑 收藏 举报