Maven 多模块项目上的 cobertura

发布于 2024-08-04 20:57:46 字数 664 浏览 13 评论 0 原文

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

现在我想运行 cobertura 代码覆盖率工具来大致了解哪些类经过了充分测试,哪些类没有经过充分测试。我对此主题做了一些调查,如果测试的某些源位于其他模块中,cobertura 似乎不知道生成正确的代码覆盖率百分比和行覆盖率。

我读过一些链接,例如 SeamT​​estCoverageWithCobertura在多模块 Maven 中使用插件覆盖范围2 但必须有一个开箱即用的解决方案。有人可以报告有关该主题的一些新方向吗?或者还有像 cobertura 这样更好的工具吗?我偶然发现了 emma,但这个工具不提供线路覆盖......

I have a Maven project with 4 modules - 3 of them contain code and some tests (testing equals and hashcode of the classes) whereas the 4th module is for testing the 3 other modules.

Now I want to run the cobertura code coverage tool to get an overview which classes are well tested and which are not. I did some investigations on that topic and it seems that cobertura is not aware of generating the right code coverage percentages and line coverages, if some sources which are tested are located within other modules.

I have read over some links like SeamTestCoverageWithCobertura and Using the plugin Coverage within a multi-module Maven 2 but there has to be an out of the box solution. Can anybody report some new directions on this topic? Or are there bether tools like cobertura? I've stumbled upon emma but this tool does not offer line coverage...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

带上头具痛哭 2024-08-11 20:57:46

从版本 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>

As of version 2.6, there is an aggregate option that can be set to true in the parent pom:

<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>
诗酒趁年少 2024-08-11 20:57:46

我们这里没有声纳,现在无法安装。所以我必须找到一种解决方法并得到一个。此解决方案适用于多模块项目中的简单 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>

它的作用是:

1. @process-classes - 检测模块的已编译类。

2. @generate-test-sources - 将之前模块中的 .ser 文件与创建的此模块之一合并以获得完整代码覆盖范围。

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

We don't have sonar here and right now, we cant install it. So i had to find a workaround and got one. This solution works with a simple mvn clean install -DrunCobertura=true in a multi module project. You only need to add this profile to your super pom.xml of your project, define the working.dir property and it should work.

<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>

What does it do:

1. @process-classes-Instrument the compiled classes of the module.

2. @generate-test-sources-Merges the .ser file from previous modules with the created one of this module to get the complete code coverage.

3. @test-Creates the code coverage report. Should be called in the last module, but due to the fact that the last module can change, i call it always and the previous reports will be overwritten. If you use the report in xml format (for Jenkins) it's fast, so it doesn't matter.

笑看君怀她人 2024-08-11 20:57:46

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

因此,很多人确实建议使用 maven dashboard 插件,但我会个人远离它,因为从长远来看它不是很令人满意,而且我遇到了很多问题(技术问题,历史丢失,......)。相反,我热烈推荐Sonar。查看 Nemo(Sonar 最新版本的公共实例),了解该示例的现场演示工具。例如,请参阅 Commons Digester 项目和 深入了解代码覆盖率

According to MCOBERTURA-65, the maven cobertura plugin still doesn't know how to aggregate reports of sub-modules into a consolidated one. Some work has been done to implement a merge target on the maven cobertura plugin (see MCOBERTURA-33) but this code hasn't been included in the plugin yet. I didn't test the patch myself and can't say if it's worth a try.

In consequence, lots of people indeed suggest to use the maven dashboard plugin but I'd personally stay far away from it as it isn't very satisfying on the long term and I faced lots of issues with it (technical problems, lost of history,...). Instead, I warmly recommend Sonar. Have a look at Nemo, a public instance of the last version of Sonar, for a live demo of this tool. See for example the Commons Digester project and the drill down of code coverage.

梦太阳 2024-08-11 20:57:46

有一些插件可以聚合 Cobertura(和其他)报告。查看声纳XRadar 插件。还有 仪表板插件,但它有点笨重。

FWIW 艾玛确实做了线路覆盖

There are a few plugins that aggregate Cobertura (and other) reports. Check out the sonar and XRadar plugins. There is also the dashboard plugin, but it is a bit clunky.

FWIW Emma does do line coverage.

眼趣 2024-08-11 20:57:46

我非常感谢 Sven Oppermann 提交了他的 runCobertura 配置文件解决方案。这有帮助
我解决了“当您可能不知道时如何获得多模块项目的汇总覆盖率报告”的问题
能够使用声纳。

我创建了一个示例,演示如何创建多模块项目,该项目生成代码覆盖率报告,该报告不仅评估单元测试覆盖率(在所有子模块中),还评估集成测试的覆盖率
将您的应用程序升级为 Jetty 中的 .WAR。该示例托管在此处:

        http://dl.dropbox.com/u/9940067/code/multi-module-cobertura.zip 

如果您复制下面列出的 runCobertura 配置文件(基于
由 Sven 提供。)

以下是一些可以帮助您使用此配置文件的注释:

  • 启动 jetty 的集成测试模块(并定义针对运行的测试
    生产 .war) 必须命名为 web-test-driver-for-code-coverage,或者您
    必须修改 runCobertura 配置块中的语句。

  • 您的覆盖率报告将出现在您设置变量的任何位置

  • 当您运行代码覆盖率构建时,您必须在命令行中包含“clean”。 “clean”会清除之前的 cobertura.ser 文件,
    如果留下潜伏在周围可能会导致非常混乱的报告
    生成(您需要“清理”的标志是报告显示
    100% 覆盖所有内容,包括您知道从未被调用的内容。

     mvn -PrunCobertura clean install # 为您提供汇总报告。
    
  • 模块 web-test-driver-for-code-coverage 定义了一个 servlet 上下文监听器,它将 cobertura 指标显式刷新到磁盘
    当网络服务器关闭时。据说容器应该自动执行此操作,但这对我不起作用,所以
    我必须挂接显式调用来刷新指标。

  • 集成测试是在groovy中完成的,因为我基于一些已经使用groovy的maven项目框架。
    很抱歉增加了混乱,但它确实向您展示了如何在 groovy 中进行测试(无论如何强烈推荐。)

  • 请注意,当您使用 runCobertura 配置文件进行编译时,所有工件都是使用 cobertura 工具创建的,甚至是您的
    .war 文件。当然,你永远不想让它在生产中出现(一方面它会运行得很慢。)我还没有
    但找到了一种食物方法来让文物重新命名,以便“cobertura-ness”显而易见。

    <前><代码><配置文件>;
    <简介>
    runCobertura
    <激活>
    <属性>
    <名称>运行Cobertura
    <值>true


    <属性>
    html
    <工作目录>/tmp
    ${working.dir}/${project.version}/cobertura
    ${cobertura.working.dir}/complete.ser


    编译

    <构建>
    <插件>
    <插件>
    org.apache.maven.plugins
    maven-clean-plugin;
    <版本>2.4.1
    <继承>假
    <配置>
    <文件集>
    <文件集>
    <目录>.
    <包括>
    **/cobertura.ser


    <文件集>
    <目录>${cobertura.working.dir}



    <插件>
    org.apache.maven.plugins
    maven-antrun-plugin;
    <版本>1.7
    <处决>
    <执行>
    cobertura-Instrument
    <阶段>流程类
    <目标>
    <目标>跑

    <配置>
    <目标>


    <如果>
    <等于 arg1="${artifactId}" arg2="web-test-driver-for-code-coverage"/>
    <那么>


    <其他>
    <如果>
    <可用文件=“${project.build.outputDirectory}”/>
    <那么>


    <文件集目录=“${project.build.outputDirectory}”>
    ;








    <执行>
    cobertura-createCombinedSerFile
    <阶段>生成测试源
    <目标>
    <目标>跑

    <配置>
    <目标>


    <如果>
    <等于 arg1="${artifactId}" arg2="web-test-driver-for-code-coverage"/>
    <那么>


    <复制文件=“${cobertura.complete.ser.file}”到file=“${basedir}/cobertura.ser”/>

    <其他>
    <如果>
    <可用文件=“${basedir}/cobertura.ser”/>
    <那么>
    >

    <如果>
    <可用文件=“${cobertura.complete.ser.file}”/>
    <那么>


    ;
    >

    >
    <移动文件=“${basedir}/tmp.ser”到file=“${basedir}/cobertura.ser”/>







    <执行>
    cobertura-copyResultSerFileAndSources
    <阶段>验证
    <目标>
    <目标>跑

    <配置>
    <目标>


    <如果>
    <可用文件=“${basedir}/cobertura.ser”/>
    <那么>
    <回显消息=“move1”/>
    <移动文件=“${basedir}/cobertura.ser”到file=“${cobertura.complete.ser.file}”/>
    />
    <如果>
    <可用文件=“${basedir}/src/main/java”/>
    <那么>
    <复制到dir="${cobertura.working.dir}/source">
    <文件集目录=“src / main / java”>
    ;






    >;







    <依赖关系>
    <依赖关系>
    net.sourceforge.cobertura;
    cobertura
    <版本>1.9.4.1

    <依赖关系>
    ant-contrib;
    ant-contrib;
    <版本>20020829





    <依赖关系>
    <依赖关系>
    net.sourceforge.cobertura;
    cobertura
    <版本>1.9.4.1




I would really like to thank Sven Oppermann for submitting his runCobertura profile solution. This helped
me solve the question of 'how do you get aggregate coverage reports for multi-module projects when you might not be
able to use Sonar.

I have created an example which demonstrates how to create multi-module projects which produce code coverage reports that assess not only unit test coverage (in all submodules), but also coverage on INTEGRATION TESTS THAT BRING
UP YOUR APPLICATION AS A .WAR IN JETTY. The example is hosted here:

        http://dl.dropbox.com/u/9940067/code/multi-module-cobertura.zip 

The recipe I am providing is pretty re-useable if you copy the runCobertura profile listed below (based on the one
provide by Sven.)

Here are some notes which will help you use this profile:

  • the integration test module that launches jetty (and defines tests that run against
    the production .war) must either be named web-test-driver-for-code-coverage, or you
    must modify the statements in the runCobertura configuration block.

  • your coverage reports will appear wherever you set your variable

  • you MUST include 'clean' on the command line when you run your build for code coverage. 'clean' will blow away prior cobertura.ser files,
    which if left lurking around can cause very confusing reports to be
    generated (a sign you need to 'clean' is that the reports show
    100% coverage for everything, including stuff you know is never called.

      mvn -PrunCobertura clean install      # gives you the aggregate reports.
    
  • the module web-test-driver-for-code-coverage defines a servlet context listener that explicitly flushes the cobertura metrics to disk
    when the web server shuts down. Supposedly the container is supposed to do this automatically, but that didn't work for me, so
    I had to hook in the explicit call to flush out the metrics.

  • the integration tests are done in groovy because i based this on some maven project skeletons that already used groovy.
    Sorry for the added clutter, but it does show you how to do your tests in groovy (which is highly recommended anyway.)

  • Note that when you compile with the runCobertura profile all of your artifacts are created with cobertura instrumentation, even your
    .war file. You NEVER want to let this get out in production of course (for one thing it would run realllll slow.) I have not
    yet figured out a food way to get the artifacts to rename themselves so that the 'cobertura-ness' is obvious.

    <profiles>
    <profile>
        <id>runCobertura</id>
        <activation>
            <property>
                <name>runCobertura</name>
                <value>true</value>
            </property>
        </activation>
        <properties>
            <cobertura.format>html</cobertura.format>
            <working.dir>/tmp</working.dir>
            <cobertura.working.dir>${working.dir}/${project.version}/cobertura</cobertura.working.dir>
            <cobertura.complete.ser.file>${cobertura.working.dir}/complete.ser</cobertura.complete.ser.file>
    
            <!-- scope which determines whether or not cobertura is included in .war file: overriden here -->
            <cobertura.dependency.scope>compile</cobertura.dependency.scope>
        </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"/>
                                    <echo message="::PROCESS CLASSES: ${artifactId}"/>
    
                                    <if>
                                      <equals arg1="${artifactId}" arg2="web-test-driver-for-code-coverage" />
                                        <then>
                                            <echo message="::SKIPPING PHASE for integration test"/>
                                        </then>
                                        <else>
                                            <if>
                                                <available file="${project.build.outputDirectory}"/>
                                                <then>
                                                    <echo message="::BEFORE INSTRUMENT"/>
                                                    <cobertura-instrument>
                                                        <fileset dir="${project.build.outputDirectory}">
                                                            <include name="**/*.class"/>
                                                        </fileset>
                                                    </cobertura-instrument>
                                                </then>
                                            </if>
                                        </else>
                                    </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"/>
                                    <echo message=":::generate-test-sources"/>
    
    
                                    <if>
                                      <equals arg1="${artifactId}" arg2="web-test-driver-for-code-coverage" />
                                        <then>
                                            <echo message="::SHORT CIRCUIT COMBINE PHASE for integration test"/>
                                            <echo  message="source - ${cobertura.complete.ser.file} dest - ${basedir}/cobertura.ser"/>
                                            <copy file="${cobertura.complete.ser.file}" tofile="${basedir}/cobertura.ser"/>
                                        </then>
                                        <else>
                                            <if>
                                                <available file="${basedir}/cobertura.ser"/>
                                                <then>
                                                    <echo message="::: Is available ${basedir}/cobertura.ser"/>
                                                </then>
                                            </if>
    
                                            <if>
                                                <available file="${cobertura.complete.ser.file}"/>
                                                <then>
                                                    <echo message="before merge1"/>
                                                    <cobertura-merge datafile="${basedir}/tmp.ser">
                                                        <fileset file="${cobertura.complete.ser.file}"/>
                                                        <fileset file="${basedir}/cobertura.ser"/>
                                                    </cobertura-merge>
                                                    <echo message="move temp.ser to ${basedir}/cobertura.ser"/>
                                                    <move file="${basedir}/tmp.ser" tofile="${basedir}/cobertura.ser"/>
                                                </then>
                                            </if>
                                        </else>
                                    </if>
                                </target>
                            </configuration>
                        </execution>
                        <execution>
                            <id>cobertura-copyResultSerFileAndSources</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef resource="tasks.properties"/>
                                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
    
                                    <echo message=":::copyResultSerFileAndSources -beforeIf"/>
                                    <if>
                                        <available file="${basedir}/cobertura.ser"/>
                                        <then>
                                            <echo message="move1"/>
                                            <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>
                                            <echo message="runreport"/>
                                            <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>
            </dependency>
        </dependencies>
    </profile>
    </profiles>
    
短暂陪伴 2024-08-11 20:57:46

Thomas Sundberg 提供了一个有趣的解决方案,其中检测和测试报告是通过 ant 完成的,但所有测试和依赖项管理都是通过 mvn 完成的。

检查这里: thomassundberg wordpress< /a>

这意味着您必须按以下顺序在父级别执行以下命令:

mvn clean compile
ant instrument
mvn test
ant report

Martijn Stelinga 描述了将这些步骤集成到 sonar 中。

test-coverage-in-multi-模块项目

Thomas Sundberg offers an interesting solution in which instrumentation and test reporting is done via ant, but all testing and dependency management via mvn.

Check Here: thomassundberg wordpress

It means that you have to execute the commands below at the parent level in this order:

mvn clean compile
ant instrument
mvn test
ant report

Integrating these steps into sonar is described by Martijn Stelinga.

test-coverage-in-multi-module-projects

追我者格杀勿论 2024-08-11 20:57:46

由于这个答案,我可以实现与您需要的非常相似的东西: Maven - 添加对工件源的依赖项

我刚刚添加了 sources 并且 cobertura 也包含来自依赖项的类。

问候。

I could implement something quite similar to what you need thanks to this answer: Maven - add dependency on artifact source

I just added <classifier>sources</classifier> and cobertura includes classes from dependencies as well.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文