sonar配置和使用

本文介绍了如何在项目中配置Sonar,包括在pom.xml中指定Sonar服务器地址,添加Jacoco依赖以支持代码覆盖率,以及配置Maven profile运行测试和分析覆盖率。最后,讲述了如何执行代码扫描并将结果上传到Sonar服务器。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值