PMD使用手冊


 

  1. 下載地址:https://sourceforge.net/projects/pmd/files/pmd/5.1.0/
  2. 安裝:解壓到任意目錄。

      3. 使用方法:         

3.1          使用命令行:(Windows下)

命令行進入PMD安裝目錄,bin文件夾,執行命令:

基本命令:pmd -d C:\Users\lsp\Desktop\svn\jhotdraw7\src -f html -R rulesets/java/unusedcode.xml

   其中 C:\Users\lsp\Desktop\svn\jhotdraw7\src為所需要分析源碼絕對路徑,可以是文件夾也可以是單個文件。

   -R 表示的采用的檢測規則。 此規則都存在於

 

Rulesets下有多種語言的規則,Java下又包含多種規則,在命令行每次只能檢測單個規則(暫時未發現多條規則應用於一次檢測),也可以自定義規則,具體怎么定義參見官網。

-f 設置檢測結果輸出格式,html, xml,text, csv,

該工具默認命令行檢測結果輸出到控制台,可以通過-r參數導出檢測結果:如D:\Program Files\pmd-bin-5.1.0\bin>pmd -d C:\Users\lsp\Desktop\svn\jhotdraw7\src -f html -r D:\pmdreport\PMDoutput.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 Files\pmd-bin-5.1.0\lib為pmd安裝路徑下的lib

C:\Users\lsp\Desktop\svn\jhotdraw7\src\源碼路徑

D:\pmdreport\pmd_report.xml分析結果文件

<formatter type="xml" toFile="D:\pmdreport\pmd_report.xml"/> 格式化輸出xml

<formatter type="net.sourceforge.pmd.renderers.HTMLRenderer" toFile="D:\pmdreport\foo.html"/> 格式化輸出HTML

<path id="pmd.classpath">
    <pathelement location="${build}"/>
    <fileset dir="D:\Program Files\pmd-bin-5.1.0\lib">
        <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:\pmdreport\pmd_report.xml"/>
        <fileset dir="C:\Users\lsp\Desktop\svn\jhotdraw7\src\">
            <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目錄下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM