2008-07-25

Slap your Java code hard with Maven and PMD

How good you think your code is? How can you be sure it's optimal, bug and bullet proof? Unit Tests? In case the coverage is good, they will tell if your code works, but will it tell if and where your code sucks in general? Let's get down to business.

As Maven is used de facto for Java builds, I assume you're using it. You may have heard of PMD, but have you tried it? If you have, did you know that it has a nice Maven Plugin? A quick way to integrate it:

Add the following to your pom.xml. Sadly, default check rulesets are too friendly, so you may want to try my configuration (even the "too hardcore" block):







org.apache.maven.plugins
maven-jxr-plugin


org.apache.maven.plugins
maven-pmd-plugin


rulesets/basic.xml
rulesets/braces.xml
rulesets/clone.xml
rulesets/codesize.xml
rulesets/coupling.xml
rulesets/favorites.xml
rulesets/finalizers.xml
rulesets/imports.xml
rulesets/junit.xml
rulesets/migrating_to_15.xml
rulesets/optimizations.xml
rulesets/typeresolution.xml
rulesets/unusedcode.xml
rulesets/strings.xml


true
true
utf-8
5
20
1.5








To generate a report, simply run:

mvn pmd:pmd

You may want to build Java cross reference for links to source code to work:

mvn jxr:jxr

Finally, open the report and see how naughty the code is:

target/site/pmd.html

Watch out for Cyclomatic Complexity!

2 comments:

  1. can you please correct your pom ? all nodes are lowercased ...

    ReplyDelete
  2. Oh, the XML got beautified somewhere along the way. Fixed it up. Thanks for pointing it out.

    ReplyDelete

Spam comments (i.e. ones that contain links to web development services) will be reported along with user profiles!

Note: only a member of this blog may post a comment.