【问题标题】:What is the proper way to use Cobertura with Maven 3.0.2在 Maven 3.0.2 中使用 Cobertura 的正确方法是什么
【发布时间】:2011-10-19 08:52:24
【问题描述】:

过去几天我一直在尝试使用 Maven 3.0.2 运行 Cobertura 2.4,但没有成功。我们有一个非常大的项目,其中包含许多模块(子项目)。 我发现文档基本上不存在或完全错误。我能找到的所有教程都不适用于 Maven 3.x(它们可以构建,但 Cobertura 要么不运行,要么无法生成报告)。

这里有人能够使它工作吗?任何有用的提示/示例? 谢谢。

【问题讨论】:

    标签: maven maven-3 cobertura


    【解决方案1】:

    我通过添加以下内容成功地将 Cobertura 集成到我的项目中:

      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <instrumentation>
                <includes>
                  <include>foo/bar/**/*.class</include>
                </includes>
              </instrumentation>
            </configuration>
        <executions>
          <execution>
            <id>clean</id>
            <phase>pre-site</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
          <execution>
            <id>instrument</id>
            <phase>site</phase>
            <goals>
              <goal>instrument</goal>
              <goal>cobertura</goal>
            </goals>
          </execution>
        </executions>
          </plugin>
        </plugins>
      </build>
    
      <reporting>
        <plugins>
          <plugin>
        <!-- use mvn cobertura:cobertura to generate cobertura reports -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <formats>
            <format>html</format>
            <format>xml</format>
          </formats>
         </configuration>
          </plugin>
        </plugins>
      </reporting>
    

    如果您运行mvn cobertura:cobertura,报告将在target\site\cobertura 中生成。另见maven cobertura plugin


    今天我用SonarQube分析项目。它有一个简单的installation 步骤(如果您对使用企业数据库不感兴趣),还包括代码覆盖率分析(使用JaCoCo)以及许多其他指标。

    【讨论】:

    • 是的,声纳是要走的路。无需更改您的项目 POM
    • Sonar 很棒,但能够在没有完整的 Sonar 分析的情况下生成覆盖率报告也很不错。
    • @Ryan Stewart 因为 Sonar 为您提供了您从未想知道的产品信息 :-)?
    • 我们正在使用声纳,但它每晚运行。我正在寻找的是关于如何将其集成以供日常使用的一般指南。到目前为止,据我所知,最好的方法基本上是什么都不做,然后运行mvn cobertura:cobertura 来生成它。到目前为止,您的回答是最好的。
    • @mattfreake 同时,SonarQube 更改了覆盖工具,现在使用 Jacoco。您应该会在项目仪表板中看到结果。
    【解决方案2】:

    在 maven 3.0.3 中(当你问这个问题时还没有出来),只需使用 maven 的站点插件并对其进行配置,使其使用 cobertura,如下所示:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <version>2.5.1</version>
                            <configuration>
                                <formats>
                                    <format>html</format>
                                    <format>xml</format>
                                </formats>
                            </configuration>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
    ....
    

    【讨论】:

    • 我们如何通过 maven 中的 pluginExecution 来启动它?有什么猜测吗?
    【解决方案3】:

    您还可以将 Cobertura 插件集成到您的 web 应用程序的 &lt;reporting&gt; 部分:

      <reporting>
        <outputDirectory>${project.build.directory}/site</outputDirectory>
        <plugins>
          <!-- Maven Project Info Reports Plugin -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>2.7</version>
            <configuration>
              <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
            </configuration>
          </plugin>
          <!-- Cobertura Code Coverage Plugin -->
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.6</version>
            <configuration>
              <instrumentation>
                <ignoreTrivial>true</ignoreTrivial>
              </instrumentation>
              <formats>
                <format>html</format>
                <format>xml</format>
              </formats>
            </configuration>
          </plugin>
        </plugins>
      </reporting>
    

    如果您运行mvn site,那么您的报告将在应用程序目标目录中的target/site/cobertura/index.html 中可用。

    【讨论】:

      猜你喜欢
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2018-12-20
      相关资源
      最近更新 更多