Maven配置sonarqube扫描插件

Maven配置sonarqube扫描插件

sonarqube是一款静态的代码扫描工具,在项目编码过程中能有效保证代码质量。在idea中,可以使用sonarlint实现实时的代码分析。同时,对于使用maven构建的项目,也可以使用其提供的maven插件实现代码扫描并生成报告。这里简单介绍如何在maven项目中配置sonarqube插件实现代码扫描和单元测试代码覆盖率分析。

准备测试项目

新建maven项目,添加service类和对应的测试类,编写测试代码

  • 项目结构
pom.xml
src
  ├─main
  │  ├─java
  │  │  └─com
  │  │      └─sonar
  │  │          └─service
  │  │                  SonarTestService.java
  │  └─resources
  └─test
      ├─java
      │  └─com
      │      └─sonar
      │          └─service
      │                  SonarTestServiceTest.java
      └─resources
  • SonarTestService
public class SonarTestService {
    public int[] sort(int[] nums, String type) {
        if (type == null) {
            return new int[]{};
        } else if ("M".equalsIgnoreCase(type)) {
            // 冒泡排序
            for (int i = 0; i < nums.length - 1; i++) {
                for (int j = 0; j < nums.length - 1 - i; j++) {
                    if (nums[j] > nums[j + 1]) {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
        } else if ("S".equalsIgnoreCase(type)) {
            // 选择排序
            for (int i = 0; i < nums.length - 1; i++) {
                int minIndex = i;
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] < nums[minIndex]) {
                        minIndex = j;
                    }
                }
                int temp = nums[i];
                nums[i] = nums[minIndex];
                nums[minIndex] = temp;
            }
        }
        return nums;
    }
}
  • SonarTestServiceTest
public class SonarTestServiceTest {
        int[] nums = {1, 2, 3, 7, 8, 9, 10, 4, 5, 6, 78};

    @Test
    public void testSortNull() {
        SonarTestService sonarTestService = new SonarTestService();
        int[] result = sonarTestService.sort(nums, null);
        assertEquals(0, result.length);
    }

    @Test
    public void testSortM() {
        SonarTestService sonarTestService = new SonarTestService();
        int[] result = sonarTestService.sort(nums, "M");
        assertNotNull(result);
    }

    @Test
    public void testSortS() {
        SonarTestService sonarTestService = new SonarTestService();
        int[] result = sonarTestService.sort(nums, "S");
        assertNotNull(result);
    }
}
  • pom.xml

<properties>
    <java.version>1.8</java.version>
    <sonar.java.source>8</sonar.java.source>
    <sonar.host.url>sonarqube服务器地址</sonar.host.url>
    <sonar.login>sonarqube服务器登录用户</sonar.login>
    <sonar.password>sonarqube服务器登录密码</sonar.password>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>4.0.0.4121</version>
        </plugin>
    </plugins>
</build>

这里关于sonar服务器的配置,也可以通过配置访问token实现。

执行扫描

完成以上配置之后,在idea中打开maven工具栏,运行 Plugins - sonar - sonar:sonar 执行sonarqube代码扫描,完成后会自动在sonarserver上创建新的项目并生成报告。同时,在项目根路径或target目录下会生成sonar扫描结果,其中也包含pdf扫描报告

配置runner

根据官网说明,最新的sonar扫描插件已经不支持jdk1.8,使用时需要升级jdk11执行扫描。这里如果项目使用jdk1.8开发,可以通过配置maven runner实现扫描

依次点击 File | Settings | Build, Execution, Deployment | Build Tools | Maven | Runner 打开maven runner配置页面,在jre中选择jdk11。如果没有,则需要安装并导入到项目SDKs中。

完成jdk11的runner配置后,可实现sonarqube代码扫描。

配置单元测试扫描

在默认的sonar扫描插件中,是不提供单元测试的执行和覆盖率测试的,sonar扫描插件只负责收集覆盖路数据并提供展示。要实现单元测试覆盖率信息显示,需要配合其他插件一起使用。

单元测试插件

在pom中提供如下插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <includes>
            <include>**/*Test.java</include>
        </includes>
    </configuration>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>initialize</phase>
        </execution>
        <execution>
            <id>report</id>
            <goals>
                <goal>report</goal>
            </goals>
            <phase>test</phase>
        </execution>
    </executions>
    <configuration>
        <destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
        <dataFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</dataFile>
    </configuration>
</plugin>
  • maven-surefire-plugin

主要负责执行单元测试用例并提供测试结果,包括成功、失败、跳过等信息,负责生成测试报告。

  • jacoco-maven-plugin

主要负责收集测试数据,收集测试过程中的覆盖率数据,生成 .exec 文件。根据单元测试的执行结果,生成最终的覆盖率信息。

扫描报告

在完成以上配置之后,执行 mvn clean test sonar:sonar 即可完成完整的sonarqube扫描。在执行完之后,登录pom中配置的sonar服务器,可查看生成的扫描报告。

类似文章