<?xml version="1.0"?>
<document>
    <properties>
        <author email="tomcopeland@users.sourceforge.net">Tom Copeland</author>
        <title>How to make a rule set</title>
    </properties>
    <body>
        <section name="How to make a new rule set">
            <p>
Say you want to pick specific rules from various rule sets and customize them.  You can do this by making your own rule set.
            </p>
            <subsection name="Create a new ruleset.xml file">
                <p>Use one of the current rulesets as an example.  Copy and paste
    it into your new file, delete all the old rules from it, and change
    the name and description.  Like this:</p>
                <source><![CDATA[
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <description>
  This ruleset checks my code for bad stuff
  </description>
</ruleset>]]></source>
            </subsection>
            <subsection name="Add some rule references to it">
                <p>After you add these references it'll look something like this:</p>
                <source><![CDATA[
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">

  <description>
  This ruleset checks my code for bad stuff
  </description>

  <!-- We'll use the entire 'strings' ruleset -->
  <rule ref="rulesets/strings.xml"/>

  <!-- Here's some rules we'll specify one at a time -->
  <rule ref="rulesets/unusedcode.xml/UnusedLocalVariable"/>
  <rule ref="rulesets/unusedcode.xml/UnusedPrivateField"/>
  <rule ref="rulesets/imports.xml/DuplicateImports"/>
  <rule ref="rulesets/basic.xml/UnnecessaryConversionTemporary"/>

  <!-- We want to customize this rule a bit, change the message and raise the priority  -->
  <rule
   ref="rulesets/basic.xml/EmptyCatchBlock"
   message="Must handle exceptions">
    <priority>2</priority>
  </rule>

  <!-- Now we'll customize a rule's property value -->
  <rule ref="rulesets/codesize.xml/CyclomaticComplexity">
    <properties>
        <property name="reportLevel" value="5"/>
    </properties>
  </rule>

  <!-- Note we want everything from braces.xml except WhileLoopsMustUseBraces -->
  <rule ref="rulesets/braces.xml">
    <exclude name="WhileLoopsMustUseBraces"/>
  </rule>
</ruleset>]]></source>
                <p>Notice that you can customize individual referenced rules.  Everything but the
       class of the rule can be overridden in your custom ruleset.</p>
            </subsection>
            <subsection name="Excluding rules from a ruleset">
                <p>You can also make a custom ruleset that excludes rules, like this:</p>
                <source><![CDATA[
<?xml version="1.0"?>
<ruleset name="mybraces"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <description>Just the braces rules I like</description>
  <rule ref="rulesets/braces.xml">
    <exclude name="WhileLoopsMustUseBraces"/>
    <exclude name="IfElseStmtsMustUseBraces"/>
  </rule>
</ruleset>]]></source>
            </subsection>
            <subsection name="Excluding files from a ruleset">
                <p>You can also exclude certain files from being processed by a ruleset using exclude patterns,
                with an optional overriding include pattern.  A file will be excluded from processing when there
                is a matching exclude pattern, but no matching include pattern.  Path separators in the source
                file path are normalized to be the '/' character, so the same ruleset can be used on multiple
                platforms transparently.  Additionally, this exclude/include technique works regardless of how
                PMD is used (e.g. command line, IDE, Ant), making it easier to keep application of your PMD
                rules consistent throughout your environment.  Here is an example:</p>
                <source><![CDATA[
<?xml version="1.0"?>
<ruleset name="myruleset"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <description>My ruleset</description>
  <exclude-pattern>.*/some/package/.*</exclude-pattern>
  <exclude-pattern>.*/some/other/package/FunkyClassNamePrefix.*</exclude-pattern>
  <include-pattern>.*/some/package/ButNotThisClass.*</include-pattern>
  <rule>...
</ruleset>]]></source>
            </subsection>
            <subsection name="Reference it in your Ant task">
                <p>You can specify the full path to your custom ruleset name alongside
        of the built-in PMD rulesets - like this:</p>
                <source><![CDATA[<pmd rulesetfiles="/home/tom/data/pmd/pmd/rulesets/favorites.xml,rulesets/unusedcode.xml">
  <formatter type="xml" toFile="foo.xml"/>
  <fileset dir="/home/tom/data/pmd/pmd/src">
    <include name="**/*.java"/>
  </fileset>
</pmd>]]></source>
            </subsection>
            <subsection name="To see it in your IDE, add it to rulesets/rulesets.properties">
                <p>At least, that's the way some of the IDE plugins do it.  Some
    have other ways of adding custom rulesets.</p>
            </subsection>
            <subsection name="Send us feedback">
                <p>If you have suggestions on clarifying this document, please post
    them to
                    <a href="http://sourceforge.net/forum/forum.php?forum_id=188192">the forum</a>.  Thanks!
                </p>
                <p>Finally, for many more details on building custom rulesets, pick up <a href="http://pmdapplied.com/">PMD Applied</a>!</p>
            </subsection>
        </section>
    </body>
</document>
