Centralizing PMD rules for use with Maven.

PMD has long been a great report to find out many useful details of your code base.

I find that the default ruleset does not contain some checks that I find very useful especially when I have more junior developers. I can catch many problematic errors.

<?xml version=”1.0″?>

&lt;ruleset name="Custom ruleset"
 xmlns="http://pmd.sf.net/ruleset/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
 xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"&gt;

 &lt;description&gt;
 This ruleset checks my code for bad stuff
 &lt;/description&gt;

 &lt;!-- Here's some rules we'll specify one at a time --&gt;
 &lt;rule ref="rulesets/basic.xml"/&gt;

 &lt;!-- We want to customize this rule a bit, change the message and raise the priority  --&gt;
 &lt;rule ref="rulesets/basic.xml/EmptyCatchBlock"
 message="Must handle exceptions"&gt;
 &lt;priority&gt;2&lt;/priority&gt;
 &lt;/rule&gt;

 &lt;rule ref="rulesets/codesize.xml"/&gt;
 &lt;rule ref="rulesets/codesize.xml/CyclomaticComplexity"&gt;
     &lt;properties&gt;
         &lt;property name="reportLevel" value="5"/&gt;
     &lt;/properties&gt;
 &lt;/rule&gt;
 &lt;rule ref="rulesets/controversial.xml" /&gt;
 &lt;rule ref="rulesets/coupling.xml" /&gt;

 &lt;rule ref="rulesets/design.xml" /&gt;
 &lt;rule ref="rulesets/imports.xml" /&gt;
 &lt;rule ref="rulesets/logging-java.xml" /&gt;

 &lt;rule ref="rulesets/optimizations.xml" /&gt;
 &lt;rule ref="rulesets/strictexception.xml" /&gt;
 &lt;rule ref="rulesets/strings.xml" /&gt;
 &lt;rule ref="rulesets/sunsecure.xml" /&gt;
 &lt;rule ref="rulesets/typeresolution.xml" /&gt;
 &lt;rule ref="rulesets/unusedcode.xml" /&gt;

 &lt;rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" /&gt;
 &lt;rule ref="rulesets/unusedcode.xml/UnusedPrivateField" /&gt;

 &lt;!-- Note we want everything from braces.xml except WhileLoopsMustUseBraces --&gt;
 &lt;rule ref="rulesets/braces.xml"&gt;
     &lt;exclude name="WhileLoopsMustUseBraces"/&gt;
 &lt;/rule&gt;
&lt;/ruleset&gt;

I uploaded my configuration to my server (http://www.baselogic.com/pmd-rules.xml), then I can access this on any project I am working on:
&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-pmd-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
    &lt;rulesets&gt;
        &lt;ruleset&gt;http://www.baselogic.com/pmd-rules.xml&lt;/ruleset&gt;
    &lt;/rulesets&gt;
    &lt;linkXref&gt;true&lt;/linkXref&gt;
    &lt;targetJdk&gt;${compiler.version}&lt;/targetJdk&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

If you find you like your PMD a bit different, just customize the rules and put onto your webserver, then you can carry your rules with you.

VN:F [1.9.1_1087]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.1_1087]
Rating: 0 (from 0 votes)
  • Share/Bookmark

This entry was posted on Monday, November 23rd, 2009 at 5:21 pm and is filed under Java-JavaEE-J2EE. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Centralizing PMD rules for use with Maven.”

  1. willcode4beer Says:

    I’m a big fan of using Sonar (http://sonar.codehaus.org) to display the results of PMD.

    I use Hudson to push the reports on every build. Combining with the Continuous Integration Game plugin gives points for improving code. The bonus comes from anyone being able to add mini-improvements whenever they get a free 5 minutes.

    VN:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.1_1087]
    Rating: 0 (from 0 votes)

Leave a Reply

You must be logged in to post a comment.