【问题标题】:Maven cobertura plugin - one report for multimodule projectMaven cobertura 插件 - 多模块项目的一份报告
【发布时间】:2011-04-15 16:10:45
【问题描述】:

我正在使用 maven cobertura 插件来报告我的多模块项目中的代码覆盖率。

问题是我不知道如何为项目中的所有模块生成一份报告。

到目前为止,我已经为每个模块生成了单独的报告,但是为整个项目提供一份报告会更好。

我的父 pom 配置:

   <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
        <inherited>true</inherited>
        <executions>
            <execution>
                <phase>test-compile</phase>
                <goals>
                    <goal>clean</goal>
                    <goal>cobertura</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

【问题讨论】:

    标签: maven-2 cobertura


    【解决方案1】:

    自从提出(并最后回答)这个问题以来,插件已经更新,现在可以通过父 POM 中的 aggregate 配置属性启用聚合报告。

    这会在target/site/cobertura/index.html 生成汇总覆盖率报告,其中将包括所有模块。

    (如果有任何用处,每个模块也会生成自己的报告。)

    父 pom.xml

    <modules>
        <module>moduleA</module>
        <module>moduleB</module>
        <module>moduleC</module>
    <modules>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <check/>
                        <formats>
                            <format>html</format>
                            <format>xml</format>
                        </formats>
                        <aggregate>true</aggregate>
                    </configuration>
                </plugin>
                ...
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
            </plugin>
        </plugins>
    ...
    </build>
    

    【讨论】:

    • 我应该在父模块上运行mvn cobertura:cobertura -Dcobertura.report.format=xml commmand,它会在父模块中生成汇总报告吗?
    • 我在父包上运行了mvn cobertura:cobertura -Dcobertura.report.format=xml 命令,它聚合了子模块的报告,但它不显示该父模块的子 maven 模块中的类的覆盖范围,如果该类被来自此父模块的不同子 maven 模块。
    • 听起来你有一个模块测试另一个模块的类。这听起来不是一个好主意。一个模块应该包含它自己的类的测试。如果不是这种情况,您应该提交一个单独的问题。
    • 这也可以通过不编辑 pom 文件来完成:mvn cobertura:cobertura -Dcobertura.aggregate=true -Dcobertura.report.format=xml 您可以根据需要更改报告格式。根据 cobertura maven 插件的 github repo,此功能可用 since v2.5 (commit 64a8823)。
    • 您好,是否可以将模块结果分组到单个报告中?可能在模块名称下。
    【解决方案2】:

    据我所知,目前不支持此功能,请参阅 MCOBERTURA-65。但是这个问题有一个补丁,也许可以试试。但我的建议是使用 Sonar 之类的东西来汇总指标。

    【讨论】:

    • 现在支持。看我的回答。
    【解决方案3】:

    我一直使用 Hudson 作为持续集成工具。 Cobertura 插件允许我在检查父模块时查看所有子模块的代码覆盖率。

    【讨论】:

      【解决方案4】:

      Jenkins Cobertura 插件会自动汇总报告,但如果您出于其他原因对覆盖文件本身感兴趣,您可以按照以下步骤进行操作:

      1. here下载Cobertura

      2. 转到您的项目工作区 -> 找到所有 .ser 文件并重命名它们

        (i=0; find . | grep cobertura.ser$ | while read line;do echo $line; cp -i $line cobertura$i.ser;i=$(($i+1));done;)
        
      3. 使用cobertura-merge.sh生成全局.ser文件

        ~/cobertura-2.0.3/cobertura-merge.sh --datafile cobertura.ser cobertura*.ser
        
      4. 使用cobertura-report.sh 生成关于全局 .ser 文件的报告

        ~/cobertura-2.0.3/cobertura-report.sh ./cobertura.ser --destination ./ --format xml
        

      您将在当前目录中生成全局coverage.xml。 您可以将其用于进一步的任何类型的处理。

      【讨论】:

      • 这也可以通过命令行仅使用 maven 完成:mvn cobertura:cobertura -Dcobertura.aggregate=true -Dcobertura.report.format=xml 您可以根据需要更改报告格式。根据 cobertura maven 插件的 github repo,此功能可用since v2.5(提交64a8823)。
      猜你喜欢
      • 2010-11-28
      • 1970-01-01
      • 2014-12-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      相关资源
      最近更新 更多