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.

Mick Knutson

Java, JavaEE, J2EE, WebLogic, WebSphere, JBoss, Tomcat, Oracle, Spring, Maven, Architecture, Design, Mentoring, Instructor and Agile Consulting. http://www.baselogic.com/blog/resume

View all posts

Java / JavaEE / Spring Boot Channel

BLiNC Supporters

BLiNC Adsense

Archives

Newsletter