title: How can I get code coverage of an external java library with jacoco?
body: <p>If I have a java project that uses a library (a jar file), is it possible to get the code coverage of classes inside this jar ? </p>
  
  <p>The idea behind this is that I would like to find out what proportion of the external libraries the project relies on (let's say spring, hibernate, or it could be the scala jars if it was a scala project, why not) are actually used. I even imagine that I could try to list them and rebundle them in a single jar that would contain <em>only</em> necessary .class files (with a plugin like apache felix, for example) to get the smallest possible jar. I'm not sure I really want to do this, I'm aware it is probably a bad idea for a number of reasons, but I think of it as an experimentation.</p>
  
  <p>I can't find how to do it, jacoco reports only coverage for the class files directly inside my project. Maybe I'm doing something wrong, I'm using the maven plugin like this :</p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;org.jacoco&lt;/groupId&gt;
              &lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;0.5.6.201201232323&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;destfile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/destfile&gt;
                  &lt;datafile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/datafile&gt;
                  &lt;includes&gt;
                      &lt;include&gt;**&lt;/include&gt;
                  &lt;/includes&gt;
              &lt;/configuration&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;jacoco-initialize&lt;/id&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;prepare-agent&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
                  &lt;execution&gt;
                      &lt;id&gt;jacoco-site&lt;/id&gt;
                      &lt;phase&gt;package&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;report&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>I've tried changing the include tag, but the only effect is restricting the default which includes only the class files directly inside my project.</p>
  
  <p>Thanks in advance !</p>
  
  <hr>
  
  <p>Edit after oers' answer :</p>
  
  <p>I found out how to do it with ant and antrun-plugin, though it is very complicated, I had much trouble with antrun plugin versions (unable to make a recent version work, even for a basic task) and I'd really like to stick to Maven. <strong>If someone knows how to do the equivalent with je jacoco maven plugin instead of ant, I'm interested !</strong></p>
  
  <p>Partial solution with ant : actually the jacoco.exec file already contained references to the classes of my external jars ; therefore it is the report task that should be told to take account of these jar, and not the runtime phase as I thought. Here is the maven configuration I used (I found help on <a href="http://intellectualcramps.wordpress.com/2012/03/22/jacoco-tycho-and-coverage-reports/">http://intellectualcramps.wordpress.com/2012/03/22/jacoco-tycho-and-coverage-reports/</a> ) :</p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
              &lt;!--&lt;version&gt;1.7&lt;/version&gt;--&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.jacoco&lt;/groupId&gt;
                      &lt;artifactId&gt;org.jacoco.ant&lt;/artifactId&gt;
                      &lt;version&gt;0.5.6.201201232323&lt;/version&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;ant-contrib&lt;/groupId&gt;
                      &lt;artifactId&gt;ant-contrib&lt;/artifactId&gt;
                      &lt;version&gt;20020829&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;jacoco-report&lt;/id&gt;
                      &lt;phase&gt;package&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;run&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;tasks&gt;
  
                              &lt;taskdef name="jacoco-report"
                                     classname="org.jacoco.ant.ReportTask"
                                     classpathref="maven.plugin.classpath" /&gt;
                              &lt;taskdef classpathref="maven.runtime.classpath"
                       resource="net/sf/antcontrib/antcontrib.properties" /&gt;
                              &lt;available
                 file="${project.basedir}/target/jacoco.exec"
                 property="jacoco.exec.file.exists" /&gt;
                              &lt;echo message="${project.basedir}/target/jacoco.exec" /&gt;
                              &lt;if&gt;
                                  &lt;equals arg1="${jacoco.exec.file.exists}"
                        arg2="true" /&gt;
                                  &lt;then&gt;
                                      &lt;echo message="Executing jacoco report" /&gt;
  
                                      &lt;trycatch&gt;
                                          &lt;try&gt;
                                              &lt;jacoco-report&gt;
                                                  &lt;executiondata&gt;
                                                      &lt;file
                                   file="${project.basedir}/target/jacoco.exec" /&gt;
                                                  &lt;/executiondata&gt;
  
                                                  &lt;structure name="Minerva"&gt;
                                                      &lt;classfiles&gt;
                                                          &lt;fileset
                                       dir="target/classes" /&gt;
  
                                                          &lt;fileset dir="C:/Data/dev/m2Repository/com/groupama/framework/crypt/fwk-cryptage/1.0/"&gt;
                                                              &lt;include name="**/*.jar"/&gt;
                                                          &lt;/fileset&gt;
  
                                                      &lt;/classfiles&gt;
  
                                                      &lt;sourcefiles
                                      encoding="UTF-8"&gt;
                                                          &lt;fileset
                                       dir="src/main/java" /&gt;
                                                      &lt;/sourcefiles&gt;
                                                  &lt;/structure&gt;
                                                  &lt;html destdir="${project.basedir}/target/jacoco/report" /&gt;
                                                  &lt;xml destfile="${project.basedir}/target/jacoco/report/jacoco.xml"/&gt;
                                              &lt;/jacoco-report&gt;
                                          &lt;/try&gt;
                                          &lt;catch&gt;
                                              &lt;echo&gt;skipping&lt;/echo&gt;
                                          &lt;/catch&gt;
                                      &lt;/trycatch&gt;
                                  &lt;/then&gt;
                                  &lt;else&gt;
                                      &lt;echo message="No jacoco.exec file found." /&gt;
                                  &lt;/else&gt;
                              &lt;/if&gt;
                          &lt;/tasks&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>