Spring Boot integrated pmd plugin quick start Demo

HBLOG
3 min readJun 4, 2024

--

1. What is the PMD plugin?

The PMD plugin allows you to run automatically on your project’s source codePMDcode analysis tool and generate a site report with its results. It also supports a separate copy/paste detector tool (or CPD) that is distributed with the PMD. This version of the Maven PMD plugin uses PMD 6.42.0 and requires Java 8. For more information, seeUpgrade the PMD at runtime. The plugin accepts configuration parameters that can be used to customize the execution of the PMD tool.

Overview of the objectives

This plugin has the following goals:

  • pmd:pmdCreate PMD site reports based on the rule set and configuration set in the plug-in. In addition to site reports, it can generate PMD output files in any of the following formats: XML, CSV, or TXT.
  • pmd:aggregate-pmdDepending on the set of rules set in the plugin and configured inAggregatorsCreate a PMD site report in the project. In addition to site reports, it can generate PMD output files in any of the following formats: XML, CSV, or TXT.
  • pmd:aggregate-pmd-no-fork在AggregatorsCreate a PMD site report in your project without having to fork it againTest compilationStage.
  • pmd:cpdCreate a report for PMD’s Copy/Paste Detector (CPD) tool. It can also generate CPD result files in any of the following formats: XML, CSV, or TXT.
  • pmd: aggregate-cpdAggregatorsCreate a report for PMD’s Copy/Paste Detector (CPD) tool in the project. It can also generate CPD result files in any of the following formats: XML, CSV, or TXT.
  • If there are any PMD violations in the source code, thenpmd:check causes the build to fail.This target is automatically invoked before executing itselfpmd:pmd 。
  • If there are any PMD violations in the source code, thenpmd:aggregate-pmd-checkAggregatorsThe build in the project failed. This target is automatically invoked before executing itselfpmd:aggregate-pmd 。
  • If there are any CPD violations in the source code, thenpmd:cpd-check will make the build fail.This target is automatically invoked before executing itselfpmd:cpd 。
  • If there are any CPD violations in the source code, thenpmd:aggregate-cpd-checkAggregatorsThe build in the project failed. This target is automatically invoked before executing itselfpmd:aggregate-cpd 。

2. Code engineering

Purpose of the experiment

Use pmd to check if the code in your project is violated

The first method: Maven plugin integration

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pmd</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<pmd.version>3.15.0</pmd.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>${maven.compiler.target}</targetJdk>
<excludes>
<!--<exclude>**/*Bean.java</exclude>-->
<!--<exclude>**/generated/*.java</exclude>-->
</excludes>
<excludeRoots>
<!--<excludeRoot>target/generated-sources/stubs</excludeRoot>-->
</excludeRoots>
<rulesets>
<ruleset>rulesets/java/quickstart.xml</ruleset>
</rulesets>
<outputDirectory>target/pmd-reports</outputDirectory>
</configuration>
<!--在clean后自动执行check-->
<!--<executions>-->
<!--<execution>-->
<!--<id>pmd-check</id>-->
<!--<phase>clean</phase>-->
<!--<goals>-->
<!--<goal>check</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
</plugin>
</plugins>
</build>
</project>

The second method: the ideal plugin

Installation

use

The above are just some of the key codes, all of which can be found in the repositories below

Code repositories

3. Testing

Run mvn pmd:check, and a detection report will be generated under target/pmd-reports/

4. References

--

--