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"
    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>
    This ruleset checks my code for java statndards
  </description>
</ruleset>

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="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>
     	PMD rule set by Soha
    </description>

	<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" />
	
</ruleset>

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"/>
    </rule>
Will have ruleset with includes and excludes will try to test ruleset xml
<?xml version="1.0"?>
<ruleset name="jig jig cha" 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>PMD rule set by Soha</description>

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


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.
<plugin>
   <artifactId>maven-pmd-plugin</artifactId>
   <configuration>
      <includeTests>true</includeTests>
      <minimumTokens>100</minimumTokens>
      <linkXref>true</linkXref>
      <targetJdk>${targetJdk}</targetJdk>
      <rulesets>
         <ruleset>${pmd-config-url}</ruleset>
      </rulesets>
      <failOnViolation>true</failOnViolation>
   </configuration>
   <executions>
      <execution>
         <phase>test</phase>
         <goals>
            <goal>pmd</goal>
            <goal>cpd</goal>
            <goal>cpd-check</goal>
            <goal>check</goal>
         </goals>
      </execution>
   </executions>
</plugin>

Place rulset xml in a specific location and enter that location for pmd-config-url property
<properties>
   <pmd-config-url>
      C:/PMD/rulesets/pmd/2.0/pmd-2.0.xml
   </pmd-config-url>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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