Maven2 Multiproject Cobertura Reporting Problems During mvn site Build

自古美人都是妖i 提交于 2019-12-06 00:26:07

I suspect that you're missing an execution of cobertura plugin during the compile phase so that the code only gets instrumented by the reporting plugins, in the site lifecycle, after the tests were run. So the test runs aren't picked up because they run on non-instrumented code. Analyze your build logs more carefully - if I'm right, you'll notice that surefire tests are executed before cobertura:instrument.

My configuration is similar to yours, but in addition to specifying the clean exectution in pluginManagement (like you), I specify the cobertura plugin explicitly in build plugins section:

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

My configuration sorta works, and all Cobertura stuff is in the global organization-wide pom, which all projects use as a parent.

This way projects don't specify anything Cobertura-related in their pom.xml's, but they still generate coverage reports.

I haven't been succesful at getting Cobertura to combine reporting from multi-projects. This has been a problem in general with multi-project reporting.

We have been evaluating sonar as a solution for our metrics reporting. It seems to do a great job of providing summary metrics across projects, including multi-proijects.

The solution implemented by me is somewhat manual, but works. It consists of several steps of one is a step to combine the several .ser files that are generated by Cobertura. This can be done by using the cobertura-merge commandline tool inside a maven task.

According to the output you show is that the files are not actually instrumented, it tells that only 3 files are instrumented.

@Marco is right, it is not possible to achieve this normally through maven only as the maven cobertura plugin is missing a merge goal.

You can achieve it through a mix of maven and ant goals : http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/

Nevertheless, in the case you have one single project undertest, there is no need to merge. You can, in the test project, copy the .ser file and the instrumented classes from the project under test :

//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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!