【问题标题】:Maven2 Multiproject Cobertura Reporting Problems During mvn site BuildMaven 多项目 Cobertura 在 mvn 站点构建期间报告问题
【发布时间】:2008-09-18 17:13:55
【问题描述】:

作为 mvn 站点构建的一部分,我们正在尝试运行 Cobertura 测试覆盖率报告的多项目。我可以让 Cobertura 在子项目上运行,但它错误地报告 0% 的覆盖率,即使报告仍然突出显示单元测试命中的代码行。

我们正在使用 mvn 2.0.8。我试过运行mvn clean sitemvn clean site:stagemvn clean package site。我知道测试正在运行,它们出现在万无一失的报告中(txt/xml 和站点报告)。我在配置中遗漏了什么吗? Cobertura 是否不适用于多项目?

这是在父 .pom 中:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>clean</id>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <inherited>true</inherited>
        </plugin>
    </plugins>
</reporting>

我尝试在子 .poms 中使用和不使用以下内容运行它:

    <reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

我在构建的输出中得到了这个:

...
[INFO] [cobertura:instrument]
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 3 files to C:\workspaces\sandbox\CommonJsf\target\generated-classes\cobertura
Cobertura: Saved information on 3 classes.
Instrument time: 186ms

[INFO] Instrumentation was successful.
...
[INFO] Generating "Cobertura Test Coverage" report.
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 3 classes.
Report time: 481ms

[INFO] Cobertura Report generation was successful.

报告如下所示:

【问题讨论】:

  • 伙计,你需要一台新显示器!我几乎看不懂那些类名。

标签: maven-2 code-coverage maven-plugin cobertura


【解决方案1】:

我怀疑您在编译阶段缺少 cobertura 插件的执行,因此代码仅在运行测试后在站点生命周期中由报告插件检测。所以测试运行没有被选中,因为它们运行在非仪器代码上。更仔细地分析您的构建日志 - 如果我是对的,您会注意到肯定会在 cobertura:instrument 之前执行测试。

我的配置和你的类似,但是除了在 pluginManagement 中指定干净的执行(像你一样),我在 build plugins 部分明确指定了 cobertura 插件:

  <build>
  ...
    <plugins>
    ...
      <plugin>
        <inherited>true</inherited>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>${cobertura.plugin.version}</version>
      </plugin>
    </plugins>
  </build>

我的配置工作正常,所有 Cobertura 的东西都在全球组织范围的 pom 中,所有项目都将其用作父级。

这样项目不会在其 pom.xml 中指定任何与 Cobertura 相关的内容,但它们仍会生成覆盖率报告。

【讨论】:

    【解决方案2】:

    我没有成功让 Cobertura 合并来自多个项目的报告。这一直是多项目报告的普遍问题。

    我们一直在评估 sonar 作为我们指标报告的解决方案。它似乎在提供跨项目(包括多项目)的摘要指标方面做得很好。

    【讨论】:

      【解决方案3】:

      我实施的解决方案有点手动,但有效。它由几个步骤组成,其中一个是合并由 Cobertura 生成的几个 .ser 文件的步骤。这可以通过在 maven 任务中使用 cobertura-merge 命令行工具来完成。

      根据您显示的输出是文件实际上没有被检测,它告诉只有 3 个文件被检测。

      【讨论】:

      【解决方案4】:

      @Marco 是对的,仅通过 maven 无法正常实现这一点,因为 maven cobertura 插件缺少合并目标。

      您可以通过混合使用 maven 和 ant 目标来实现它:http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/

      不过,如果您有一个待测项目,则无需合并。您可以在测试项目中,从被测项目中复制 .ser 文件和检测类:

      //in test project
      <plugin> 
      <groupId>com.github.goldin</groupId>
      <artifactId>copy-maven-plugin</artifactId>
      <version>0.2.5</version>
      <executions>
          <execution>
          <id>copy-cobertura-data-from-project-under-test</id>
          <phase>compile</phase>
          <goals>
              <goal>copy</goal>
          </goals>
          <configuration>
          <resources>
              <resource>
                              <directory>${project.basedir}/../<project-under-test>/target/cobertura</directory>
                                  <targetPath>${project.basedir}/target/cobertura</targetPath>
                      <includes>                  
                                    <include>*.ser</include>
                      </includes>
                 </resource>
                 <resource>
                          <directory>${project.basedir}/../<project-under-test>/target/generated-classes/cobertura/</directory>
                          <targetPath>${project.basedir}/target/generated-classes/cobertura</targetPath>
                          <preservePath>true</preservePath>
                 </resource>
              </resources>
                  </configuration>
              </execution>
      </executions>
      </plugin>
      
      //in parent project
      <build>
      <plugins>
          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
          <configuration>
              <format>xml</format>
              <aggregate>true</aggregate>
          </configuration>
          <executions>
              <execution>
                          <goals>
                      <goal>clean</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
          </plugins>
      </build>
      <reporting>
      <plugins>
          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
          <version>${cobertura.version}</version>
              </plugin>
      </plugins>
       </reporting>
      

      【讨论】:

        猜你喜欢
        • 2011-04-15
        • 2010-10-10
        • 1970-01-01
        • 2017-01-03
        • 2013-04-27
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多