【问题标题】:Integration sonar with jacoco. I see excluded packages in sonar coverage report与 jacoco 集成声纳。我在声纳覆盖报告中看到排除的包
【发布时间】:2016-09-22 15:54:02
【问题描述】:

我想从 cobertura 迁移到 jacoco 代码覆盖工具。 我使用 maven 作为构建工具,并在我的 pom.xml 文件中添加了 jococo-maven-plugin。它的配置如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.2.201409121644</version>
    <configuration>
        <append>true</append>
        <outputEncoding>UTF-8</outputEncoding>
        <destFile>${sonar.jacoco.reportPath}</destFile>
        <dataFile>${sonar.jacoco.reportPath}</dataFile>
        <outputDirectory>${jacoco.output.path}</outputDirectory>
        <excludes>
            <exclude>com/acmecorp/acmeproject/mbean/**/*.class</exclude>
            <exclude>com/acmecorp/acmeproject/model/**/*.class</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

所以在执行mvn clean install 之后,jacoco 为我生成了一些带有代码覆盖率的漂亮 html 页面。这份文件看起来正是我想要的样子。

我需要执行的第二步是将 jococo 与声纳集成。以前我将声纳与 cobertura 集成。所以我简单地删除了 cobertura 的特定属性,并添加了与 jococo 相关的属性。

<properties>
    <!--<sonar.cobertura.reportPath>target/site/cobertura/coverage.xml</sonar.cobertura.reportPath>-->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.projectKey>${project.artifactId}</sonar.projectKey>
    <sonar.projectName>ACME Project</sonar.projectName>
    <sonar.projectVersion>${project.version}</sonar.projectVersion>
    <sonar.java.source>${java.compliance.level}</sonar.java.source>
    <sonar.sources>src/main/java</sonar.sources>
    <sonar.jdbc.url>jdbc:mysql://4.4.8.8:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true&amp;useConfigs=maxPerformance</sonar.jdbc.url>
    <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
    <sonar.jdbc.username>ranos</sonar.jdbc.username>
    <sonar.jdbc.password>ranos</sonar.jdbc.password>
    <sonar.host.url>http://4.4.8.8:9000/sonar/</sonar.host.url>
    <sonar.jacoco.reportPath>target/sites/jacoco/jacoco.exec</sonar.jacoco.reportPath>
    <jacoco.output.path>target/sites/jacoco/</jacoco.output.path>
</properties>

Maven 插件

<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.0.1</version>
</plugin>

和个人资料

<profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

结果我在服务器上得到了这样的代码覆盖率:

我看到在服务器上生成的覆盖率报告中,我看到我的排除包包含在报告中。当我使用 cobertura 时,一切正常。

所以可能有人可以帮助我解决这个问题。我不想为声纳配置添加排除项。我真的希望它可以是 jococo 插件的一些额外配置。

更新

如果我将 cobertura maven-plugin 与声纳一起使用 (我的意思是,如果我评论这一行 &lt;sonar.jacoco.reportPath&gt;target/sites/jacoco/jacoco.exec&lt;/sonar.jacoco.reportPath&gt; 并取消评论这一行 &lt;sonar.cobertura.reportPath&gt;target/site/cobertura/coverage.xml&lt;/sonar.cobertura.reportPath&gt;

【问题讨论】:

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


    【解决方案1】:

    排除项不需要发生在 JaCoCo 端,而是发生在 SonarQube 端,并通过 SonarQube UI 而不是在您的 POM 中指定。

    文档将为您提供详细信息:http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreCodeCoverage

    【讨论】:

    • 嗨,安,但如果我只更改 sonar-maven 插件中的一行(取消注释 cobertura 覆盖率报告),那么声纳报告看起来很完美。检查我的问题的更新部分
    【解决方案2】:

    将要排除的包放入 maven &lt;properties&gt; 块中的 pom.xml

    <properties>
      <sonar.jacoco.excludes>*model*,*generated*,*exception*</sonar.jacoco.excludes>
    </properties>

    如果还是不行,试试把属性名改成sonar.coverage.exclusions,我觉得后面的版本改了。

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2015-05-15
      • 2013-09-26
      • 2016-11-15
      • 2014-04-09
      • 2013-10-14
      相关资源
      最近更新 更多