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.



Do you need to solve any of these problems in Java?

Convert PDF to HTML5
Convert PDF to HTML5
Convert PDF to SVG
Convert PDF to SVG
Convert PDF Forms to HTML5 Forms
View Forms in the browser
Java PDF Reader and Viewer
View PDF Documents
Convert PDF to image
Convert PDF to image
Extract Text from PDF
Extract Text from PDF
Read/Write images in Java
Read/Write images
Replace ImageIO
Replace ImageIO
Convert Image to PDF
Convert Image to PDF
Sylwia Dorota Kedzia Sylwia is a Java developer. She is very passionate about programming and all things Polish.

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 2021. All rights reserved.