aeinstein83
aeinstein83

Reputation: 289

EMMA code coverage for all modules

I am new to maven and our project has several modules. my goal is generate one coverage report for the entire project (that has all the modules). below is the rough skeleton of our Based on project's POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>1.0.0</modelVersion>
<groupId>com.xx.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test</name>

<modules>
    <module>module1</module>
    <module>module2</module>
    <module>module3</module>
    <module>module4</module>
    <module>module5</module>        
</modules>

<build>
    .
    .
    .

</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>emma-maven-plugin</artifactId>
            <version>1.0-alpha-3</version>
        </plugin>
    </plugins>
</reporting>
</project>

as you see above i have added emma plug-in and executed following : mvn emma:emma This is generating individual coverage reports for each module under the dir called 'target'. is there a way i can consolidate all these reports into one report? any pointers..?

Upvotes: 1

Views: 577

Answers (2)

Bob Fields
Bob Fields

Reputation: 191

Is there any way to do this generically for multiple submodules in the ant report task? Specifying the exact directories works OK, but not ** dirset references. This works OK:

<classfiles>
    <dirset dir="${basedir}">
        <include name="module1/target/classes"/>
        <include name="module2/target/classes"/>

OR

<dirset dir="">
    <include name="module1/target/classes"/>
    <include name="module2/target/classes"/>

OR

<dirset dir="" includes="module1/target/classes,module2/target/classes"/>

These do not work (0 classes, or 'Error while creating report' with no further details):

<dirset dir="">
  <include name="**/target/classes"/>

OR

<dirset dir="${basedir}">
  <include name="**/target/classes"/>

OR

<dirset dir="" includes="**/target/classes"/>

OR

<dirset dir="${basedir}" includes="**/target/classes"/>

Interestingly, sourcefile ** references produces the HTML source links OK, I thought it worked exactly the same as classfiles:

<sourcefiles>
    <dirset dir="" includes="**/src/main/java"/>

This does not work:

<sourcefiles>
    <dirset dir="${basedir}" includes="**/src/main/java"/>

Upvotes: 0

aeinstein83
aeinstein83

Reputation: 289

Following is the approach to achieve required results

  1. add a JaCoCo plugin to the parent pom.xml so all projects could generate a coverage report.

  2. create new module ‘coverage’ for appending all the results of the Jacoco plugin…this is just one time thing.

  3. in pom.xml of this new module, insert the required plugins to join all the coverage information a. define all properties where all the classes,sources,generated-sources etc, since the JaCoCo report plugin requires you set the location of the build directory, class directory, source directory or generated-source directory b. we are using ANT task with Maven. JaCoCo Ant task will merge results from multiple JaCoCo file results

  4. now from parent pom.xml run : mvn clean install

  5. if module has any test cases , ‘jacoco.exec’ file will be generated for that module (this file has all the coverage information that is required for reporting), individual module code coverage reporting is also generated at this point (under \target\site\jacoco)
  6. and finally from ‘coverage’ pom.xml run : mvn clean install a. this is going to run ANT reporting task within maven that will grab all the generated ‘jacoco.exec’ files for all the modules, merge the results and generate the report. Below is the ANT reporting target that can be run within maven

    <?xml version="1.0" encoding="UTF-8" ?>
    <project name="maven-antrun-" default="main"  >
    <target name="main">
    <echo message="Generating JaCoCo Reports"/>
    <taskdef name="report" classname="org.jacoco.ant.ReportTask">
    <classpath path="./target/jacoco-jars/org.jacoco.ant.jar"/>
    </taskdef>
    <mkdir dir="./target/coverage-report"/>
    <report>
    <executiondata>
      <fileset dir="../module1/target">
        <include name="jacoco.exec"/>
      </fileset>
      <fileset dir="../module2/target">
        <include name="jacoco.exec"/>
      </fileset>
    
    </executiondata>
    <structure name="Project Code Coverage">
      <group name="project">
        <classfiles>
          <fileset dir="../module1/target/classes"/>
          <fileset dir="../module2/target/classes"/>
    
        </classfiles>
        <sourcefiles encoding="UTF-8">
          <fileset dir="../module1/src/main/java"/>
          <fileset dir="../module2/src/main/java"/>          
        </sourcefiles>
      </group>
    </structure>
    <html destdir="./target/coverage-report/html"/>
    <xml destfile="./target/coverage-report/coverage-report.xml"/>
    <csv destfile="./target/coverage-report/coverage-report.csv"/>
    </report>
    </target>
    </project> 
    

Upvotes: 2

Related Questions