Code Analysis using PMD and Checkstyle

java Add comments

We started code review using Crucible, so i searched for some code analysis maven plugins. PMD is a Java code analysis tool and used to find potential problems like unused code, duplicate code, unused variables… PMD is useful and easy:

pom.xml:

    <reporting>
        <outputDirectory>target/reporting/pmd</outputDirectory>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-pmd-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>

If you get the following error, simply add target-jdk configuration:
Caused by: net.sourceforge.pmd.ast.ParseException: Can’t use generics unless running in JDK 1.5 mode!

    <reporting>
        <outputDirectory>target/reporting/pmd</outputDirectory>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-pmd-plugin</artifactId>
              <configuration>
                  <targetJdk>1.5</targetJdk>
              </configuration>
            </plugin>
        </plugins>
    </reporting>

The PMD report is at pmd.html, but i couldn’t find categories, and more detailed reports.

We can also customize PMD rules & rulesets:

How to write a PMD rule?
How to make a new rule set?

CheckStyle is also a strong Maven 2 plugin for java code analysis. Simply running mvn checkstyle:checkstyle gives us detailed html/rss report(mvn site gives heap or memory error for some projects).

    <reporting>
    ……
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>

We can customize the reports generated by Maven’s site plugin. Simply add or overwrite <reports> tag at project.xml. The result gives us reports of project like dependencies, project info, project reports (CMD and PMD reports included), source repository, …

<reports>
    <report>index</report>
    <report>dependencies</report>
    <report>project-team</report>
    <report>mailing-list</report>
    <report>cim</report>
    <report>issue-tracking</report>
    <report>license</report>
    <report>scm</report>
    <report>maven-pmd-plugin</report>
    <report>maven-checkstyle-plugin</report>
</reports>
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

Leave a Reply