PMD使用手册
文中測(cè)試?yán)泳捎肑HotDraw7源碼,官網(wǎng)關(guān)于 JHotDraw的檢測(cè)結(jié)果:http://pmd.sourceforge.net/reports/jhotdraw_JHotDraw.html
下載地址:https://sourceforge.net/projects/pmd/files/pmd/5.1.0/
安裝:解壓到任意目錄。
3. 使用方法:
3.1 使用命令行:(Windows下)
命令行進(jìn)入PMD安裝目錄,bin文件夾,執(zhí)行命令:
基本命令:pmd -d C:UserslspDesktopsvnjhotdraw7src -f html -R rulesets/java/unusedcode.xml
其中 C:UserslspDesktopsvnjhotdraw7src為所需要分析源碼絕對(duì)路徑,可以是文件夾也可以是單個(gè)文件。
-R 表示的采用的檢測(cè)規(guī)則。 此規(guī)則都存在于
Rulesets下有多種語(yǔ)言的規(guī)則,Java下又包含多種規(guī)則,在命令行每次只能檢測(cè)單個(gè)規(guī)則(暫時(shí)未發(fā)現(xiàn)多條規(guī)則應(yīng)用于一次檢測(cè)),也可以自定義規(guī)則,具體怎么定義參見(jiàn)官網(wǎng)。
-f 設(shè)置檢測(cè)結(jié)果輸出格式,html, xml,text, csv,
該工具默認(rèn)命令行檢測(cè)結(jié)果輸出到控制臺(tái),可以通過(guò)-r參數(shù)導(dǎo)出檢測(cè)結(jié)果:如D:Program Filespmd-bin-5.1.0in>pmd -d C:UserslspDesktopsvnjhotdraw7src -f html -r D:pmdreportPMDoutput.html -R rulesets/java/unusedcode.xml
其他參數(shù):
|
參數(shù) |
說(shuō)明 |
默認(rèn)值 |
|
-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
在項(xiàng)目路徑下build.xml文件中加入:
D:Program Filespmd-bin-5.1.0lib為pmd安裝路徑下的lib
C:UserslspDesktopsvnjhotdraw7src源碼路徑
D:pmdreportpmd_report.xml分析結(jié)果文件
<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插件檢測(cè)較為簡(jiǎn)單,只需在pom.xml文件中加入:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
執(zhí)行mvn pmd:pmd
即可在target文件夾下看到以下文件:
即違反規(guī)則的報(bào)告文件,每個(gè)規(guī)則對(duì)應(yīng)一個(gè)xml文件。也可以配置規(guī)則。
<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
安裝網(wǎng)址:http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/
安裝完右鍵項(xiàng)目或文件:選擇PMD->check code即可使用PMD工具檢查代碼。
選擇PMD-->Findsuspectcutandpaste。檢查結(jié)果會(huì)放在reports目錄下
總結(jié)
- 上一篇: 我看阿Q精神800字作文
- 下一篇: 祝宝宝健康成长的句子257个