上週客戶要求對OA系統的代碼質量進行了一個總體審查,而且要出一份報告給領導。html
爲此花了半天時間把代碼審查工具PMD琢磨了下,現將具體操做步驟描述以下,以供你們參考!ide
質量是衡量一個軟件是否成功的關鍵要素。而對於商業軟件系統,尤爲是企業應用軟件系統來講,除了軟件運行質量、文檔質量之外,代碼的質量也是很是重要的。軟件開發進行到編碼階段的時候,最大的風險就在於如何保證代碼的易讀性和一致性,從而使得軟件的維護的代價不會很高。工具
PMD是一種分析Java代碼錯誤的開源工具。與其餘分析工具不一樣的是,PMD經過靜態分析獲知代碼錯誤。也就是說,在不運行Java程序的狀況下報告錯誤。PMD附帶了許多能夠直接使用的規則,利用這些規則能夠找出Java源程序的許多問題,例如:oop
此外,用戶還能夠本身定義規則,檢查Java代碼是否符合某些特定的編碼規範。例如,你能夠編寫一個規則,要求PMD找出全部建立Thread和Socket對象的操做。優化
一、首先須要下載ant-1.8.2及pmd-bin-4.2.1兩個資源;ui
二、ant環境變量配置:this
目錄:[../pmd-bin-4.2.1/pmd-4.2.1]編碼
其中運行完生成的(E:\Tool\PMD\imis_oa.html)文件即是審查結果報告!spa
目錄:[../pmd-bin-4.2.1/pmd-4.2.1/rulesets].net
1 <?xml version="1.0"?> 2 3 <ruleset name="Favorites" 4 xmlns="http://pmd.sf.net/ruleset/1.0.0" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" 7 xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> 8 9 <description> 10 The Favorites ruleset contains links to rules that I like to use. Usually I 11 combine this ruleset with the unusedcode.xml, basic.xml, and import.xml rulesets for my projects. 12 This ruleset also serves as an example of how to do a custom ruleset. 13 </description> 14 <rule ref="rulesets/basic.xml"/> 15 <rule ref="rulesets/unusedcode.xml"/> 16 <rule ref="rulesets/braces.xml/IfStmtsMustUseBraces"/> 17 <rule ref="rulesets/braces.xml/WhileLoopsMustUseBraces"/> 18 <rule ref="rulesets/braces.xml/IfElseStmtsMustUseBraces"/> 19 <rule ref="rulesets/braces.xml/ForLoopsMustUseBraces"/> 20 <rule ref="rulesets/design.xml/CloseResource"/> 21 <rule ref="rulesets/design.xml/NonStaticInitializer"/> 22 <rule ref="rulesets/design.xml/NonCaseLabelInSwitchStatement"/> 23 <rule ref="rulesets/design.xml/OptimizableToArrayCall"/> 24 <rule ref="rulesets/design.xml/EqualsNull"/> 25 <rule ref="rulesets/design.xml/AbstractClassWithoutAbstractMethod"/> 26 <rule ref="rulesets/design.xml/UnnecessaryLocalBeforeReturn"/> 27 <rule ref="rulesets/design.xml/UncommentedEmptyMethod"/> 28 <rule ref="rulesets/design.xml/UseCollectionIsEmpty"/> 29 <rule ref="rulesets/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal"/> 30 <rule ref="rulesets/design.xml/EmptyMethodInAbstractClassShouldBeAbstract"/> 31 <rule ref="rulesets/design.xml/ReturnEmptyArrayRatherThanNull"/> 32 <rule ref="rulesets/design.xml/AbstractClassWithoutAnyMethod"/> 33 <rule ref="rulesets/basic.xml/EmptyCatchBlock"/> 34 <rule ref="rulesets/basic.xml/EmptyIfStmt"/> 35 <rule ref="rulesets/basic.xml/EmptyWhileStmt"/> 36 <rule ref="rulesets/basic.xml/EmptyTryBlock"/> 37 <rule ref="rulesets/basic.xml/EmptyFinallyBlock"/> 38 <rule ref="rulesets/basic.xml/EmptySwitchStatements"/> 39 <rule ref="rulesets/basic.xml/EmptyStatementNotInLoop"/> 40 <rule ref="rulesets/basic.xml/BooleanInstantiation"/> 41 <rule ref="rulesets/basic.xml/UnnecessaryFinalModifier"/> 42 <rule ref="rulesets/strings.xml/StringToString"/> 43 <rule ref="rulesets/strings.xml/StringInstantiation"/> 44 <rule ref="rulesets/controversial.xml/UnnecessaryConstructor"/> 45 <rule ref="rulesets/controversial.xml/NullAssignment"/> 46 <rule ref="rulesets/controversial.xml/UnusedModifier"/> 47 48 49 </ruleset>
經本人屢次嘗試不替換會報如下錯誤:
若以上步驟都成功完成,你想要的代碼審查報告也已生成!呈現形式以下: