maven jacoco插件不会生成覆盖率报告

时间:2016-03-24 11:33:46

标签: java maven junit sonarqube jacoco-maven-plugin

我使用sonarqube作为测试结果的输出,而maven和jacoco则用于测试测试用例。

Sonarqube版本是5.4 Maven版本是3.3.9 Jacoco版本0.7

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.sonarqube</groupId>
  <artifactId>example-ut-maven-jacoco</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Java :: UT Coverage with JaCoCo :: Maven</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- Minimal supported version is 4.7 -->
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
         <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.15</version>
        <configuration>
            <!-- Sets the VM argument line used when unit tests are run. -->
            <argLine>${surefireArgLine}</argLine>
            <!-- Skips unit tests if the value of skip.unit.tests property is true -->
            <skipTests>${skip.unit.tests}</skipTests> 
        </configuration>
    </plugin>
    </plugins>
  </build>



</project>

我也关注此link here但是对于我使用的文件enter link description here

这是我如何构建测试项目

mvn clean test
mvn clean verify
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install
mvn sonar:sonar

buiild返回成功但没有生成代码覆盖率我也通过去sonarqube localhost检查了这个:9000 /它显示0.00%的代码覆盖率但是单元测试都显示为成功&#34;它是成功的&#34 ;

有什么可能导致这个问题的想法吗?此外,我还是新手。

1 个答案:

答案 0 :(得分:2)

从我的角度来看,设置看起来还不错。

您已更改报告文件的默认名称。因此,我猜你也需要告诉声纳分析仪。

单元测试的jacoco默认值为:${project.build.directory}/jacoco.exec 那也是声纳试图拿起的名字。

您只需设置一个属性即可指示声纳分析仪的位置:

<properties
    <sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</sonar.jacoco.reportPath>
    ...

请验证您的jacco文件是否存在且非空 - 如果是这样,声纳将在配置正确后立即将其取出。