The Apache Maven PMD Plugin allows you to integrate PMD (Project Mess Detector) into your Maven project. PMD is a static code analysis tool that finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and more. This guide will walk you through configuring and using the Maven PMD Plugin with a complete example.
The Maven PMD Plugin helps in detecting and reporting code problems in your source code. It can be configured to run during the build process and generate reports that highlight potential issues.
Latest Version
As of this writing, the latest version of the Maven PMD Plugin is 3.17.0. Using the latest version ensures access to the newest features and bug fixes.
To use the Maven PMD Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a complete Maven project with the PMD Plugin.
Step 1: Create a Maven Project
Run the following command to create a new Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=pmd-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will generate a simple Maven project with the following structure:
pmd-plugin-demo |-- src | |-- main | | |-- java | | | `-- com | | | `-- example | | | `-- App.java | `-- test | |-- java | | `-- com | | `-- example | | `-- AppTest.java |-- pom.xml `-- target
Step 2: Add Maven PMD Plugin Configuration
Navigate to the project directory and open the pom.xml file. Add the Maven PMD Plugin configuration inside the <build> section:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>pmd-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.17.0</version> <executions> <execution> <goals> <goal>check</goal> </goals> <configuration> <targetJdk>21</targetJdk> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Explanation
Step 3: Modify the App.java to Introduce an Issue
Open src/main/java/com/example/App.java and introduce a simple issue that PMD can detect. For example, an unused variable:
package com.example; public class App { public static void main(String[] args) { int unusedVariable = 0; // This is an unused variable System.out.println("Hello World!"); } }
Step 4: Run the PMD Check
Run the following command to execute the PMD check:
mvn pmd:check
Output
If there are issues in your code, PMD will report them. In this case, PMD should report the unused variable in App.java.
[INFO] --- maven-pmd-plugin:3.17.0:check (default-cli) @ pmd-plugin-demo --- [INFO] PMD Failure: com.example.App:7 Rule:UnusedLocalVariable Priority:3 Unused variable 'unusedVariable'.
Step 5: Generate the PMD Report
You can also generate a PMD report to view all the issues. Run the following command:
mvn pmd:pmd
The report will be generated in the target/site directory. Open target/site/pmd.html in your browser to view the report.
Here is the complete pom.xml file for the example project:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>pmd-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.17.0</version> <executions> <execution> <goals> <goal>check</goal> </goals> <configuration> <targetJdk>21</targetJdk> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
The Maven PMD Plugin is a powerful tool for improving the quality of your code by detecting potential issues early in the development process. This guide provided a complete example of setting up and using the Maven PMD Plugin, helping you integrate static code analysis into your Maven projects. By following these steps, you can easily enhance the quality and maintainability of your code.