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

时间:2017-07-16 17:10:50

标签: maven jenkins sonarqube jacoco-maven-plugin

我正在使用最新的SonarQube 6.4,Jenkins 2.60.1,我有一个应用程序进行了一些测试。在使用coverage插件在eclipse中运行应用程序后,我可以看到大约90%的测试覆盖率。当我使用Jenkins构建我的应用程序然后SonarQube显示结果但测试覆盖率始终为null时,我的问题就出现了。我相信我错过了一些东西,所以任何帮助都会受到赞赏。

这是我的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 Steps中的Jenkins属性(使用Execute SonarQube Scanner):

    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

如果您需要我配置的任何其他内容,我会将其发布到这个地方。

1 个答案:

答案 0 :(得分:1)

不建议使用帖子构建操作。

你的是一个Maven项目,所以没有必要明确提供属性。

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