Sylwia Dorota Kedzia Sylwia is a Java developer. She is very passionate about programming and all things Polish.

Creating your own custom PMD rulesets

1 min read

In my previous article “Java testing using FindBugs and PMD in NetBeans” I  explained how to use FindBugs and PMD plugins in NetBeans IDE. In this article I will explain how to easy it is to create your own customized  PMD rulesets.

Creating new rulesets .xml file

Each PMD ruleset is defined in a single XML file. Creating a new ruleset file is very simple. Just copy the code below into a new XML file, change the name, description and you are ready to add your rulesets.

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Custom ruleset"
<description>This ruleset detects bad practices in code my code</description>

<!-- Place to add your rules -->

Adding new rules

There are 2 ways that you can add particular set of rules:

1. You can either specify each rule that you wish to add

<!-- Braces Pmd rules: -->
<rule ref="rulesets/java/braces.xml/ForLoopsMustUseBraces"/>
<rule ref="rulesets/java/braces.xml/IfElseStmtsMustUseBraces"/>
<rule ref="rulesets/java/braces.xml/IfStmtsMustUseBraces"/>
<rule ref="rulesets/java/braces.xml/WhileLoopsMustUseBraces"/>

2. Or add full set of rules at once

<!-- Braces Pmd rules: -->
<rule ref="rulesets/java/braces.xml"/>

For full list of all available rulesets please visit the PMD website.

Excluding rules from a ruleset

Some of the rulesets contain many rules that you may not want to use. Therefore you can exclude some of the rules and here is how to do it:

<!-- Braces Pmd rules: -->
<rule ref="rulesets/java/braces.xml"/>
<exclude name="IfElseStmtsMustUseBraces"/>

Excluding files from rulesets

Here you can either exclude class or whole package from rulesets, which gives you more control over your application.

1. Excluding class

<!-- Check for Empty Catch blocks -->
<rule ref="rulesets/java/empty.xml/EmptyCatchBlock"/>

2. Excluding package

<!-- Check for Empty Catch blocks -->
<rule ref="rulesets/java/empty.xml/EmptyCatchBlock"/>

Additionally you can exclude particular rule violation in your own code classes using @SuppressWarnings such as:

private static void saveAsJPEG(final String jpgFlag) throws IOException {


Adding comments

To add comments to your xml file you can either add <description> or <!– –>. Notice that <!– –> will allow you comment out a blocks of tags.

<description>Basic Pmd rules:</description>

<!-- Basic Pmd rules: -->

Hopefully you found this guide useful, tune in next time.



IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Sylwia Dorota Kedzia Sylwia is a Java developer. She is very passionate about programming and all things Polish.

How to choose JPG versus JPEG2000 for image files

Since we started to support both JPG and JPG2000 as image file outputs in our software, we have found that this is a very...
Mark Stephens
1 min read

What’s new in Java13?

1 min read

2 Replies to “Creating your own custom PMD rulesets”

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.