Step by Step Guide to install and setup PMD for Apex – Salesforce
Introduction
In the ever-evolving Salesforce development landscape, maintaining the quality of code is of utmost importance. In today’s article will talk about steps to install and setup PMD for Apex and run static code analysis on Apex classes.
Code analysis and review serve as vital practices in upholding high code standards throughout the Salesforce development lifecycle. These processes involve a systematic examination of source code to identify and rectify potential issues, ensuring optimal performance, maintainability, and reliability of the software. In today’s article we will cover
This article covers:
- Understanding PMD
- Key Benefits of PMD in the Development Process
- Download the PMD binary package
- Setting up Environment Variable for PMD
- Validate the installation and setup PMD
- Some examples of using the PMD command
- Conclusion
Understanding PMD:
PMD acts as an invaluable static code analysis tool tailored specifically for Salesforce development. It aids developers in identifying and addressing potential issues in their Apex code. PMD examines various code quality aspects, including code complexity, possible bugs, and adherence to best practices.
Key Benefits of PMD in the Development Process:
Enforcement of Code Consistency: PMD enforces coding standards, guaranteeing that your Apex code adheres to best practices, maintaining consistency throughout the entire development process.
Prevention of Bugs: By identifying potential issues early on, PMD helps prevent bugs from infiltrating production code, resulting in more robust and reliable software.
Optimized Performance: PMD’s meticulous analysis includes checks for code complexity and performance bottlenecks, leading to optimized and efficient code.
A Step-by-Step Guide to install and setup PMD for Apex in Salesforce:
Step 1: Download the PMD binary package

Visit the PMD website here and click on the Download button to install the latest binary package.
Step 2: Setting up Environment Variable for PMD

Once the package is installed, extract the zip file and navigate to the bin folder.
Copy the complete path from the address bar and keep it handy, this will be required to setup in the Environment variable path in the system.

Next, search for Environment variables in the Windows search bar, then click on Edit environment variables. (Your options may vary based on accounts setup in the Windows OS).

Select the Path variable from the list and click on Edit button.

In the next screen enter the PMD bin folder path that you copied earlier and click on OK button.
Step 3: Validate the installation and setup PMD
Open the command prompt in the Windows.
Tip: You can press Windows + R button to run a command, then enter CMD and press ok to open the command prompt.
In the command prompt, enter pmd and press the enter button on your keyboard. You should now see the below response if PMD is installed and setup successfully.

If you are using the latest version of PMD, then you might see additional commands under the PMD package. Use pmd <command> to execute that specific command followed by options and parameters.
Note: You might need to close and open the command prompt window if you don’t see any response or not recognized as an internal or external command error. Make sure that Environment Variable is setup for this to work properly.
Some examples of using the PMD command
Run PMD check to do a static code analysis on a specific Apex Class
pmd check --dir C:\Development\MyDevOrg\mydevorg\force-app\main\default\classes\AccountData.cls -R C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\ruleset.xml
Run PMD check to do a static code analysis on a folder containing Apex Classes
pmd check --dir C:\Development\MyDevOrg\mydevorg\force-app\main\default\classes -R C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\ruleset.xml
Conclusion
Incorporating PMD for Apex code analysis and review is indispensable for Salesforce developers. The following comprehensive guide, will help you to successfully install and setup PMD for Apex, empowering you to enhance code quality, enforce coding standards, prevent bugs, and optimize performance.
Embrace PMD as an essential tool in your Salesforce development journey, ensuring the delivery of reliable and high-quality software solutions.
Feel free to copy paste the sample commands above and play around. Don’t forget to subscribe to our newsletter for more Salesforce development tips.
If you have any issues or suggestions, please feel free to use the comment section below.
3 Comments