Emma reporting in Maven build

How to produce html Emma report automatically with Maven build. Useful for continuous build environments.
First, produce the report using the Emma Maven plugin like so (see http://mojo.codehaus.org/emma-maven-plugin/usage.html):

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>emma-maven-plugin</artifactId>
	<version>1.0-alpha-3</version>
	<inherited>true</inherited>
	<executions>
		<execution>
			<phase>process-classes</phase>
			<goals>
				<goal>instrument</goal>
			</goals>
		</execution>
	</executions>
</plugin>
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<inherited>true</inherited>
	<configuration>
		<forkMode>once</forkMode>
		<reportFormat>txt</reportFormat>
		<classesDirectory>${project.build.directory}/generated-classes/emma/classes</classesDirectory>
		<argLine>-XX:-UseSplitVerifier</argLine>
	</configuration>
</plugin>

Next, use the exec-maven-plugin to process the Emma output into the format you want. This example shows html and txt.

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>exec-maven-plugin</artifactId>
	<version>1.2.1</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>exec</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<executable>java</executable>
		<arguments>
			<argument>-cp</argument>
			<argument>${project.build.directory}/lib/*</argument>
			<argument>emma</argument>
			<argument>report</argument>
			<argument>-r</argument>
			<argument>html,txt</argument>
			<argument>-in</argument>
			<argument>${project.build.directory}/coverage.em,${basedir}/coverage.ec</argument>
		</arguments>
	</configuration>
</plugin>

Note, the phase for this step is set to “package”, which comes after “process-classes” (see http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html). Also, this example relies on the fact that I have configured maven-dependency-plugin to include my dependencies in the …/lib directory. Example:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.6</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/lib</outputDirectory>
				<overWriteReleases>false</overWriteReleases>
				<overWriteSnapshots>false</overWriteSnapshots>
				<overWriteIfNewer>true</overWriteIfNewer>
			</configuration>
		</execution>
	</executions>
</plugin>

With this example, I get my html file in ${project.build.directory}/converage/index.html which I can then specify as a artifact in my continuous build environment (such as Team City) and easily access on any historical build.

Specifying logback configuration when using maven exec:exec

If you don’t want your logback configuration stored in your jar, but you want to be able to execute your java program with the exec-maven-plugin (using mvn exec:exec), you can specify the default logback configuration as follows. More details on logback configuration can be found here: http://logback.qos.ch/manual/configuration.html

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>exec-maven-plugin</artifactId>
	<version>1.2.1</version>
	<executions>
		<execution>
			<goals>
				<goal>exec</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<executable>java</executable>
		<arguments>
			<argument>-cp</argument>
			<classpath/>
			<argument>-Dlogback.configurationFile=${basedir}/src/main/resources/logback.xml</argument>
			<argument>com.possemeeg.test.experiment.Program</argument>
		</arguments>
	</configuration>
</plugin>