title: Junit Coverage using Jacoco with ant build
body: <p>I have run into this specific problem while executing the junit with jacoco:coverage for code coverage. Tried a few things but no luck yet.</p>
  
  <p>I have this junit build script.</p>
  
  <pre><code>&lt;target name="executeJunitMain" depends="createJunitLibs" description="Executes All Junit and prepare report."&gt;
  
              &lt;junit fork="yes" haltonfailure="off" failureProperty="junit.failure" includeantruntime="true" maxmemory="256m"&gt;
                  &lt;classpath refid="compile.class.path" /&gt;
                  &lt;formatter type="xml" /&gt;
                  &lt;jvmarg value="-javaagent:${external.junit.lib.dir}/jmockit.jar"/&gt;
                  &lt;sysproperty key="jacoco-agent.destfile" file="${coverage.dir}/jacoco.exec"/&gt;
                  &lt;batchtest fork="yes" todir="${report.dir}" &gt;
                      &lt;fileset dir="${dest.dir}"&gt;
                          &lt;include name="**/Test*.class" /&gt;
                          &lt;exclude name="**/AllTests.class" /&gt;
                          &lt;exclude name="**/*$*.class"/&gt;
                      &lt;/fileset&gt;
                  &lt;/batchtest&gt;
              &lt;/junit&gt;
  
          &lt;antcall target="report"/&gt;
  
      &lt;/target&gt;
  
  &lt;!-- Execute the coverage report.--&gt;
      &lt;target name="report" description="Collect Coverage Report"&gt;
  
          &lt;!-- Creates the Junit Report --&gt;
          &lt;junitreport todir="${report.dir}"&gt;
              &lt;fileset dir="${report.dir}"&gt;
                  &lt;include name="TEST-*.xml"/&gt;
              &lt;/fileset&gt;
              &lt;report format="frames" todir="${report.dir}/html"/&gt;
          &lt;/junitreport&gt;
  
          &lt;!-- Creates the coverage Report.--&gt;
          &lt;mkdir dir="${report.dir}/coverage"/&gt;
          &lt;jacoco:report&gt;
              &lt;executiondata&gt;
                  &lt;file file="${coverage.dir}/jacoco.exec" /&gt;
              &lt;/executiondata&gt;
  
              &lt;structure name="Code Coverage"&gt;
                  &lt;classfiles&gt;
                      &lt;zipfileset src="${sources.lib.dir}/${test.jar.name}.jar"/&gt;
                                       &lt;/classfiles&gt;
              &lt;/structure&gt;
              &lt;html destdir="${report.dir}/coverage" /&gt;
          &lt;/jacoco:report&gt;
  
      &lt;/target&gt;
  </code></pre>
  
  <p>using this ant script sometimes the junit build is failing. Error as below </p>
  
  <pre><code>BUILD FAILED
  C:\Users\user\Codebases\junit.xml:111: The following error occurred while executing this line:
  C:\Users\user\Codebases\junit.xml:147: The following error occurred while executing this line:
  C:\Users\user\Codebases\junit.xml:164: Unable to read execution data file C:\Users\user\CodeBases\ju
  nit\coverage\jacoco.exec
  </code></pre>
  
  <p>But sometimes it works perfectly. I am not sure when it works and when it does not. Any help will be appreciated.</p>
  
  <p>Thanks</p>