Skip to content

Add or improve existing PMD rules

David Renz edited this page May 30, 2016 · 34 revisions

If you want to add rules that work similarly to the ones we already have you will have to extend PMD itself. Those are the steps you need to do:

Setup development environment

  • Install latest Eclipse IDE for Java Developers
  • Fork PMD (https://github.com/pmd/pmd)
  • Connect your eclipse with master branch of your PMD repo
  • Import the project as general project
  • Delete the project, uncheck the Delete project contents on disk and copy the project location
  • Import them as existing Maven Projects and auto-resolve problems

Add a new rule for Apex PMD

  • Create a new rule (example AvoidDmlInLoops)
  • Use a similar rule as starting point and adapt it
  • Add rule definition to the ruleset of its category and the complete ruleset
  • Add the rule to the according Test class
  • Create a new xml file with test cases
  • Run your unit tests

Build PMD and test your new rule

  • Install Maven (brew install maven if you are using Homebrew)
  • To build PMD navigate to the root directory of PMD and execute mvn clean package
  • After building PMD navigate to /pmd/pmd-dist/target and unzip pmd-bin-5.5.0-SNAPSHOT.zip
  • Run PMD pmd-bin-5.5.0-SNAPSHOT/bin/run.sh pmd -d /files/to/analyse -f text -R apex-ruleset

Commit and Pull Request the new rule

  • Synchronize your workspace with your repository
  • Compare and check your changes and commit them
  • Create a pull request
Clone this wiki locally