Monday, May 27, 2013

How to use maven with cobertura (code coverage) with sonar in multi-modules

Problem
The case is ..
  • a multi-module project managed by maven
  • some project has test cases (junit) of others ..project A has the junits and project B,C have the business
  • you are using sonar and cobertura to get code coverage percentage
  • Now, you get 0 coverage in project B, C and wrong indicator in project A
  • you want to use maven as usual and don't use ant :)
The problem and  is explained here with a pretty ant/maven  solution. I just re-implement the solution using maven-antrun-plugin to be executed  in one step.

Download the sample project from

 ..here


maven-antrun-plugin solution

in the sample project, I move cobertura.jar inside the lib folder of the distribution and rename to cobertura-jars then strip all versions


  •  add this profile to the parent project. with cobertura dependency  to do the cleanup

    <dependencies>
    <dependency>
    <groupid>net.sourceforge.cobertura</groupid>
    <artifactid>cobertura</artifactid>
    <version>1.9.4.1</version>
    </dependency>
    </dependencies>

    <profiles>
    <profile>
    <id>code-coverage</id>
    <build>
    <plugins>
    <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-clean-plugin</artifactid>
    <version>2.4.1</version>
    <configuration>
    <filesets>
    <fileset>
    <directory>.</directory>
    <includes>
    <include>*.ser</include>
    </includes>
    </fileset>
    </filesets>
    </configuration>
    </plugin>
    </plugins>

    </build>
    </profile>
    </profiles>

  • add this profile to each module ... to do instrumentation except the ear and the parent
<profiles>
<profile>
<id>code-coverage</id>
<build>
<plugins>
<plugin>
<artifactid>maven-antrun-plugin</artifactid>
<version>1.7</version>
<dependencies>
<dependency>
<groupid>net.sourceforge.cobertura</groupid>
<artifactid>cobertura</artifactid>
<version>1.9.4.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<taskdef classname="net.sourceforge.cobertura.ant.InstrumentTask" name="cobertura-instrument">
<classpath path="${cobertura.lib.dir}/cobertura.jar;${cobertura.lib.dir}/log4j.jar;${cobertura.lib.dir}/asm.jar;${cobertura.lib.dir}/asm-tree.jar;${cobertura.lib.dir}/jakarta-oro.jar">
</classpath></taskdef>
<cobertura-instrument todir="./target/classes">
<fileset dir="./target/classes">
<include name="**/*.class">
</include></fileset>
</cobertura-instrument>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

  • add  this profile to the ear project ..  to do the merge and generate the report (note the location of source and test code)
<profiles>
<profile>
<id>code-coverage</id>
<build>
<plugins>

<plugin>
<artifactid>maven-antrun-plugin</artifactid>
<version>1.7</version>
<dependencies>
<dependency>
<groupid>net.sourceforge.cobertura</groupid>
<artifactid>cobertura</artifactid>
<version>1.9.4.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<target>


<taskdef classname="net.sourceforge.cobertura.ant.MergeTask" name="cobertura-merge">
<classpath path="${cobertura.lib.dir}/cobertura.jar;${cobertura.lib.dir}/log4j.jar;${cobertura.lib.dir}/asm.jar;${cobertura.lib.dir}/asm-tree.jar;${cobertura.lib.dir}/jakarta-oro.jar">

</classpath></taskdef>

<taskdef classname="net.sourceforge.cobertura.ant.ReportTask" name="cobertura-report">
<classpath path="${cobertura.lib.dir}/cobertura.jar;${cobertura.lib.dir}/log4j.jar;${cobertura.lib.dir}/asm.jar;${cobertura.lib.dir}/asm-tree.jar;${cobertura.lib.dir}/jakarta-oro.jar">

</classpath></taskdef>

<mkdir dir="target/sum">
<mkdir dir="target/report">

<cobertura-merge datafile="target/sum/coberturaSum.ser">
<fileset dir="../.">
<include name="**/cobertura.ser">
</include></fileset>
</cobertura-merge>

<cobertura-report datafile="target/sum/coberturaSum.ser" destdir="target/report" format="xml">
<fileset dir="../bus1/src/main/java">
<fileset dir="../bus1/src/test/java">

<fileset dir="../bus2/src/main/java">
<fileset dir="../bus2/src/test/java">


<fileset dir="../test/src/main/java">
<fileset dir="../test/src/test/java">


</fileset></fileset></fileset></fileset></fileset></fileset></cobertura-report>
</mkdir></mkdir></target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>




  • Then execute with the following
 clean install org.codehaus.sonar:sonar-maven3-plugin:3.0:sonar -P code-coverage -Dmaven.test.failure.ignore=true -Dsonar.dynamicAnalysis=reuseReports  -Dsonar.cobertura.reportPath=../busEar/target/report/coverage.xml  -Dcobertura.lib.dir=../parent/cobertura-jars



No comments:

Post a Comment