Code Quality Reporting with Maven

September 2016

By improving the code quality of a program, one will also improve its robustness and maintainability. This guide will teach you how to generate code quality reports for a Maven project.


How To Generate Project Site and Reports with Maven

Maven gives you the ability to generate a HTML site that describes your project. You can use the
mvn site
command to generate your project site.

Once done, open the target/site/index.html file to view the site:


The project site will usually contain technical information about the project’s architecture (dependencies, plug-ins…); you will need to enrich it with the data generated by Maven's report plug-ins (as defined in your project configuration file).

Code Analysis and Reporting Plug-ins for Maven

Below is an excerpt that can be incorporated into your configuration file (pom.xml). It features a couple of standalone plug-ins for code analysis and reporting. Each plug-in will generate a specific report on the project site.


<build>
  <plugins>
    <plugin>
      <!-- The Failsafe Plugin is designed to run integration tests while the 
      Surefire Plugin is designed to run unit tests. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.19.1</version>
      <executions>
        <execution>
          <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
  <reporting>
    <plugins>
      <plugin>
        <!-- The JXR Plugin generates a cross-reference of the project's sources. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jxr-plugin</artifactId>
      <version>2.5</version>
    </plugin>
    <plugin>
      <!-- The Javadoc Plugin generates javadocs using the Javadoc tool. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
      <version>2.10.3</version>
      <configuration>
        <additionalparam>-Xdoclint:none</additionalparam>
      </configuration>
    </plugin>
    <plugin>
      <!-- The Surefire Report Plugin generates reports for unit and integration 
        tests. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>2.19.1</version>
    </plugin>
    <plugin>
      <!-- The Checkstyle Plugin generates a report regarding the code style 
        used by the developers. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>2.17</version>
    </plugin>
    <plugin>
      <!-- The PMD plugin generates PMD and CPD reports using the PMD code 
        analysis tool. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-pmd-plugin</artifactId>
      <version>3.6</version>
      <configuration>
        <skipEmptyReport>false</skipEmptyReport>
        <linkXref>true</linkXref>
        <minimumTokens>50</minimumTokens>
        <rulesets>
          <ruleset>rulesets/java/android.xml</ruleset>
          <ruleset>rulesets/java/basic.xml</ruleset>
          <ruleset>rulesets/java/braces.xml</ruleset>
          <ruleset>rulesets/java/clone.xml</ruleset>
          <ruleset>rulesets/java/codesize.xml</ruleset>
          <ruleset>rulesets/java/comments.xml</ruleset>
          <!-- <ruleset>rulesets/java/controversial.xml</ruleset> -->
          <ruleset>rulesets/java/coupling.xml</ruleset>
          <ruleset>rulesets/java/design.xml</ruleset>
          <ruleset>rulesets/java/empty.xml</ruleset>
          <ruleset>rulesets/java/finalizers.xml</ruleset>
          <ruleset>rulesets/java/imports.xml</ruleset>
          <ruleset>rulesets/java/j2ee.xml</ruleset>
          <ruleset>rulesets/java/javabeans.xml</ruleset>
          <ruleset>rulesets/java/junit.xml</ruleset>
          <ruleset>rulesets/java/logging-jakarta-commons.xml</ruleset>
          <ruleset>rulesets/java/logging-java.xml</ruleset>
          <ruleset>rulesets/java/migrating.xml</ruleset>
          <ruleset>rulesets/java/migrating_to_13.xml</ruleset>
          <ruleset>rulesets/java/migrating_to_14.xml</ruleset>
          <ruleset>rulesets/java/migrating_to_15.xml</ruleset>
          <ruleset>rulesets/java/migrating_to_junit4.xml</ruleset>
          <ruleset>rulesets/java/naming.xml</ruleset>
          <ruleset>rulesets/java/optimizations.xml</ruleset>
          <ruleset>rulesets/java/strictexception.xml</ruleset>
          <ruleset>rulesets/java/strings.xml</ruleset>
          <ruleset>rulesets/java/sunsecure.xml</ruleset>
          <ruleset>rulesets/java/typeresolution.xml</ruleset>
          <ruleset>rulesets/java/unnecessary.xml</ruleset>
          <ruleset>rulesets/java/unusedcode.xml</ruleset>
        </rulesets>
      </configuration>
    </plugin>
    <plugin>
      <!-- FindBugs Plugin looks for bugs in Java programs. It is based on 
        the concept of bug patterns. -->
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>findbugs-maven-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>
  </plugins>
</reporting>



N.B. These plug-ins were given as examples. Click plug-ins to discover the full list of available plug-ins.

How To Generate Reports in Maven

Before you create the project site, it is important to run unit and integration tests for each of the new plug-ins. You can, for example, use the
mvn verify site
command to generate a project site that will already include the results of the unit and integration tests.

How To View Reports in Maven

Click on My Project > Project Documentation > Project Reports to view the reports:


Source XRef

This report will allow you to view the source code of the project:


Test Source Xref

This report will allow you to view a cross-reference version of the project's test source code.

Java Docs

This report will contain the JavaDoc API documentation:


Test Java Docs

This report will contain the Test JavaDoc API documentation.

Surefire Report

This report will contain the results for unit testing:


Failsafe Report

This report will contain the results for integration testing.

Checkstyle

This report will list all the violations of the standard syntax rules in Java:


The rules behind the errors messages can be edited through a Custom Checkstyle Checker Configuration.

CPD

This report will highlight duplicate code blocks in the project:


Edit the
<minimumTokens>value</minimumTokens>
parameter to define the minimum code length.

PMD

This report will highlight poor code construction (in violation with the current PMD Ruleset) that may potentially lead to errors:


Edit the
<rulesets>
parameter to change the default PMD Ruleset.

FindBugs

Maven will create a source code report with FindBugs Library. This extensive report will list the possible errors in your code:


Compile Your Reports with SonarQube

SonarQube is an open source platform that allows you to compile the information from your various reports, assess the quality of your code, and compare your project with others.

Related :

This document entitled « Code Quality Reporting with Maven » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.