cobertura在maven多模块项目上

pan*_*atz 30 maven-2 cobertura

我有一个包含4个模块的Maven项目 - 其中3个包含代码和一些测试(测试等于和类的哈希码),而第4个模块用于测试其他3个模块.

现在我想运行cobertura代码覆盖率工具来概述哪些类经过了充分测试,哪些不是.我对该主题进行了一些调查,如果测试的某些源位于其他模块中,则cobertura似乎不知道生成正确的代码覆盖百分比和线路覆盖率.

我已经阅读了一些链接,如SeamT​​estCoverageWithCobertura在多模块Maven 2中使用插件Coverage,但必须有一个开箱即用的解决方案.有人可以报告一些关于这个主题的新方向吗?还是有像cobertura这样的工具?我偶然发现了艾玛,但这个工具不提供线路覆盖......

Mar*_*tin 19

从版本2.6开始,有一个聚合选项可以在父pom中设置为true:

<reporting>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <outputDirectory>./target/tmpCobertura</outputDirectory>
        <formats>
            <format>html</format>
        </formats>
        <aggregate>true</aggregate>
    </configuration>
  </plugin>
</plugins>
</reporting>
Run Code Online (Sandbox Code Playgroud)

  • 这也可以通过命令行完成:`mvn cobertura:cobertura -Dcobertura.aggregate = true -Dcobertura.report.format = xml`您可以根据需要更改报告格式.根据cobertura maven插件的github repo,这个功能可用[自v2.5起](https://github.com/mojohaus/cobertura-maven-plugin/blob/master/src/main/java/org/codehaus/ mojo/cobertura/CoberturaReportMojo.java#L126)(commit [64a8823](https://github.com/mojohaus/cobertura-maven-plugin/commit/64a8823866b4c8be74a44383162088d616c65185#diff-e4171be1b77f9a9b331e21a0661c1433R126)). (3认同)
  • 恕我直言,这应该成为公认的答案,因为接受的答案中提到的两个问题都是固定的,因为2.5. (2认同)

Sve*_*ann 9

我们现在没有声纳,我们无法安装它.所以我必须找到一个解决方法并得到一个.此解决方案适用mvn clean install -DrunCobertura=true于多模块项目中的简单解决方案.您只需要将此配置文件添加到super pom.xml项目中,定义working.dir属性即可.

<profile>
    <id>runCobertura</id>
    <activation>
        <property>
            <name>runCobertura</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <cobertura.format>html</cobertura.format>
        <cobertura.working.dir>${working.dir}/${project.version}/cobertura</cobertura.working.dir>
        <cobertura.complete.ser.file>${cobertura.working.dir}/complete.ser</cobertura.complete.ser.file>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.4.1</version>
                <inherited>false</inherited>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>.</directory>
                            <includes>
                                <include>cobertura.ser</include>
                            </includes>
                        </fileset>
                        <fileset>
                                <directory>${cobertura.working.dir}</directory>
                            </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>cobertura-Instrument</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${project.build.outputDirectory}"/>
                                    <then>
                                        <cobertura-instrument>
                                            <fileset dir="${project.build.outputDirectory}">
                                                <include name="**/*.class"/>
                                            </fileset>
                                        </cobertura-instrument>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cobertura-createCombinedSerFile</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${cobertura.complete.ser.file}"/>
                                    <then>
                                        <cobertura-merge datafile="${basedir}/tmp.ser">
                                            <fileset file="${cobertura.complete.ser.file}"/>
                                            <fileset file="${basedir}/cobertura.ser"/>
                                        </cobertura-merge>
                                        <move file="${basedir}/tmp.ser" tofile="${basedir}/cobertura.ser"/>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cobertura-copyResultSerFileAndSources</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <taskdef resource="tasks.properties"/>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="${basedir}/cobertura.ser"/>
                                    <then>
                                        <move file="${basedir}/cobertura.ser" tofile="${cobertura.complete.ser.file}"/>
                                        <mkdir dir="${cobertura.working.dir}/source"/>
                                        <if>
                                            <available file="${basedir}/src/main/java"/>
                                            <then>
                                                <copy todir="${cobertura.working.dir}/source">
                                                    <fileset dir="src/main/java">
                                                        <include name="**/*.java"/>
                                                    </fileset>
                                                </copy>
                                            </then>
                                        </if>
                                        <cobertura-report datafile="${cobertura.complete.ser.file}" format="${cobertura.format}" destdir="${cobertura.working.dir}/report">
                                            <fileset dir="${cobertura.working.dir}/source"/>
                                        </cobertura-report>
                                    </then>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>net.sourceforge.cobertura</groupId>
                        <artifactId>cobertura</artifactId>
                        <version>1.9.4.1</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.cobertura</groupId>
            <artifactId>cobertura</artifactId>
            <version>1.9.4.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</profile>
Run Code Online (Sandbox Code Playgroud)

它有什么作用:

1. @process-classes -Instrument模块的已编译类.

2. 使用此模块中创建的模块从以前的模块中@generate-test-sources获取.ser文件,以获得完整的代码覆盖率.

3. @test创建代码覆盖率报告.应该在最后一个模块中调用,但由于最后一个模块可以更改,我总是调用它,之前的报告将被覆盖.如果您使用xml格式的报告(对于Jenkins)它很快,所以没关系.


Pas*_*ent 8

根据MCOBERTURA-65,maven cobertura插件仍然不知道如何将子模块的报告汇总成一个统一的.已经完成了一些工作来实现mergemaven cobertura插件的目标(参见MCOBERTURA-33),但此代码尚未包含在插件中.我自己没有测试补丁,也不能说是否值得一试.

因此,很多人确实建议使用maven 仪表板插件,但我个人远离它,因为从长远来看它不是很令人满意,我遇到了很多问题(技术问题,历史遗失) ,...).相反,我热烈推荐Sonar.看看最新版Sonar的公共实例Nemo,以获得此工具的现场演示.例如,参见Commons Digester项目和代码覆盖率深入分析.

  • 声纳不会显示前三个模块中的代码被第四个模块中的代码覆盖.它只是汇总了4个完全独立且不完整的报告. (8认同)