PMD是源代码分析器。它能找到常见的编程缺陷,如未使用的变量,空的catch块,不必要的对象创建等等。它支持Java,JavaScript,Salesforce.com Apex和Visualforce,PLSQL,Apache Velocity,XML,XSL。
此外,它还可以检查重复和冲突的代码。支持多种语言,可以在Java,C,C ++,C#,Groovy,PHP,Ruby,Fortran,JavaScript,PLSQL,Apache Velocity,Scala,Objective C,Matlab,Python,Go,Swift中找到重复的代码。
在这里是使用maven来集成PMD,在打包或者编译之前通过PMD可以很好地检查代码,最好还搭配surefire来检测覆盖率,chekstyle和findbug同时使用
在POM.XML文件里面添加如下
<build>
<plugins>
<!-- Plugin to create executable jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<targetJdk>1.8</targetJdk>
<rulesets>
<ruleset>category/java/bestpractices.xml</ruleset>
<ruleset>category/java/codestyle.xml</ruleset>
<ruleset>category/java/design.xml</ruleset>
<ruleset>category/java/errorprone.xml</ruleset>
<ruleset>category/java/multithreading.xml</ruleset>
<ruleset>category/java/performance.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>