PMD allows you to perform code static analysis for your project, but sometimes the default doesn’t fit the way you decided to write code. The good thing is that you can customize the rules you want to use to fit your preferences.

To customize the rules you will need to create an xml file with this structure:

1
2
3
4
5
6
7
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
   xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>Rules for my project</description>
</ruleset>

Then you can add rules you want to use:

1
<rule ref="rulesets/java/clone.xml" />

Similar rules come packed together some times, but if for some reason there is a rule you don’t want to use you can exclude it:

1
2
3
<rule ref="rulesets/java/comments.xml">
    <exclude name="CommentSize" />
</rule>

There are some rules that allow you to customize some values. For my project I wanted to allow more methods per class than the default (10). So I had to do this:

1
2
3
4
5
6
<rule ref="rulesets/java/codesize.xml" />
<rule ref="rulesets/java/codesize.xml/TooManyMethods">
    <properties>
        <property name="maxmethods" value="20" />
    </properties>
</rule>

The PMD configuration file for my project looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <description>My project rules</description>

    <rule ref="rulesets/java/android.xml" />
    <rule ref="rulesets/java/basic.xml" />
    <rule ref="rulesets/java/braces.xml" />
    <rule ref="rulesets/java/clone.xml" />
    <rule ref="rulesets/java/codesize.xml" />
    <rule ref="rulesets/java/codesize.xml/TooManyMethods">
        <properties>
            <property name="maxmethods" value="20" />
        </properties>
    </rule>
    <rule ref="rulesets/java/comments.xml">
        <exclude name="CommentSize" />
    </rule>
    <rule ref="rulesets/java/controversial.xml">
        <exclude name="OnlyOneReturn" />
        <exclude name="AvoidLiteralsInIfCondition" />
        <exclude name="DataflowAnomalyAnalysis" />
    </rule>
    <rule ref="rulesets/java/design.xml" />
    <rule ref="rulesets/java/empty.xml" />
    <rule ref="rulesets/java/finalizers.xml" />
    <rule ref="rulesets/java/imports.xml" />
    <rule ref="rulesets/java/j2ee.xml" />
    <rule ref="rulesets/java/junit.xml" />
    <rule ref="rulesets/java/naming.xml">
        <exclude name="LongVariable" />
        <exclude name="ShortVariable" />
    </rule>
    <rule ref="rulesets/java/optimizations.xml" />
    <rule ref="rulesets/java/strictexception.xml" />
    <rule ref="rulesets/java/strings.xml" />
    <rule ref="rulesets/java/sunsecure.xml" />
    <rule ref="rulesets/java/typeresolution.xml" />
    <rule ref="rulesets/java/unnecessary.xml">
        <exclude name="UselessParentheses" />
    </rule>
    <rule ref="rulesets/java/unusedcode.xml" />
</ruleset>

To run PMD using the custom rules you defined you can use:

1
pmdRun.sh pmd -d src/ -f text -R pmd_rules.xml
[ automation  java  productivity  ]
Monitoring Kubernetes Resources with Fabric8 Informers
Managing Kubernetes Objects With Yaml Configurations
Introduction to Bazel
Monetizing a Jekyll blog with Adsense
Introduction to Simple Workflow Service (SWF)