Today I was experimenting with maven. I wanted to add code coverage. Ofcourse I start thinking about cobertura immediately. So I dived into cobertura and maven integration. It became kind of an experience. I know a lot more about maven now. I have to figure out some stuff, but for now it is good enough. This entry gives some steps to help you get up to speed with cobertura.

1. Install maven 2
quick start guide maven
2. Create an application that knows how to handle the goals package and site.
see the quick start guide
3. The fun part begins : compile and install your own cobertura plugin.
Download the sources from the codehaus subversion repository. You can use the following url to download them anonymously with a tool like tortoisesvn
https://svn.codehaus.org/mojo/trunk/mojo/mojo-sandbox/cobertura-maven-plugin
Then you have the sources. Now you need to alter the pom of that project to be able to check for latest versions. See the guide on the plugins website.

<repositories>
  <repository>
    <id>Maven Snapshots</id>
    <url>http://snapshots.maven.codehaus.org/maven2/</url>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>
<pluginRepositories>
  <pluginRepository>
    <id>Maven Snapshots</id>
    <url>http://snapshots.maven.codehaus.org/maven2/</url>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    <releases>
      <enabled>false</enabled>
    </releases>
  </pluginRepository>
</pluginRepositories>

Issue the following command for the cobertura-maven-plugin project you have just downloaded and altered.

mvn install

Now you are ready for the next step

4. Configure the pom.xml of your project to create the cobertura report.
There are two aspects, first you need to add the plugin to the build part, then to the reports part. See the usage page of the plugin home page

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>clean</goal>
          </goals>
        </execution>
      </executions>
    </plugin>  	
  </plugins>
</build>
<reporting>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
    </plugin>
  </plugins>
</reporting>

5. Run the site goal and start browsing your coverage files.
This is where I am having problems, it does work but you must do some parts alone. First I will give you what works, then I will give you what I am having problems with.

mvn clean package cobertura:cobertura
mvn site

The first thing that gives problems is doing the code coverage on the top project that does not have sources itself. An error due to the fact that target/classes is missing is thrown. The second problem I have is that I cannot do the two commands at once, there is a class loading issue.

I want to emphasize that this is a beginners perspective. The issues I am having might not be the complete truth. If you now more, or if I find out more, I will revise this part.

You can find the sources at my sample project at javaforge.
resources
http://mojo.codehaus.org/source-repository.html
http://cobertura.sf.net
http://mojo.codehaus.org/cobertura-maven-plugin

Cobertura and maven