PMD – Download Apex Code analysis as CSV, JSON, XML
Introduction
As important it is to write quality code, it is best practice to monitor Apex classes in your Salesforce org and create a workable list of classes with issues. In this article, you will learn how to download Apex code analysis as CSV, JSON, XML or HTML. Senior Developers or Team Leads should maintain a workable list of Apex classes with identified PMD issues and then create a plan to fix them.
You can run PMD scan and download a code analysis report for all the Apex classes in your org with a simple setup, this boosts your productivity by reducing the effort required to manually scan through all the classes.
This article covers:
- Why extract PMD report ?
- Steps to Install, Setup & Verify PMD
- Create sample rules to review Apex classes
- CLI Command to extract PMD as CSV
- CLI Command to extract PMD as JSON
- CLI Command to extract PMD as XML
- CLI Command to extract PMD as HTML
- Conclusion
Why Download Apex Code analysis as CSV, JSON, XML ?
A PMD report highlights list of all the Apex Classes with issues defined as part of the ruleset. You can even download the report in your local (CSV, JSON, HTML etc) and do further analysis to take actions.
The PMD report has 3 important columns:
- Name of the class.
- Line in the class where issue is identified.
- The actual issue identified on that line number.
It is important to periodically run and extract the PMD report as:
- It saves time by automatically scanning through all the Apex classes in your org and highlighting issues identified.
- It gives a high level view of your org’s Apex health.
- It helps to gain trust and confidence of the customer.
- It can also be included as step in your CI/CD pipeline. This way each time an Apex class is committed into the branch, PMD kicks in to scan the code and stop or proceed with the deployment based on your configuration. This makes sure your repository is clean without any major defects.
Steps to Install, Setup & Verify PMD
- Visit the PMD website here and download the latest package.
- Extract the zip installed in your system. Example: pmd-dist-7.0.0-rc4-bin
- After extracting the zip, copy the bin folder path from the extracted folder. Example: C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\bin
- Setup the Environment Variable by:
- Open Environment Variable from control panel, edit PATH and add the complete location of bin folder.
- Optionally, you can setup the path from command prompt by entering SET PATH=C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\bin;%PATH%.
Create sample rules to review Apex classes
Open your notepad and copy paste the below xml code to get started, save the file as ruleset.xml. (You can name it anything you like)
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default ruleset used by the CodeClimate Engine for Salesforce.com Apex" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description> Custom Rule Set </description>
<rule ref="category/apex/bestpractices.xml" />
<rule ref="category/apex/documentation.xml" />
<rule ref="category/java/errorprone.xml/EmptyCatchBlock" />
<!--excluding some rules -->
<rule ref="category/apex/codestyle.xml">
<exclude name="WhileLoopsMustUseBraces"/>
<exclude name="IfElseStmtsMustUseBraces"/>
</rule>
</ruleset>
Explanation
This is a sample ruleset that we have created for this article. If you build more complex and customized ruleset by reading the PMD article here.
In the rule set sample above, we mention the rules that we want our code to check for using <rule ref=”” /> tag. We can also mentioned rules that we want to ignore while running the static code analysis in pmd using <exclude name=”” /> tag.
CLI Command to Download Apex Code analysis as CSV
pmd.bat 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 -f csv -r C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\output.csv

CLI Command to extract PMD as JSON
pmd.bat 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 -f json -r C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\output.json

Command to extract PMD as XML
pmd.bat 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 -f xml -r C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\output.xml

Command to extract PMD report as HTML
pmd.bat 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 -f html -r C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\output.html

Explanation
In the above examples, we highlighted some of the PMD commands which you can use to download Apex code analysis in CSV, JSON, XML or HTML format.
The command start by specifying the directory which holds all the Apex classes, here we have given the complete path of the folder with –dir option.
pmd.bat check --dir C:\Development\MyDevOrg\mydevorg\force-app\main\default\classes
Next, we define the ruleset that we want to use to analyze the code, we specify the complete path of the ruleset.xml file that was created earlier with -R option.
-R C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\ruleset.xml
After this, we mention the format of the file we want to download the code analysis report as. In our example we have mentioned this with -f option and values as csv, json, html, xml.
-f html
At the end, we mention the complete file path with name and the extension with option -r. If the file is not already present, a new file is created in the location provided with the name given and all the details of apex analysis are captured.
-r C:\pmd-dist-7.0.0-rc4-bin\pmd-bin-7.0.0-rc4\output.html
Conclusion
In the world of Salesforce development, maintaining code quality is paramount. Being a Salesforce Developer monitoring Apex classes and identifying issues becomes essential. In this article, we explore how to extract download Apex Code analysis as CSV, JSON, XML, and HTML.
We discussed about why its important to run a PMD analysis on Apex classes and download the report. We also discussed on the steps to install & Setup PMD, sample commands to extract PMD reports in different formats.
You can read the below articles around PMD:
Apex code analysis with PMD in VSCode
Apex static code analysis with PMD from CLI
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.