Friday, August 24, 2012

Maven Jacoco plugin, Standalone JVM, Jacoco report generation


Steps to generate coverage data

1. Add the below JVM line when starting up your java program
-javaagent:${project.deploy.path}/org.jacoco.agent-0.5.7.201204190339-runtime.jar=destfile=${project.jacoco.path}/jacoco/jacoco.exec,dumponexit=true

-javaagent:${project.deploy.path}/org.jacoco.agent-0.5.7.201204190339-runtime.jar
path to jacoco runtime jar which is needed to instrument(add probes) to all classes/jar files

destfile=${project.jacoco.path}/jacoco/jacoco.exec
file where the coverage data is stored.

dumponexit=true
other optional parameters used by Jacoco

2. Start the java program.

3. Run your integration tests thru the java program that has been instrumented by Jacoco.

4. Stop the  java program.
When the JVM exits, it will dump all the coverage data in destfile


When generating coverage report using maven-jacoco-plugin

1. copy coverage file (destfile) to Hudson/Jenkins view location. The Hudson/Jenkins view is the place where the jacoco report will be generated.
example: cp -f ${project.jacoco.path}/jacoco/jacoco.exec /test/view/myproject/target

2. copy all instrumented jars of java program to Jenkins view under the target/classes folder.  These jar files should be present in your java program's \lib folder
example: cp -rf /myproject/lib /test/view/myproject/target/classes

3. for line level coverage with sourcecode highlighting, copy all the *.java files to the view where the jacoco report is being generated.
example: rsync -avm --include='*.java' -f 'hide,! */' /myproject/src/main/java /test/view/myproject/src/main/java

4. invoke jacoco:report maven goal
jacoco:report

5. view the line level coverage report at /test/view/myproject/target/site/jacoco/index.html
For the line level coverage to work, the java code has to be compiled with debug information. see the configuration for below maven compiler plugin


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<debug>on</debug>
<debuglevel>lines,source</debuglevel>
</configuration>
</plugin>


<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.7.201204190339</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>        
</plugin>

No comments: