My new website on Artificial Intelligence and Machine Learning

How to make a pmd rule set in maven

Say you want to pick specific rules from various rule sets and customize them. You can do this by making your own rule set..

Create a new ruleset.xml file

This is the sample schema for pmd ruleset and make sure whenever you create ruleset file seperately change the name and description. This schema supports maven3(2.0.0). Maven2 suppports 1.0.0 scema vesrion..
<?xml version="1.0"?>
<ruleset name="Custom ruleset for PMD"
    This ruleset checks my code for java statndards

Add some rule references to it
After you add these references it'll look something like this.Notice that you can customize individual referenced rules. Everything but the class of the rule can be overridden in your custom ruleset.
<?xml version="1.0"?>
<ruleset name="jig jig cha" xmlns=""

     	PMD rule set by Soha

	<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField" />
	<rule ref="rulesets/java/imports.xml/DuplicateImports" />
	<rule ref="rulesets/java/basic.xml/UnnecessaryConversionTemporary" />
	<rule ref="rulesets/java/strings.xml" />

Different ways Syntax to follow
  • Specific rules for different tokens like java, jsp, plsql, xml and xsl etc..
         <rule ref="rulesets/java/unusedcode.xml" />

    we have different combination like below:
        <rule ref="rulesets/jsp/basic-jsf.xml" />
        <rule ref="rulesets/plsql/codesize.xml" />

  • Each rule will have rules inside a rule
        <rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField" />
        <rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField"/ >

  • Exclude rule from specific rule xml
    <rule ref="rulesets/java/braces.xml">
        <exclude name="WhileLoopsMustUseBraces"/>
Will have ruleset with includes and excludes will try to test ruleset xml
<?xml version="1.0"?>
<ruleset name="jig jig cha" xmlns=""

	<description>PMD rule set by Soha</description>

	<rule ref="rulesets/java/unusedcode.xml">
		<exclude name="UnusedLocalVariable"/>
	<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField" />

PMD & CPD setup for the Maven
Add plugin information (inside plugins tag) to pom.xml for the project wise. Otherwise if we have common parent for all the projects, better we can add, we can utilize for all the projects.

Place rulset xml in a specific location and enter that location for pmd-config-url property

Write a class with unncessary private variable and unecessary local variable. Try to excude and include UnusedPrivateField and UnusedLocalVariable in ruleset, check how will it through pmd violatons in mvn clean install or mvn pmd:check
package com.javavillage.dozer;

import java.util.ArrayList;
import java.util.List;

public class MainDozer {
	private String str;

	public static void main(String args[]) {
		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		List<String> list3 = new ArrayList<String>();
		List<String> list = new ArrayList<String>();

		System.out.println("Hi PMD");


Note: Here we have two points keep in mind
  • How to identify remaining rules to include
    Ans: Just extract pmd jar(pmd-5.1.0.jar) you can see folder called rulset and open and check you will find the rules.

  • If we don't specify any rule which rules will get execute
    Ans: This will be based on maven pmd plugin. Inside maven-pmd-plugin we have default xml file called maven.xml for to throw PMD violations