Maven PMD Reporting Plug-in

Maven을 통한 빌드시 PMD Code Inspection의 결과물을 내보낼 수 있게 한다. ‘pmd:pmd’ Goal을 실행하면 정해진 경로로 xml 결과물이 생성되며, Hudson(Jenkins)과 같은 CI의 plug-in과 조합하여 상당한 수준의 관리 통합도 가능하다.
또한 ‘pmd:cpd’ Goal을 통해 Copy & Paset Detector(CPD)도 이용할 수 있다.

1. Maven POM

    <!-- Reporting -->
    <reporting>
        <plugins>
        ...
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-pmd-plugin</artifactid>
                <version>2.4</version>
                <configuration>
                    <rulesets>
                        <ruleset>rulesets/unusedcode.xml</ruleset>
                        <ruleset>rulesets/coupling.xml</ruleset>
                        <ruleset>rulesets/clone.xml</ruleset>
                        <ruleset>rulesets/codesize.xml</ruleset>
                        <ruleset>rulesets/braces.xml</ruleset>
                        <ruleset>rulesets/naming.xml</ruleset>
                        <ruleset>rulesets/optimizations.xml</ruleset>
                        <ruleset>rulesets/imports.xml</ruleset>
                        <ruleset>rulesets/logging-java.xml</ruleset>
                        <ruleset>rulesets/strings.xml</ruleset>
                    </rulesets>
                    <sourceencoding>utf-8</sourceencoding>
                    <targetjdk>1.6</targetjdk>
                    <minimumtokens>10</minimumtokens>
                    <excludes>
                        <exclude> **/com/xenomity/common/** /*.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            ...
        </plugins>
    </reporting>

2. Rulesets
maven-pmd-plugin.jar 내부에는 /rulesets 경로에 각 카테고리에 해당하는 ruleset XML 파일들이 정의되어 있다. 제공 룰셋들이 너무 엄격하거나 loose하다면, 커스터마이징도 물론 가능하다. 자세한 내용은 http://pmd.sourceforge.net/”을 참고한다.

  • Android Rules: These rules deal with the Android SDK, mostly related to best practices. To get better results, make sure that the auxclasspath is defined for type resolution to work.
  • Basic JSF rules: Rules concerning basic JSF guidelines.
  • Basic JSP rules: Rules concerning basic JSP guidelines.
  • Basic Rules: The Basic Ruleset contains a collection of good practices which everyone should follow.
  • Braces Rules: The Braces Ruleset contains a collection of braces rules.
  • Clone Implementation Rules: The Clone Implementation ruleset contains a collection of rules that find questionable usages of the clone() method.
  • Code Size Rules: The Code Size Ruleset contains a collection of rules that find code size related problems.
  • Controversial Rules: The Controversial Ruleset contains rules that, for whatever reason, are considered controversial. They are separated out here to allow people to include as they see fit via custom rulesets. This ruleset was initially created in response to discussions over UnnecessaryConstructorRule which Tom likes but most people really dislike :-)
  • Coupling Rules: These are rules which find instances of high or inappropriate coupling between objects and packages.
  • Design Rules: The Design Ruleset contains a collection of rules that find questionable designs.
  • Finalizer Rules: These rules deal with different problems that can occur with finalizers.
  • Import Statement Rules: These rules deal with different problems that can occur with a class' import statements.
  • J2EE Rules: These are rules for J2EE
  • JavaBean Rules: The JavaBeans Ruleset catches instances of bean rules not being followed.
  • JUnit Rules: These rules deal with different problems that can occur with JUnit tests.
  • Jakarta Commons Logging Rules: The Jakarta Commons Logging ruleset contains a collection of rules that find questionable usages of that framework.
  • Java Logging Rules: The Java Logging ruleset contains a collection of rules that find questionable usages of the logger.
  • Migration Rules: Contains rules about migrating from one JDK version to another. Don't use these rules directly, rather, use a wrapper ruleset such as migrating_to_13.xml.
  • Migration13: Contains rules for migrating to JDK 1.3
  • Migration14: Contains rules for migrating to JDK 1.4
  • Migration15: Contains rules for migrating to JDK 1.5
  • MigratingToJava4: Contains rules for migrating to JDK 1.5
  • Naming Rules: The Naming Ruleset contains a collection of rules about names - too long, too short, and so forth.
  • Optimization Rules: These rules deal with different optimizations that generally apply to performance best practices.
  • Strict Exception Rules: These rules provide some strict guidelines about throwing and catching exceptions.
  • String and StringBuffer Rules: These rules deal with different problems that can occur with manipulation of the class String or StringBuffer.
  • Security Code Guidelines: These rules check the security guidelines from Sun, published at http://java.sun.com/security/seccodeguide.html#gcg
  • Type Resolution Rules: These are rules which resolve java Class files for comparisson, as opposed to a String
  • Unused Code Rules: The Unused Code Ruleset contains a collection of rules that find unused code.
  • 3. Maven Goal List

    pmd:pmd PMD Report XML, CSV or TXT 생성
    pmd:cpd CPD Report XML, CSV or TXT 생성

    4. Hudson CI Hudson 2.1.0 기준으로 PMD/CPD의 결과를 보려면 우선 다음과 같은 plug-in들이 설치되어 있어야 한다.

    hudson plugins

    그리고 원하는 Job의 Configure -> Post-Build Action에 다음 사항을을 확인한다.

    post build

    Hudson의 해당 Job workspace가 PMD Reporting의 디폴트 경로가 되며, 별다른 설정을 하지 않으면 plug-in에서는 workspace/reports 경로에 결과물을 생성시킨다. 디폴트 경로르 사용하는 경우, 위 설정에서 별다른 result XML을 설정해줄 필요는 없다.

    성공적으로 빌드가 끝나면 hudson의 좌측 메뉴에 ‘PMD Warnings’과 ‘Duplicate Code’라는 메뉴가 추가된 것을 확인할 수 있다.

    PMD Result 1

    Comments