##### For example

- Run PMD on a Sourceforge project

## Integrations with IDEs

### Summary

IDE | CVS Module | Maintainers
BlueJ | pmd-bluej | Tom Copeland
CodeGuide | N/A | Austin Moore
Eclipse | pmd-eclipse | Philippe Herlin
Emacs | pmd-emacs | Nascif Abousalh Neto
Gel | pmd-gel | Andrei Lumianski
IntelliJ IDEA | N/A | Tom Copeland
JBuilder | pmd-jbuilder | Tom Copeland
JCreator | N/A | Brant Gurganus
JDeveloper | pmd-jdeveloper | Tom Copeland
JEdit | pmd-jedit | Jiger Patel
Maven | N/A | Vincent Massol
Maven 2 | N/A | Vincent Massol
NetBeans/Sun Java Studio Enterprise/Creator | pmd-netbeans | Radim Kubacki
TextPad | N/A | Jeff Epstein

### General comments

A general note - most plugins include the PMD jar file, which has the rulesets inside it. So even though the rulesets parameter that some plugins use (i.e., "rulesets/unusedcode.xml") looks like a filesystem reference, it's really being used by a getResourceAsStream() call to load it out of the PMD jar file.

### BlueJ

BlueJ is a teaching IDE. To install the PMD extension for BlueJ, download the PMDExtension jar file and place it in your `bluej/lib/extensions/` directory.

### Code Guide

Here's how to set up PMD with Omnicore's CodeGuide (taken from here):

- Download the PMD zip file from here
- Unpack the zip file to your local disk. I put it in /Applications/
- Add the following tool to CodeGuide (Tools->Configure Tools->New):

- Name: PMD
- Kind: Tool for directories
- Executable: /Applications/pmd-3.7/bin/pmd.sh
- Arguments: $DIRECTORY_PATH$ emacs $USER_ARGUMENTS$

Now you can right click on a source directory, select the PMD tool and a dialog box will appear. Type in the name of a rule set (e.g. unusedcode). PMD is then executed on the directory you selected with the rule set you specified. Since CodeGuide understands the emacs syntax you can click on the file name and CodeGuide will jump to that file and line number.

Note: I set this up on Mac OS X. It should work on other unix variations just the same. Of course you need to change the "Executable" setting above to where your PMD is installed. On Windows you need to use the pmd.bat file instead of pmd.sh.

If you haven't heard of CodeGuide before, check it out. It's a great IDE.

### Eclipse

To install the PMD plugin for Eclipse:

- Start Eclipse and open a project
- Select "Help"->"Software Updates"->"Find and Install"
- Click "Next", then click "New remote site"
- Enter "PMD" into the Name field and "http://pmd.sf.net/eclipse" into the URL field
- Click through the rest of the dialog boxes to install the plugin

Alternatively, you can download the latest zip file and follow the above procedures except for using "New local site" and browsing to the downloaded zip file.

To configure PMD, select "Windows"->"Preferences", then select PMD.

To run PMD, right-click on a project node and select "PMD"->"Check code with PMD".

To run the duplicate code detector, right-click on a project node and select "PMD"->"Find suspect cut and paste". The report will be placed in a "reports" directory in a file called "cpd-report.txt".

To find additional help for other features, please read included help by selecting Help->Help Contents and browse the "How to..." section in the "PMD Plugin Documentation" book.

After installing an update, if you get an Exception such as "java.lang.RuntimeException: Couldn't find that class xxxxx", try deleting the ruleset.xml file in the .metadata/plugins/net.sourceforge.pmd.eclipse directory in your workspace.

### Emacs

Integration with GNU Emacs is performed through an ELisp package, pmd.el. It supports two commands, "pmd-current-buffer" and "pmd-current-dir". The output is captured in a compilation buffer which allows the user to "jump" directly to the source code position associated with the PMD warnings.

### Gel

Here's how to set up the Gel plugin:

- Download the pmd-gel-[version].zip file
- Close Gel
- Remove any old plugin versions from your gel\plugins directory
- Unzip the new zip file into your gel\plugins directory
- Start Gel
- Go to Tools->Options->Plugin
- Select the PMD plugin and click "Remove"
- Click "Add" and select "net.sourceforge.pmd.gel.PMDPlugin"
- Restart Gel

That's pretty much it. Now you can open a Java project and click on Plugins->PMD and a configuration panel will pop up. You can pick which ruleset you want to run and you can also pick whether you want to run PMD on the current file or on every source file in your project.

### IDEA

There's not a "real" - i.e., integrated into the same Java VM - plugin for IDEA, but using it as an IDEA "External Tool" works OK. Here's how to set it up:

- Open IDEA and go to File->Settings
- Click on the "External Tools" icon
- Click on the Add button
- Fill in the blocks

- Name: PMD
- Description: PMD, good for what ails you.
- Menu: Select the "Main menu", "Project views", "Editor menu", and "Search results" checkboxes.
- Program: $JDKPath$\bin\java.exe
- For the next parameter you'll need to plug in the location of your PMD installation and the rulesets you want to use
- Parameters: -cp %CLASSPATH%;c:\pmd\lib\pmd-3.7.jar;c:\pmd\lib\jakarta-oro-2.0.8.jar;c:\pmd\lib\jaxen-1.1-beta-7.jar net.sourceforge.pmd.PMD "$FilePath$" ideaj unusedcode,imports "$Sourcepath$" $FileClass$.method $FileName$

That's pretty much it. Now you can right click on a source directory and select PMD, it'll run recursively on the source files, and the results should be displayed in a window and hyperlinked into the correct file and line of code. I usually right-click on the message window title bar and unselect "autohide" so the window doesn't go away every time I fix something in the code window.

### JBuilder

To enable this OpenTool in JBuilder:

- Download the latest binary release.
- Unzip it into your `jbuilder/lib/ext/` directory
- Restart JBuilder

- Check a single file by bringing up the context menu from the file tab and selecting PMDCheck
- Configure the rulesets that the PMD OpenTool will use by selecting Tools->PMD->Configure PMD
- Check all the files in a project by bringing up the context menu for the project node and selecting PMD Check Project
- Locate duplicate code by right clicking on a package and selection "Check with CPD"

When running PMD, the results will be displayed in the MessageView under a tab called PMD Results. If you click on a violation message within this view, you will be taken to the line in the source code where the violation was detected.

Things still to do:

- Enable selection of individual rules within a rule set (maybe)
- Optional insertion of @todo comments at the point of a violation
- Possibly provide configurable ability to limit the number of violations per rule per file

### JCreator

1. Open Configure > Options
2. Go to the Tools panel
3. Click New > Program
4. Browse for PMD's pmd.bat
5. Put quotations around the path if it has spaces.
6. Set the initial directory to PMD's \bin directory
7. Check capture output
8. Put '"$[PrjDir]" emacs' followed by desired rulesets in the arguments

To run PMD on a project, just pick pmd from the Tools menu.

### JDeveloper

To install the extension for JDeveloper 10.1.2, download the binary release and unzip it into your jdev/lib/ext directory. Then restart JDeveloper. To use it, open the Tools-&Preferences menu, click on the PMD option, and select a couple of rules to try. To run it, right click on either a Java source file or a .jpr file (i.e., a project file) and select PMD. Any rule violations should show up in a LogWindow at the bottom of the screen.

To install the extension for JDeveloper 10.1.3, use JDeveloper's built in "update center" functionality. Fire up JDeveloper, click "Help", click "Check for Updates", then click "Add" to add a new update center and enter:

- Name: PMD Update Center
- Location: http://pmd.sf.net/center.xml

Click your way through the ensuing dialogs and you'll install the plugin. Note that I've set up the plugin for the latest JDeveloper 10.1.3 EA, so if you're using an earlier preview release of 10.1.3 it might not work.

### JEdit

The way I use the JEdit plugin is:

- Dock the ErrorList by going to Utilities->Global Options->Docking and putting ErrorList at the bottom of the screen
- Open the File Browser if it isn't already open
- Double-click on a source directory
- Select Plugins->PMD->Check directory recursively

Note that you can select individual rules by going to Utilities->Global Options->Plugin Options->PMD. Also, you can change the plugin to prompt you for a directory to check by going to that same menu and selecting the "Ask for Directory" checkbox.

### Maven

Please see the Maven plugins web site for more information.

### Maven 2

Please see the Maven 2 plugin web site for more information. Also, the bug tracker for this plugin is here.

### NetBeans

Here's how to set up the Netbeans plugin:

- Download the latest binary release from here
- Unzip the zip file to any directory. You'll see that it contained a .nbm file that encapsulates the plugin.
- Run NetBeans and click Tools->Update Center
- Click the 'Install Manually Downloaded Modules' radiobutton and click Next
- Click the Add buttom and select the .nbm file, then click Next.
- Click thru the rest of the dialog to install the plugin

### TextPad

##### Assumptions

- The Java Development Kit, version 1.3.1 (versions 1.3 and higher are acceptable) is properly installed into your machine, and exists in `D:\java\jdk_131\`. This means that `D:\java\jdk_131\bin\java.exe` exists.
- PMD version 3.7 exists in `D:\java\pmd-3.7\`. This means that `D:\java\pmd-3.7\lib\pmd-3.7.jar` (among other jar files in the same directory) exist.

##### To integrate into TextPad

1. In the Configure menu, choose Preferences.... This opens the Preferences dialog
2. In the left pane of the Preferences dialog, choose the Tools branch by clicking on the word "Tools".
3. On the far right of the dialog, click on the Add button, and then select Program... from the drop-down. This opens the standard Windows Open File dialog.
4. Type `D:\java\jdk_131\bin\java.exe` and click the Open button. In the center pane of the Preferences dialog, an item "Java" has now been added, and is currently selected.
5. Click the word Java, which makes the word editable. Select the entire word, and type "PMD directory". Press Return
6. Repeat steps three through five, but type "PMD file", instead of "PMD directory".
7. Click Apply.
8. Expand the Tools branch (if not already) by clicking on the '`+`' directly to its left.
9. In the expanded list, select PMD directory. This changes the right side of this dialog to the "tool" form.
10. In the "tool" form, enter these parameters:
    - Parameters: `-classpath D:\java\pmd-3.7\lib\pmd-3.7.jar;D:\java\pmd-3.7\lib\jakarta-oro-2.0.8.kar;D:\java\pmd-3.7\lib\jaxen-1.1-beta-7.jar;D:\java\pmd-3.7\lib\xmlParserAPIs-2.6.2.jar; D:\java\pmd-3.7\lib\xercesImpl-2.6.2.jar net.sourceforge.pmd.PMD $FileDir net.sourceforge.pmd.renderers.TextPadRenderer E:\directory\my_pmd_ruleset.xml -debug`
      (Eliminate the spaces following both '`jaxen-1.1-beta-7.jar;`' and '`xmlParserAPIs-2.6.2.jar;`' and '`jakarta-oro-2.0.8.jar;`'. They are there to prevent the text from exceeding the right side of your screen)
    - Initial Folder: `$FileDir`
    - Save all documents first: `Checked`
    - Capture output: `Checked`
    - All other checkboxes: Unchecked
    - Regular expression to match output: `^\([^(]+\)(\([0-9]+\),`
    - Registers/File: `1`
    - Registers/Line: `2`
11. In the expanded list, select PMD file.
12. In the "tool" form, enter the same parameters as above, except replace '`$FileDir`' with '`$File`', in the Parameters textbox.
13. To save your work (truly, given a quirk of TextPad), click on OK, which closes the Preferences dialog. Restart TextPad and re-open the Preferences dialog.
14. Go back to both the "PMD directory" and "PMD file" Tools branches, and replace '`E:\directory\my_pmd_ruleset.xml`' with the ruleset of your choice. For example, `basic`.
15. Go to the Keyboard branch in the left pane (above Tools), which changes the right side to the "keyboard configuration" form.
16. In the Categories list box, select Tools.
17. In the Command list box, select PMD directory.
18. Put your cursor into the Press new shortcut key, and type your desired key command. For example `Ctrl+Page Up`
19. Click Assign.
20. In the Command list box, select PMD file.
21. Put your cursor into the Press new shortcut key, and type your desired key command. For example `Ctrl+Page Down`
22. Click Assign.
23. Save your work again: Click on OK, which closes the Preferences dialog, and then restart TextPad.

##### To run PMD against a single Java file

1. In TextPad, open any Java file.
2. Click `Ctrl+Page Down`. This opens an empty, read-only text document (titled "Command Results"). When PMD completes its analysis, this document will be populated with a listing of violated rules (or "Command completed successfully" indicating no violations).
3. Double click any line to go to it.

##### To run PMD against a directory of Java files

1. In TextPad, open any file in the root directory you wish to analyze. Unfortunately, you'll need to create a dummy file, if no file exists there.
2. Click `Ctrl+Page Up`. This opens an empty, read-only text document (titled "Command Results"). When PMD completes its analysis, this document will be populated with a listing of violated rules (or "Command completed successfully" indicating no violations).
3. Double click any line to go to it.

Because directory analysis may take a while, you may choose to cancel this operation. Do so by closing the (blank Command Results) document, and then confirming that, "yes, I do really want to exit the tool".