Jacoco, Tycho, and Coverage Reports

It’s been a while since I posted here.  Most of my postings are now on Google+ or via twitter.  However, neither place does well for code snippets so here I am.   If you are using Tycho and want Java code coverage, the Jacoco maven plugin makes enabling the agent during your build relatively simple for you unit tests.

<plugin>
   <groupId>org.jacoco</groupId>
   <artifactId>jacoco-maven-plugin</artifactId>
   <version>0.5.6.201201232323</version>
   <executions>
      <execution>
         <goals>
            <goal>prepare-agent</goal>
         </goals>
      </execution>
   </executions>
</plugin>

Add the above as part of the build entry or as part of a build entry in a coverage profile if you don’t want it to always run.

Getting reliable coverage reports in XML or HTML is a bit more work. While there is a report goal for the jacoco-maven-plugin it currently doesn’t display all of the coverage for all bundles instrumented as it needs to see the source code for those bundles as well. However, the Jacoco report Ant Task doesn’t have this problem. So with a bit of help from the maven-antrun-plugin and antcontrib, we can generate the reports. All reports will be populated in target/jacoco/report. Both HTML and XML reports will be generated.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.7</version>
   <dependencies>
      <dependency>
         <groupId>org.jacoco</groupId>
         <artifactId>org.jacoco.ant</artifactId>
         <version>0.5.6.201201232323</version>
      </dependency>
      <dependency>
         <groupId>ant-contrib</groupId>
         <artifactId>ant-contrib</artifactId>
         <version>20020829</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>jacoco-report</id>
         <phase>install</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <target>
                <property name="source-location" location="../"/>
                <taskdef name="jacoco-report"
                   classname="org.jacoco.ant.ReportTask"
                   classpathref="maven.plugin.classpath" />
                <taskdef classpathref="maven.runtime.classpath"
                         resource="net/sf/antcontrib/antcontrib.properties" />
                <available
                   file="${project.basedir}/target/jacoco.exec"
                   property="jacoco.exec.file.exists" />
                <echo message="${project.basedir}/target/jacoco.exec" />
                <if>
                  <equals arg1="${jacoco.exec.file.exists}"
                          arg2="true" />
                  <then>
                     <echo message="Executing jacoco report" />
                     <echo message="${source-location}"/>
                     <trycatch>
                         <try>
                            <jacoco-report>
                               <executiondata>
                                   <file
                                     file="${project.basedir}/target/jacoco.exec" />
                               </executiondata>

                               <structure name="Minerva">
                                   <classfiles>
                                      <fileset
                                         dir="${source-location}/org.aniszczyk.minerva.core/target/classes" />
                                   </classfiles>
                                   <sourcefiles
                                        encoding="UTF-8">
                                      <fileset
                                         dir="${source-location}/org.aniszczyk.minerva.core/src/" />
                                   </sourcefiles>
                               </structure>
                               <html destdir="${project.basedir}/target/jacoco/report" />
                               <xml destfile="${project.basedir}/target/jacoco/report/jacoco.xml"/>
                            </jacoco-report>
                         </try>
                         <catch>
                             <echo>skipping</echo>
                         </catch>
                     </trycatch>
                  </then>
                  <else>
                     <echo message="No jacoco.exec file found." />
                  </else>
               </if>
           </target>
         </configuration>
       </execution>
    </executions>
</plugin>

The code above basically checks for a jacoco.exec file. If one exists, it runs the jacoco-report ant task. The ant task specifies where the classfiles can be found as well as where the source code can be found. It will also generate both HTML and XML reports.

I hope this helps others enable code coverage in their Tycho builds. I’ve contributed the above as examples in the Minerva project on github.

About these ads
This entry was posted in build, craftsmanship, eclipse, maven, release engineering, testing, tycho. Bookmark the permalink.

7 Responses to Jacoco, Tycho, and Coverage Reports

  1. Hi David,

    This will be a quick topic in our EclipseCon talk with Xavier Seignard: http://www.eclipsecon.org/2012/sessions/get-ready-fight-your-technical-debt-tycho-sonar-and-jacoco .
    FYI, JBoss actively follows a very important feature request about Jenkins and Jacoco https://issues.jenkins-ci.org/browse/JENKINS-10835 and we even proposed a subject to the GSoc for that: https://community.jboss.org/wiki/GSoC12Ideas#A_Jenkins_plugin_to_visualize_Jacoco_code_coverage_reports . Jonathan Furth, who will mentor this GSoC already wrote a draft of a Jenkins plugin for Jacoco https://github.com/jfuerth/emma-plugin/tree/jacoco .

    If you have any student you know would be interested in it, please send him to Jonathan ;)

  2. kingargyle says:

    Yep. Following that bug and github project already. I may mess around with the xslt a bit to see if I can get it converted to work better with emma or cobertura code coverage plugins.

  3. Guillaume says:

    It appears I had about the same problem as you, and you helped me find a solution unsing ant, along with someone on stackoverflow : http://stackoverflow.com/questions/10213464/how-can-i-get-code-coverage-of-an-external-java-library-with-jacoco (this was my question).
    I’d be insterested if you found a way to do the samed exclusively with maven ! Thanks.

  4. Great Article. Helped me create the code coverage. I had issue with one of the project though. It uses jibx binding with java 7 and i had to use -XX:-UseSplitVerifier as the vm argument to get it working. But jacoco is not working with this argument in the surefire plugin.

    Do you have any idea about this.

    • kingargyle says:

      Not sure. I would suggest asking on the Jacoco forums and see if they have any information. I know that Jacoco in the past has had problems with Hibernate or things that can do some funky class loader magic. You can always exclude the jibx framework from code coverage itself so it doesn’t get instrumented.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s