PMD-java代码静态分析工具

来源:互联网 发布:黑马人工智能视频教程 编辑:程序博客网 时间:2024/04/29 21:57
一、PMD

1、下载pmd-bin-5.0.5.zip
<http://sourceforge.net/projects/pmd/>

2、使用
>pmd -d D:\myproject\src -f text -R rulesets/java/unusedcode.xml -r mybug.txt
常用参数说明:
  -d[dir]           源文件目录(检测目录)
  -f[format]     导出格式
  -R[rulesets]   指定规则集
  -h[help]        帮助
  -r[reportfile]    导出文件

3、自定义规则集
3.1)导入规则集、导入特定规则的规则集、排除指定规则的规则集
新建myruleset.xml
<?xml version="1.0"?>
<ruleset name="myruleset">
  <description>
  Sample ruleset for developerWorks article
  </description>
  <!--导入规则集-->
  <rule ref="rulesets/java/unusedcode.xml"/>
  <rule ref="rulesets/java/naming.xml"/>
  <!--导入特定规则-->
  <rule ref="rulesets/java/basic.xml/EmptyFinallyBlock"/>
  <!--排除特定规则-->
  <rule ref="rulesets/design.xml">
<exclude name="SwitchStmtsShouldHaveDefault"/>
  </rule>
  <!--传参-->
  <rule ref="rulesets/java/basic.xml/AvoidUsingHardCodedIP">
<properties>
            <property name="checkAddressTypes" value="IPv6"/>
        </properties>
  </rule>
</ruleset>

>pmd -d D:\myproject\src -f csv -R  myruleset.xml  -r mybug.csv


二、PMD for Eclipse Plugin

1、版本支持说明

Eclipse

PMD for Eclipse plugin

3.x

3.x(used PMD v4.2.5)

4.x

4.x(used PMD v5.0.5)


2、PMD插件安装
1、启动myEclipse
2、选择HelpSoftware UpdatesFind and Install…
3、选择“Search for new features to install”(如果是更新而不是新装,则选择 “Search for updates of the currently install features”),点击 Next
4、点击 “New Remote Site...”
5、输入Name和URL,点击OK按钮
Name:pmd
URL:http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/
6、"PMD"将出现在 Sites to include in search页面中,选中PMD,点击Finish
7、In the Search Results 页面,选中PMD,点击 Next
8、选中“I Accept the terms of the license agreements”, 点击 Next
9、确认安装路径正确,点击 Finish
10、等待Eclipse下载需要的jar文件包,点击 Install
11、根据提示,选择Yes重启Eclipse

3、导入自定义规则集
   略

4、CPD使用说明

1、 启动myEclipse,
2、 选择WindowPreferences…,进行配置。如图:
可以导入,编辑,删除规则集
 
3、选中项目/javaSource,右键PMDFind Suspect Cut And Paste…,如图:
 
4、显示PMD视图,如图:




规则集详细说明(中文)下载地址:
http://download.csdn.net/detail/lihaipeng0417/6198565


原创粉丝点击