Using custom rule sets with the plug-in is quite simple. PMD searches the rule sets files on the classpath defined for the plug-in. So, to use custom rule sets, they must be on the PMD Plug-in classpath.
The way to do that with Eclipse is to package the rule sets in a plug-in fragment. A fragment is an extension of a plug-in and all the classes and ressource files it contains are automatically added to the main plug-in classpath.
The steps to add a custom rule set are :
The following details these steps.
The first step is to setup the plug-in development environment. Open the Preferences dialog box. Select Target platform in the Plug-in devlopment category. Then push the Select All button and click OK to close the window.
The second step is to create a plug-in project for the fragment.
The last step is to configure the plug-in manifest.
net.sourceforge.pmd.eclipseIn the Plug-in version field type the PMD plug-in version (ex. 1.0.0). In the Match Rule field, select how you want to be bound with the PMD Plug-in. A good choice is
Greater or equalso that if the PMD Plug-in is upgraded, the fragment will still be activated.
The more complex task is done ;-)
Check the PMD official documentation to learn how to develop rule classes.
This step is simply to create one or more XML rule set files. Place them in the source directory. Read the PMD documentation to learn how to write rule set files.
Before deploying the fragment, it is necessary to test it. To do that, the simplest way is to launch a test Eclipse environment :
In the Run menu, select Run as->Run-time workbench. This will launch a secondary workbench with all the installed plug-ins and the fragment currently in development. In that workbench, create a test Java project to test the rule set. (see step 6 to learn how to configure the PMD Plug-in).
Before deploying the fragment, it is necessary to package it. Export the project with the export wizard and select Deployable plug-ins and fragments. Select the fragment project and specify the zip file that will form the distribution package.
Unzip the distribution package on the target environment. If the target environment is Eclipse, then the Eclipse install directory must be "eclipse" ; unzip the distribution file from the parent directory (the directory in the distribution file begins with "eclipse/plugins...").
If the target environment is WSAD, unzip the distribution file from the installation path of WSAD.
To activate the custom rule set, open the preferences window and select the Ruleset selection tab in the PMD category. Type the path and name of the custom rule set file in the combo box and press the Add rule set button. The custom rule set is added to the list. Press OK to close the Preferences dialog.
Now, the custom rule set file will be used each time a PMD check will occur.