Your most unhappy customers are your greatest source of learning.

jacoco code coverage with Ant - Sonar

Here is the detail for how to setup code coverage using jacoco plugin which is OOB come out with SONAR :

  1. Write sample java project called ant-jacoco-codecoverage
  2.  Project folder structure would be given as below snapshot:
  3. Under the target folder you may have to create classes & reports folders where reports folder will have one more folder called junit
  4. Write sample Java class in src folder you can copy paste below code  :
    1. public class One {
        String message = "foo";
        public String foo() {
          return message;
        public void uncoveredMethod() {
  5. Now create test class called and put the below code :
    1. import static org.junit.Assert.assertEquals;
      import org.junit.Test;
      public class OneTest {
        public void testFoo() throws Exception {
          One one = new One();
  6. Now put the junit-4.10.jar file under lib folder
  7. Download jacocoant.jar  jacoco ant task library and put that jar file under Apache Ant /lib directory
  8. Now create build.xml to run the test cases and generate sonar report (Use below code)
    1. <?xml version="1.0" encoding="UTF-8"?>
      <project name="UT coverage with Ant and JaCoCo running tests" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">

          <!-- ========= Define the main properties of this project ========= -->
          <property name="src.dir" value="src" />
          <property name="test.dir" value="test" />
          <property name="lib.junit.dir" value="lib" />
          <property name="build.dir" value="target" />
          <property name="classes.dir" value="${build.dir}/classes" />
          <property name="reports.dir" value="${build.dir}/reports" />
          <property name="reports.junit.xml.dir" value="${reports.dir}/junit" />
          <!-- Define the Sonar properties -->
          <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar332?useUnicode=true&amp;characterEncoding=utf8" />
              <property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />

              <!-- the username and password of your database -->
              <property name="sonar.jdbc.username" value="root" />
              <property name="sonar.jdbc.password" value="password" />

          <property name="sonar.projectKey" value="org.codehaus.sonar:example-ut-ant-jacoco-runTests" />
          <property name="sonar.projectName" value="UT coverage with Ant and JaCoCo running tests" />
          <property name="sonar.projectVersion" value="1.0" />
          <property name="sonar.language" value="java" />
          <property name="sonar.sources" value="${src.dir}" />
          <property name="sonar.tests" value="${test.dir}" />
          <property name="sonar.binaries" value="${classes.dir}" />
          <property name="sonar.sourceEncoding" value="UTF-8" />
          <property name="sonar.surefire.reportsPath" value="${reports.junit.xml.dir}" />  

          <!-- The following properties are required to use JaCoCo: -->
          <property name="sonar.dynamicAnalysis" value="reuseReports" />
          <property name="" value="jacoco" />
          <property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />

          <!-- ========= Define "regular" targets: clean, compile, test, ... ========= -->
    2. <target name="clean">
              <delete dir=".sonar" />
              <delete dir="${build.dir}" />
              <delete dir="${reports.dir}" />

          <target name="init">
              <mkdir dir="${build.dir}" />
              <mkdir dir="${classes.dir}" />
              <mkdir dir="${reports.dir}" />
              <mkdir dir="${reports.junit.xml.dir}" />

          <target name="compile" depends="init">
              <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"  />
              <javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" />

          <path id="classpath">
              <fileset dir="${lib.junit.dir}" includes="*.jar"/>
          <target name="test" depends="compile">
              <taskdef name="junit" classname="">
                      <path refid="classpath"/>
              <!-- Import the JaCoCo Ant Task -->
              <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
              <!-- Run your unit tests, adding the JaCoCo agent -->
              <jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
                <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
                  <classpath location="${classes.dir}" />
                  <classpath refid="classpath" />

                  <formatter type="xml" />
                  <batchtest todir="${reports.junit.xml.dir}">
                    <fileset dir="${test.dir}">
                      <include name="**/*" />

          <!-- ========= Define Sonar target ========= -->
          <target name="sonar" depends="compile">
              <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
                  <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
                  <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" />
              <!-- Execute Sonar -->
               <sonar:sonar key="${sonar.projectKey}" version="0.1-SNAPSHOT"  xmlns:sonar="antlib:org.sonar.ant"/>

          <!-- ========= The main target "all" ========= -->
          <target name="all" depends="clean,compile,test,sonar" />

  9. Now start sonar server
  10. Run  >> ant compile
  11. ant test
  12. ant sonar
  13. Now if all above commands running properly, then see the code coverage in sonar dashboard
    1. See below snapshot:
Note :
For Sonar below version than 3.4, you may have to set below property for coverage:
<property name="sonar.code.codeCoveragePlugin" value="jacoco" />
Instead of
<property name="" value="jacoco" />

Most Reading


Like Me & Share

Buy Websites

My Followers