January 30, 2010

Maven Reporting with Cobertura, Dashboard, Change Report, FindBugs and PMD

I have been using maven coberture plugin before but it has one unimplemented function, aggregate cobertura report from multiple module. This can be solved by sonar and dashboard maven plugin, but Sonar requires that is run on a server, i.e. that you have a continuous integration server, but in some cases you have not got there and still is building your application locally than you can use the maven Dashboard plugin.


The parent pom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>se.msc.examples</groupId>
 <artifactId>reporting-parent</artifactId>
 <version>0.1-SNAPSHOT</version>
 <packaging>pom</packaging>
 <url>http://www.msc.se/examples</url>
    <organization>
        <name>Msc.se</name>
    </organization>
    <developers>
     <developer>
      <name>Magnus K Karlsson</name>
      <email>magnus.k.karlsson@msc.se</email>
     </developer>
    </developers>
    
 <modules>
  <module>reporting-demo</module>
  <module>reporting-core</module>
 </modules>
 
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.2</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
     <encoding>UTF-8</encoding>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <reporting>
  <plugins>
   <!-- Generate 'Changes Report' from src/changes/changes.xml -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-changes-plugin</artifactId>
    <version>2.3</version>
    <reportSets>
     <reportSet>
      <reports>
       <report>changes-report</report>
      </reports>
     </reportSet>
    </reportSets>
   </plugin>

   <!-- JXR - Source code as HTML --> 
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jxr-plugin</artifactId>
    <version>2.1</version>
    <configuration>
     <!-- Enable aggregation for multimodule projects. -->
     <aggregate>true</aggregate>
     <inputEncoding>utf-8</inputEncoding>
     <outputEncoding>utf-8</outputEncoding>
    </configuration>
   </plugin>

   <!-- JavaDoc - API-documentation -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.6.1</version> 
    <configuration>
     <!-- Enable aggregation for multimodule projects. -->
     <aggregate>true</aggregate>
                    <show>public</show>
                    <charset>utf-8</charset>
                    <docencoding>utf-8</docencoding>
                    <encoding>utf-8</encoding>     
    </configuration>
   </plugin>

   <!-- Surefire - JUnit testing-->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-report-plugin</artifactId>
    <version>2.5</version>
    <configuration>
     <!-- Required to properly link JXR -->
     <xrefLocation>${project.reporting.outputDirectory}/../xref-test</xrefLocation>
    </configuration>
   </plugin>

   <!-- JDepend - Package dependencies -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jdepend-maven-plugin</artifactId>
    <version>2.0-beta-2</version>
   </plugin>

   <!-- Cobertura - Test code coverage report. -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.3</version>
   </plugin>
   
   <!-- PMD - Generate PMD and CPD reports using the PMD code analysis tool. -->
            <plugin>
             <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                 <linkXref>true</linkXref>
                 <!-- Required to properly link JXR -->
     <xrefLocation>${project.reporting.outputDirectory}/../xref-test</xrefLocation>
                 <sourceEncoding>utf-8</sourceEncoding>
                    <aggregate>true</aggregate>
                    <targetJdk>1.5</targetJdk>
                </configuration>
            </plugin>
            
   <!-- FindBugs - Finds potential bugs in your source code -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>2.3</version>
    <configuration>
     <xmlOutput>true</xmlOutput>
     <effort>Max</effort>
    </configuration>
   </plugin>

   <!-- JavaNCSS - Source code metrics -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>javancss-maven-plugin</artifactId>
    <version>2.0</version>
   </plugin>

   <!-- TagList - Creates a list with TODO:s etc -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>taglist-maven-plugin</artifactId>
    <version>2.4</version>
                <configuration>
                    <aggregate>true</aggregate>
                    <tags>
                        <tag>TODO</tag>
                        <tag>FIXME</tag>
                        <tag>@todo</tag>
                    </tags>
                </configuration>    
   </plugin>

   <!-- The dashboard plugin should be specified as the last report. -->
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>dashboard-maven-plugin</artifactId>
    <version>1.0.0-beta-1</version>
   </plugin>

  </plugins>
 </reporting>

</project>



The jar module pom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <parent>
  <groupid>se.msc.examples</groupId>
  <artifactid>reporting-parent</artifactId>
  <version>0.1-SNAPSHOT</version>
 </parent>
 <modelversion>4.0.0</modelVersion>
 <artifactid>reporting-demo</artifactId>
 <packaging>jar</packaging>
 <name>Demo Reporting</name>

 <reporting>
  <!-- Needed in order to generate the dashboard report properly. -->
  <outputdirectory>
            ${basedir}/../target/site/${project.artifactId}
        </outputDirectory>
 </reporting>

</project>

No comments: