您的位置:首页 > 产品设计 > 产品经理

PMD使用手册

2014-03-21 23:32 134 查看
文中测试例子均采用JHotDraw7源码,官网关于JHotDraw的检测结果:http://pmd.sourceforge.net/reports/jhotdraw_JHotDraw.html

下载地址:https://sourceforge.net/projects/pmd/files/pmd/5.1.0/

安装:解压到任意目录。

3.使用方法:

3.1使用命令行:(Windows下)

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

基本命令:pmd-dC:\Users\lsp\Desktop\svn\jhotdraw7\src-fhtml-Rrulesets/java/unusedcode.xml


其中C:\Users\lsp\Desktop\svn\jhotdraw7\src为所需要分析源码绝对路径,可以是文件夹也可以是单个文件。

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



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

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

该工具默认命令行检测结果输出到控制台,可以通过-r参数导出检测结果:如D:\ProgramFiles\pmd-bin-5.1.0\bin>pmd-dC:\Users\lsp\Desktop\svn\jhotdraw7\src-fhtml-rD:\pmdreport\PMDoutput.html-Rrulesets/java/unusedcode.xml

其他参数:

参数

说明

默认值

-auxclasspath

specifiestheclasspathforlibrariesusedbythesourcecode.Thisisusedbythetyperesolution.Alternatively,a'file://'URLtoatextfilecon

-benchmark,-b

Benchmarkmode-outputabenchmarkreportuponcompletion;defaulttoSystem.err

False

-dir,-d

rootdirectoryforsources

-encoding,-e

specifiesthecharactersetencodingofthe

sourcecodefilesPMDisreading(i.e.,UTF-8)

UTF-8

-format,-f

reportformattype

text

-language,-l

specifyalanguagePMDshoulduse

java

-minimumpriority,-min

ruleprioritythreshold;ruleswithlower

prioritythantheywillnotbeused

Low

-property,-P

{name}={value}:defineapropertyforthereport

{}

-reportfile,-r

sendreportoutputtoafile;

System.out

-rulesets,-R

commaseparatedlistofrulesetsnametouse

-shortnames

printsshortenedfilenamesinthereport

false

-showsuppressed

reportshouldshowsuppressedruleviolations

false

-stress,-S

performsastresstest

-suppressmarker

specifiestheStringthatmarksthealinewhichPMDshouldignore;defaultisNOPMD

NOPMD

-threads,-t

setthenumberofthreadsusedbyPMD

1

-uri,-u

DatabaseURIforsources

-debug,-verbose,-D,-V

Debugmode

false

-version,-v

specifyversionofalanguagePMDshoulduse

1.8

3.2Ant

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

D:\ProgramFiles\pmd-bin-5.1.0\lib为pmd安装路径下的lib

C:\Users\lsp\Desktop\svn\jhotdraw7\src\源码路径

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

<formattertype="xml"toFile="D:\pmdreport\pmd_report.xml"/>格式化输出xml

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

<pathid="pmd.classpath">

<pathelementlocation="${build}"/>

<filesetdir="D:\ProgramFiles\pmd-bin-5.1.0\lib">

<includename="*.jar"/>

</fileset>

</path>

<taskdefname="pmd"classname="net.sourceforge.pmd.ant.PMDTask"classpathref="pmd.classpath"/>


<targetname="pmd">

<pmdrulesetfiles="rulesets/java/imports.xml,java-unusedcode">

<formattertype="xml"toFile="D:\pmdreport\pmd_report.xml"/>

<filesetdir="C:\Users\lsp\Desktop\svn\jhotdraw7\src\">

<includename="**/*.java"/>

</fileset>

</pmd>

</target>


3.3Maven

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

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-pmd-plugin</artifactId>

</plugin>

执行mvnpmd: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.4EclipseIde

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

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

选择PMD-->Findsuspectcutandpaste。检查结果会放在reports目录下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: