Maven PMD example

摘要: In this article, we will show you how to use Maven PMD Plugin to analyze the Java code.

In this article, we will show you how to use Maven PMD Plugin to analyze the Java code.

P.S PMD requires Java 1.7

1. Maven PMD Plugin

Define the maven-pmd-plugin in the reporting tag, so that mvn site will generate the PMD report.

pom.xml
	<reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>
        </plugins>
    </reporting>

2. Java Code

A simple Java code. We will use Maven PMD plugin to analyze this code and display the issues in a report.

package com.mkyong.examples;
public class StaticCodeExample {
    //Unused field
    private int abc;
    private String ip = "127.0.0.1";
    public void test() {
        String[] field = {"a", "b", "c", "s", "e"};
        String s = "";
        for (int i = 0; i < field.length; ++i) {
            s = s + field[i];
        System.out.println(ip);

3. Maven Site

mvn compile site to generate a Maven site for the Java project, the PMD report will be generated and integrated into the Maven site automatically.

$ mvn compile site
[INFO] Generating "PMD" report           --- maven-pmd-plugin:3.11.0:pmd
[INFO] Generating "Dependency Information" report --- maven-project-info-reports-plugin:3.0.0:dependency-info
[INFO] Generating "About" report         --- maven-project-info-reports-plugin:3.0.0:index
[INFO] Generating "Plugin Management" report --- maven-project-info-reports-plugin:3.0.0:plugin-management
[INFO] Generating "Plugins" report       --- maven-project-info-reports-plugin:3.0.0:plugins
[INFO] Generating "Summary" report       --- maven-project-info-reports-plugin:3.0.0:summary
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.732 s
[INFO] Finished at: 2018-11-19T15:38:56+08:00
[INFO] ------------------------------------------------------------------------

4. PMD Report

Review the report at target/site/pmd.html

5. FAQs

5.1 Review all PMD built-in rules for Java here.

References

  1. PMD official site
  2. Maven PMD Plugin
  3. List of tools for static code analysis

上一篇: Maven How to force re-download project dependencies?
下一篇: Java Global variable examples
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号