Maven – PMDの例

この記事では、Maven PMD Pluginを使用してJavaコードを分析する方法を示します。
P.S PMD requires Java 1.7
1. Maven PMDプラグイン
mvn siteがPMDレポートを生成するように、reportingタグでmaven-pmd-pluginを定義します。
pom.xml
org.apache.maven.plugins maven-pmd-plugin 3.11.0
2. Javaコード
シンプルなJavaコード。 Maven PMDプラグインを使用してこのコードを分析し、問題をレポートに表示します。
package com.example.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サイト
mvn compile siteを使用してJavaプロジェクトのMavenサイトを生成すると、PMDレポートが生成され、Mavenサイトに自動的に統合されます。
$ 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] ------------------------------------------------------------------------
5. FAQs
5.1 Review all PMD built-in rules for Java here.
ソースコードをダウンロード
$ git clone https://github.com/example/maven-examples.git
$ cd maven-static-code-analysis
$ mvn compile site
