在 jenkins 构建作业之后,SonarQube 中的测试覆盖率始终为 null

Test coverage is always null in SonarQube after jenkins build job

我正在使用最新的 SonarQube 6.4、Jenkins 2.60.1,并且我有一个应用程序进行了一些测试。 运行 在 Eclipse 中使用覆盖率插件的应用程序后,我可以看到大约 90% 的测试覆盖率。当我使用 Jenkins 构建我的应用程序然后 SonarQube 显示结果但测试覆盖率始终为空时,我的问题就开始了。我相信我遗漏了一些东西,因此我们将不胜感激。

这是我的 pom.xml 文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <sonar.host.url>http://localhost:9000</sonar.host.url>
    <sonar.sources>src/main</sonar.sources>
    <sonar.tests>src/test</sonar.tests>
    <!-- Below property indicates the pattern of the test suite -->
    <runSuite>**/*Suite.class</runSuite>
    <!-- Sonar-JaCoCo properties -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
    <sonar.language>java</sonar.language>
    <jacoco.out.path>${session.executionRootDirectory}/target</jacoco.out.path>
    <sonar.jacoco.itReportPath>${env.WORKSPACE}/target/${jacoco.out.file}</sonar.jacoco.itReportPath>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.dbunit/dbunit -->
    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.5.3</version>
    </dependency>


    <dependency>
        <groupId>com.github.springtestdbunit</groupId>
        <artifactId>spring-test-dbunit</artifactId>
        <version>1.3.0</version>
    </dependency>

    <!-- Adds Tomcat and Spring MVC, along others, jackson-databind included 
        transitively -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.jayway.restassured/spring-mock-mvc -->
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>spring-mock-mvc</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.lordofthejars/nosqlunit-core -->
    <dependency>
        <groupId>com.lordofthejars</groupId>
        <artifactId>nosqlunit-core</artifactId>
        <version>1.0.0-rc.5</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <reportsDirectory>${project.build.directory}/target/surefire-reports</reportsDirectory>
                <argLine>${jacoco.agent.argLine}</argLine>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <argLine>${jacoco.agent.arg}</argLine>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Below plugin ensures the execution of test cases during maven build -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>${runSuite}</include>
                </includes>
            </configuration>
        </plugin>
        <!-- Sonar-JaCoCo integration plugin -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <configuration>
                <destFile>${sonar.jacoco.reportPaths}</destFile>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

这是 Post 步骤中我的 Jenkins 属性(使用执行 SonarQube 扫描仪):

    sonar.jdbc.dialect=mssql
    sonar.projectKey=BillApplication
    sonar.projectName=BillApplication
    sonar.projectVersion=1.0
    sonar.projectBaseDir=.
    sonar.sources=.
    sonar.binaries=target/classes
    sonar.dynamicAnalysis=reuseReports
    sonar.junit.reportsPath=build/test-reports/jacoco.exec
    sonar.java.coveragePlugin=jacoco
    sonar.core.codeCoveragePlugin=jacoco
    sonar.junit.reportsPath=target/surefire-reports
    sonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec

最后我使用的命令是这样的:

    clean org.jacoco:jacoco-maven-plugin:prepare-agent install -P 
    integration-tests --fail-at-end

如果您需要我的配置的任何其他内容,我会post在这个地方。

post 构建操作已弃用。

您的项目是 Maven 项目,因此无需显式提供属性。

As described in the docs,在你的构建命令之后你真正需要做的是:$SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL