zoukankan      html  css  js  c++  java
  • PMD使用手册

    1. 下载地址:https://sourceforge.net/projects/pmd/files/pmd/5.1.0/
    2. 安装:解压到任意目录。

          3. 使用方法:         

    3.1          使用命令行:(Windows下)

    命令行进入PMD安装目录,bin文件夹,执行命令:

    基本命令:pmd -d C:UserslspDesktopsvnjhotdraw7src -f html -R rulesets/java/unusedcode.xml

       其中 C:UserslspDesktopsvnjhotdraw7src为所需要分析源码绝对路径,可以是文件夹也可以是单个文件。

       -R 表示的采用的检测规则。 此规则都存在于

     

    Rulesets下有多种语言的规则,Java下又包含多种规则,在命令行每次只能检测单个规则(暂时未发现多条规则应用于一次检测),也可以自定义规则,具体怎么定义参见官网。

    -f 设置检测结果输出格式,html, xml,text, csv,

    该工具默认命令行检测结果输出到控制台,可以通过-r参数导出检测结果:如D:Program Filespmd-bin-5.1.0in>pmd -d C:UserslspDesktopsvnjhotdraw7src -f html -r D:pmdreportPMDoutput.html -R rulesets/java/unusedcode.xml

    其他参数:

    参数

    说明

    默认值

    -auxclasspath

    specifies the classpath for libraries used by the source code. This is used by the type resolution.Alternatively, a 'file://' URL to a text file con

    -benchmark, -b

    Benchmark mode - output a benchmark report upon completion; default to System.err

    False

    -dir, -d

    root directory for sources

    -encoding, -e

    specifies the character set encoding of the

    source code files PMD is reading (i.e., UTF-8)

    UTF-8

    -format, -f

    report format type

    text

    -language, -l

    specify a language PMD should use

    java

    -minimumpriority, -min

    rule priority threshold; rules with lower

    priority than they will not be used

    Low

    -property, -P

    {name}={value}: define a property for the report

    {}

    -reportfile, -r

    send report output to a file;

    System.out

    -rulesets, -R

    comma separated list of rulesets name to use

    -shortnames

    prints shortened filenames in the report

    false

    -showsuppressed

    report should show suppressed rule violations

    false

    -stress, -S

    performs a stress test

    -suppressmarker

    specifies the String that marks the a line which PMD should ignore; default is NOPMD

    NOPMD

    -threads, -t

    set the number of threads used by PMD

    1

    -uri, -u

    Database URI for sources

    -debug, -verbose, -D, -V

    Debug mode

    false

    -version, -v

    specify version of a language PMD should use

    1.8

     

    3.2          Ant

    在项目路径下build.xml文件中加入:

    D:Program Filespmd-bin-5.1.0lib为pmd安装路径下的lib

    C:UserslspDesktopsvnjhotdraw7src源码路径

    D:pmdreportpmd_report.xml分析结果文件

    <formatter type="xml" toFile="D:pmdreportpmd_report.xml"/> 格式化输出xml

    <formatter type="net.sourceforge.pmd.renderers.HTMLRenderer" toFile="D:pmdreportfoo.html"/> 格式化输出HTML

    <path id="pmd.classpath">
        <pathelement location="${build}"/>
        <fileset dir="D:Program Filespmd-bin-5.1.0lib">
            <include name="*.jar"/>
        </fileset>
    </path>
    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>
     
    <target name="pmd">
        <pmd rulesetfiles="rulesets/java/imports.xml,java-unusedcode">
            <formatter type="xml" toFile="D:pmdreportpmd_report.xml"/>
            <fileset dir="C:UserslspDesktopsvnjhotdraw7src">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

    3.3          Maven

    使用maven插件检测较为简单,只需在pom.xml文件中加入:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
    </plugin>
    执行mvn pmd:pmd

    即可在target文件夹下看到以下文件:

         

    即违反规则的报告文件,每个规则对应一个xml文件。也可以配置规则。

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
    <configuration>
               <rulesets>
                <ruleset>/rulesets/braces.xml</ruleset>
                <ruleset>/rulesets/naming.xml</ruleset>
               </rulesets>
    </configuration>
    </plugin>

     

    3.4          Eclipse Ide

    安装网址: http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/

    安装完右键项目或文件:选择PMD->check code即可使用PMD工具检查代码。

    选择PMD-->Find suspect cut and paste。检查结果会放在reports目录下

  • 相关阅读:
    如何选择一款程序员理想中的显示器
    群英论道聚北京,共话PostgreSQL
    中国人民大学教授杜小勇:One Size Does not Fit All?
    4个技巧,教你如何用excel绘制出高大上的图表
    容易被误读的IOSTAT
    【Android Studio安装部署系列】四、Android SDK目录和作用分析
    【Android Studio安装部署系列】四、Android SDK目录和作用分析
    【Android Studio安装部署系列】四、Android SDK目录和作用分析
    【Android Studio安装部署系列】四、Android SDK目录和作用分析
    Java框架 面试题总结
  • 原文地址:https://www.cnblogs.com/liveandevil/p/3616949.html
Copyright © 2011-2022 走看看