PMD is a source code analyzer. It finds common programming flaws such as unused variables, empty catch blocks, and unnecessary object creation. It also includes the Copy/Paste Detector (CPD) that finds duplicated code.
Use the following steps to run PMD on a Maven project:
project-with-violations
).mvn pmd:pmd pmd:cpd
[INFO] --- maven-pmd-plugin:3.2:pmd (default-cli) @ project-with-violations --- [WARNING] Unable to locate Source XRef to link to – DISABLED [INFO] --- maven-pmd-plugin:3.2:cpd (default-cli) @ project-with-violations --- [WARNING] Unable to locate Source XRef to link to - DISABLED [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
target
folder:pmd.xml
report:cpd.html
file in the site
folder:The pmd
or cpd
goals of the Maven PMD plugin are not bound to any phase. Also, they analyze the Java source and thus, do not need any other Maven phase or goal to be run.
When the pmd
goal is run, it generates a PMD site report using default rulesets and the configuration set in the plugin. It also generates a pmd output file in the XML format.
You can also define your own customized ruleset. To do this, add the following code in the configuration
section of the pom file:
<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.4</version> <configuration> <rulesets> <!-- Two rule sets that come bundled with PMD --> <ruleset>/rulesets/java/braces.xml</ruleset> <ruleset>/rulesets/java/naming.xml</ruleset> <!-- Custom local file system rule set --> <ruleset>d: ulesetsstrings.xml</ruleset> <!-- Custom remote rule set accessed via a URL --> <ruleset>http://localhost/design.xml</ruleset> </rulesets> </configuration> </plugin> </plugins> </reporting>
Likewise, when the cpd
goal is run, it generates a similar report for duplicated code. By default, the minimum token count that it considers to report that code is duplicated is 100 tokens (which is typically 10 lines of code).
The plugin can be made to fail the build by using the check goal in the following way:
project-with-violations
:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.4</version> <executions> <execution> <goals> <goal>check</goal> <goal>cpd-check</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
mvn verify
3.137.183.210