<p>How do I setup an Ant task to generate <a href="http://emma.sourceforge.net" rel="noreferrer">Emma</a> code coverage reports?</p>

[{'answer_id': 52994, 'author': 'Rob Spieldenner', 'author_id': 5118, 'author_profile': 'https://Stackoverflow.com/users/5118', 'pm_score': 5, 'selected': True, 'text': '<p>To answer questions about where the source and instrumented directories are (these can be switched to whatever your standard directory structure is):</p>\n\n<pre><code>&lt;property file="build.properties" /&gt;\n&lt;property name="source" location="src/main/java" /&gt;\n&lt;property name="test.source" location="src/test/java" /&gt;\n&lt;property name="target.dir" location="target" /&gt;\n&lt;property name="target" location="${target.dir}/classes" /&gt;\n&lt;property name="test.target" location="${target.dir}/test-classes" /&gt;\n&lt;property name="instr.target" location="${target.dir}/instr-classes" /&gt;\n</code></pre>\n\n<p>Classpaths:</p>\n\n<pre><code>&lt;path id="compile.classpath"&gt;\n  &lt;fileset dir="lib/main"&gt;\n    &lt;include name="*.jar" /&gt;\n  &lt;/fileset&gt;\n&lt;/path&gt;\n\n&lt;path id="test.compile.classpath"&gt;\n  &lt;path refid="compile.classpath" /&gt;\n  &lt;pathelement location="lib/test/junit-4.6.jar" /&gt;\n  &lt;pathelement location="${target}" /&gt;\n&lt;/path&gt;\n\n&lt;path id="junit.classpath"&gt;\n  &lt;path refid="test.compile.classpath" /&gt;\n  &lt;pathelement location="${test.target}" /&gt;\n&lt;/path&gt;\n</code></pre>\n\n<p>First you need to setup where Ant can find the Emma libraries:</p>\n\n<pre><code>&lt;path id="emma.lib" &gt;\n    &lt;pathelement location="${emma.dir}/emma.jar" /&gt;\n    &lt;pathelement location="${emma.dir}/emma_ant.jar" /&gt;\n&lt;/path&gt;\n</code></pre>\n\n<p>Then import the task:</p>\n\n<pre><code>&lt;taskdef resource="emma_ant.properties" classpathref="emma.lib" /&gt;\n</code></pre>\n\n<p>Then instrument the code:</p>\n\n<pre><code>&lt;target name="coverage.instrumentation"&gt;\n    &lt;mkdir dir="${instr.target}"/&gt;\n    &lt;mkdir dir="${coverage}"/&gt;\n    &lt;emma&gt;\n        &lt;instr instrpath="${target}" destdir="${instr.target}" metadatafile="${coverage}/metadata.emma" mode="copy"&gt;\n            &lt;filter excludes="*Test*"/&gt;\n        &lt;/instr&gt;\n    &lt;/emma&gt;\n    &lt;!-- Update the that will run the instrumented code --&gt;\n    &lt;path id="test.classpath"&gt;\n        &lt;pathelement location="${instr.target}"/&gt;\n        &lt;path refid="junit.classpath"/&gt;\n        &lt;pathelement location="${emma.dir}/emma.jar"/&gt;\n    &lt;/path&gt;\n&lt;/target&gt;\n</code></pre>\n\n<p>Then run a target with the proper VM arguments like:</p>\n\n<pre><code>&lt;jvmarg value="-Demma.coverage.out.file=${coverage}/coverage.emma" /&gt;\n&lt;jvmarg value="-Demma.coverage.out.merge=true" /&gt;\n</code></pre>\n\n<p>Finally generate your report:</p>\n\n<pre><code>&lt;target name="coverage.report" depends="coverage.instrumentation"&gt;\n    &lt;emma&gt;\n        &lt;report sourcepath="${source}" depth="method"&gt;\n            &lt;fileset dir="${coverage}" &gt;\n                &lt;include name="*.emma" /&gt;\n            &lt;/fileset&gt;\n            &lt;html outfile="${coverage}/coverage.html" /&gt;\n        &lt;/report&gt;\n    &lt;/emma&gt;\n&lt;/target&gt;\n</code></pre>\n'}, {'answer_id': 143208, 'author': 'wheleph', 'author_id': 15647, 'author_profile': 'https://Stackoverflow.com/users/15647', 'pm_score': 0, 'selected': False, 'text': '<p>Emma 2.1 introduces another way of obtaining runtime coverage information (.ec file). One can remotely request the data from the given port of the computer where an instrumented application is runnig. So there\'s no need to stop VM.</p>\n\n<p>To get the file with runtime coverage data you need to insert the following snippet in your Ant script between running of your tests and generating coverage report:</p>\n\n<pre><code>&lt;emma&gt;\n    &lt;ctl connect="${emma.rt.host}:${emma.rt.port}" &gt;\n        &lt;command name="coverage.get" args="${emma.ec.file}" /&gt;\n        &lt;command name="coverage.reset" /&gt;\n    &lt;/ctl&gt;\n&lt;/emma&gt;\n</code></pre>\n\n<p>Other steps are similar to Emma 2.0. They are perfectly described in <a href="https://stackoverflow.com/questions/52984/how-do-i-generate-emma-code-coverage-raeports-using-ant#52994">previous post</a></p>\n\n<p>More information on Emma 2.1 features: <a href="http://sourceforge.net/project/shownotes.php?group_id=108932&amp;release_id=336859" rel="nofollow noreferrer">http://sourceforge.net/project/shownotes.php?group_id=108932&amp;release_id=336859</a></p>\n'}, {'answer_id': 201683, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://emma.sourceforge.net/userguide_single/userguide.html#N10291" rel="nofollow noreferrer">User Guide has a good example of how to set up your build script</a> so that you not only seperate the instrumented code from the execution, but it\'s also all contained in the same <code>&lt;target&gt;</code> so that you don\'t have to run a series of different targets, but instead you can just do something like <code>ant emma tests</code> (if <code>ant tests</code> was how you normally ran your unit tests, for example).</p>\n\n<p>Here\'s their example:</p>\n\n<pre><code>&lt;target name="emma" description="turns on EMMA instrumentation/reporting" &gt;\n    &lt;property name="emma.enabled" value="true" /&gt;\n    &lt;!-- EMMA instr class output directory: --&gt;\n    &lt;property name="out.instr.dir" value="${basedir}/outinstr" /&gt;\n    &lt;mkdir dir="${out.instr.dir}" /&gt;\n&lt;/target&gt;\n\n&lt;target name="run" depends="init, compile" description="runs the examples" &gt;\n    &lt;emma enabled="${emma.enabled}" &gt;\n      &lt;instr instrpathref="run.classpath"\n             destdir="${out.instr.dir}" \n             metadatafile="${coverage.dir}/metadata.emma"\n             merge="true"\n      /&gt;\n    &lt;/emma&gt;\n\n    &lt;!-- note from matt b: you could just as easily have a &lt;junit&gt; task here! --&gt;\n    &lt;java classname="Main" fork="true" &gt;\n      &lt;classpath&gt;\n       &lt;pathelement location="${out.instr.dir}" /&gt;\n        &lt;path refid="run.classpath" /&gt;\n        &lt;path refid="emma.lib" /&gt;\n      &lt;/classpath&gt; \n      &lt;jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" /&gt;\n      &lt;jvmarg value="-Demma.coverage.out.merge=true" /&gt;\n    &lt;/java&gt;\n\n    &lt;emma enabled="${emma.enabled}" &gt;\n      &lt;report sourcepath="${src.dir}" &gt;\n        &lt;fileset dir="${coverage.dir}" &gt;\n          &lt;include name="*.emma" /&gt;\n        &lt;/fileset&gt;\n\n        &lt;txt outfile="${coverage.dir}/coverage.txt" /&gt;\n        &lt;html outfile="${coverage.dir}/coverage.html" /&gt;\n      &lt;/report&gt;\n    &lt;/emma&gt;\n&lt;/target&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52984', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5118/']