You are currently viewing How To Use PMD Code Analyzer With IntelliJ

How To Use PMD Code Analyzer With IntelliJ

Creating new software is always a productive task for developers. However, building software that is maintainable and long-lasting is challenging. Fortunately, some tools can assist you in improving the quality of your code. And PMD is one of these tools.

The most widely used IDE for developing Salesforce applications and tools and code scanning tools, IntelliJ, is used to avoid flaws in a project. However, before deploying them into the production environment or making them live, it is essential to know the process of PMD with IntelliJ. So, in this guide, we have provided all the relevant information that you, as a developer, would require for your project when using PMD with IntelliJ. 

What Is PMD?

PMD is a program that analyses static source code. It detects programming faults such as unneeded variables, empty catch blocks, and the creation of unnecessary objects, among other things. It focuses on Java and Apex, but it also supports six more languages.

PMD has several features, including built-in checks (or rules in PMD terminology) documented in our Rule References for each language. You can construct your own rules using PMD’s rich API, which you may accomplish in Java or as a self-contained XPath query. When PMD is used as part of the construction process, it is most effective. It can then be used as a quality gate to ensure that your codebase adheres to a coding standard. PMD can be used for a variety of purposes, including:

  • As a Gradle task
  • As a Maven goal
  • As an Ant task
  • From command-line

PMD is a comprehensive tool that offers excellent scanning methods and is far faster than other programs in the market. It can be an excellent alternative for APEX code scanning because of its large number of predefined rules. Further, PMD allows the developer to construct their own rules while working on a project or software.

Installation And Overview

Before we start installing and configuring PMD, we need to know how it works and how we can link it to our IntelliJ IDE. The methods are pretty simple to follow, and the installation takes very little time.

References for downloading PMD and Apex rules:

Note – You must download the two files mentioned above in your system and store them at a secure location on your computer. You must have IntelliJ installed in your system, and You must also download the plugin PMD by Amit dev in IntelliJ.

Installation and Configuration

Let’s dive into the installation and configuration steps for PMD installation in IntelliJ.

Step 1: Download PMD bin.zip file

Go to this URL Releases · pmd/pmd · GitHub and search for the latest release of the PMD bin file and download the zip file in your system. Extract the file and save it to the C: drive directory.

Step 2: Download the Rule.xml file from the google drive link

 You can download the file from Download Apex rule file from here and save it to the document folder. You can open this file with any text editor and add or remove the desired rules you want to check.

You can check the latest Apex rules from here https://pmd.github.io/pmd-6.34.0/pmd_rules_apex.html

Step 3: Download PMD plugin in IntelliJ

Now Open IntelliJ and Install the PMDPlugin by Amit Dev in IntelliJ. This plugin will help you to run the rules and PMD analyzer in IntelliJ.

Step 4: Configure your PMD plugin in IntelliJ

Now you need to create an external tool profile in IntelliJ. Simply just follow the steps, and it will be done in a few minutes.
(i)  In IntelliJ, Open Settings → Tools → External Tools. 

In the External Tools window, click on Create new.

(ii) Create New will open a pop-up window, which you need to configure. 

In Name Type PMD.

In Description Type any description that you like.

In Program,  Enter the path of the PMD.bat file, which you will find in the folder where you have extracted the PMD.Bin zip file in the bin directory of that folder. 

In Arguments,  copy & paste this macro there:

-d “$FilePath$” -f ideaj -R rulesets/java/quickstart.xml -P sourcePath=”$Sourcepath$” -P classAndMethodName=$FileClass$.method -P fileName=$FileName$

In the Working Directory,  copy & paste this macro there:

$ProjectFileDir$

After filling above details, they should look like this: 

Click Ok and Apply and save the profile that you have just created.

(iii) Now, the PMD will be loaded with standard java rules. We have to import Apex rules. Make sure you have downloaded the Rules.xml file from the above drive link. 

If not, then Download from here- Download Apex rule file from here.

Now follow the steps as follows:

Go to Settings and find PMD (Not in the external tools), and open it.

It will open a PMD rule window. Click on the new Rule, and browse the file location where you have stored the Rules.xml file. 

Click Ok and ok and save it.

Done, you have set up all the files and configuration, and you will be ready to run the scanner.

Running Scanner

Scanning your code is very easy once you have configured all the settings and plugins.

  1. Go to the Project and right-click on the parent directory of your project, which you want to scan.
  1. Search Run PMD, choose Custom rule and the Rule file name bin folder path and start scanning your code.

It will soon show you all the reports in the PMD window. From there, you can choose the file, error lines and can fix it.

If you want to export an HTML file report, click on the export button and choose the file location where you want to save the file.

Choose a file location for the report file and save it.

If you want to Print a CSV version of your report file, you have to run a command in the Terminal of IntelliJ.

First, go to the path of the bin folder where you saved the extracted PMD bin file.

Type 

CD <PMD bin file bin folder path> 

Then Type 

PMD  -d  <Project file path in double quotes>  -f csv   -R  <Rule.xml file path in double quotes> -reportfile  <Output file location with filename.csv in double quotes>

Run the command and wait for till the execution is complete. 

My sample for the above commands are-

cd C:\pmd-bin-6.34.0\pmd-bin-6.34.0\bin 

      2)         

pmd -d “C:\Users\GET IT RENT\IdeaProjects\Saurabh-Personal-org” -R “C:\Users\GET IT   RENT\Documents\rules.xml” -f csv -reportfile “C:\Users\GET IT RE

NT\Documents\output.csv”

References

Documentation and Scanner: 

Videos:

Conclusion

Whether working on a small project or developing an extensive system or software, PMD has proved to be a beneficial tool for developers. It is an open-source code analyzer that finds and reports on flaws in your code. It searches your source code for errors such as dead code, empty statements, open curly braces, declared and unused variables, duplicated code, and name difficulties, among others.  

Want some best Salesforce consulting services for your business? Contact Cloud Analogy and get in touch with a team of experts who will transform CRM challenges into opportunities. 

Leave a Reply