lint/pmd/p3c靜態代碼檢查工具的配置與使用

一、工具介紹

1、lint/pmd/p3c工具做檢查。

其中,pmd/p3c偏向於java語言本身,lint工具入偏向於android。
(後續會考慮加入C/C++的代碼檢查。)

工具、規則介紹:
pmd: https://pmd.github.io/ https://pmd.github.io/pmd/pmd_rules_java_design.html
p3c: https://github.com/alibaba/p3c https://github.com/alibaba/p3c/tree/master/p3c-gitbook
lint: 命令行運行:lint show

二、工具使用方法

1、IDE上對應插件的使用。

支持使用的IDE:android studio/Intellij IDEA。
兩個工具的使用方法、插件配置類似,全文以android studio爲例。

step1. 安裝pmdplugin、alibaba Java Coding Guidelines
方法一、IDE => Performances => Plugins => Marketplace
搜索並安裝如上pmd/ alibaba兩個插件。

方法二、手動下載插件,後安裝。
pmdplugin: https://plugins.jetbrains.com/plugin/1137-pmdplugin/versions (下載時特別注意選擇IDE平臺)
alibaba: https://github.com/alibaba/p3c/blob/master/idea-plugin/README_cn.md
推薦使用方法一,特別注意:如果是手動下載安裝插件,建議IDE更新到最新版本,下載最新版本的插件。非最新版本IDE,可能會有一些安裝使用問題。

step2. 配置插件

LINT
lint:https://developer.android.com/studio/write/lint?hl=zh-CN#manuallyRunInspections
限制只檢查指定規則的方法:
1> 在build.gradle裏面
apply 'com.android.lib'
apply 'com.android.application'
的項目下進行配置。
1、在build.gradle最後添加

android{
lintOptions{
abortOnError false
//check 'NewApi'
lintConfig file('lint.xml')
}
}
2> 拷貝配置文件lint.xml文件到build.gradle所在目錄

step3. 使用插件進行檢查

LINT
1> 在 Android 視圖中,打開項目並選擇此項目以及您要分析的文件夾或文件。
2> 從菜單欄選擇 Analyze > Inspect Code。
3> 在 Specify Inspection Scope 對話框中查看設置。

4> 在 Inspection Profile 中,保留默認的配置文件 (Project Default)。
5> 點擊 OK 以運行檢查,顯示運行 Inspect Code 所生成的 Lint 和其他 IDE 檢查結果。

關於具體的配置定製見 https://developer.android.com/studio/write/lint?hl=zh-CN#manuallyRunInspections
 

PMD
限制只檢查指定規則的方法:
1> 配置檢查規則: IDE => Performances => Other Settings => PMD

2> 點擊加號添加規則,選擇附件中給出的pmd規則

3> 使用配置的檢查規則進行檢查: IDE => Run PMD => Custom Rules => highpriority_config

4> 開始pmd檢查,並顯示檢查的結果。

 

P3C
未發現可以在plugin中配置指定規則的方法。
1> 檢查: IDE => 阿里編碼規約 => 編碼規約掃描

特別的,LINT/P3C可以一起選擇後進行檢查。在build.gradle裏面的配置的lintConfig在命令行、IDE檢查中均有效。
方法: Analyze => Inspect Code => 打開配置 => 同時選擇Ali-Check 和 Android lint。

 

2、命令行方法檢查(按需查閱)

LINT
限制只檢查指定規則的方法:
1> 在build.gradle裏面
apply 'com.android.lib'
apply 'com.android.application'
的項目下進行配置。
a. 在build.gradle最後添加

android{
lintOptions{
abortOnError false
//check 'NewApi'
lintConfig file('lint.xml')
}
}


b. 拷貝配置文件lint.xml文件到build.gradle所在目錄

lint只會檢查包含了AndroidManifest.xml的應用項目
非獨立應用lint檢查命令:
lint code_dir --config lint.xml --xml result.xml
獨立應用lint檢查命令:
./gradlew lintRelease [-p project]
結果路徑會顯示在日誌打印中,按settings.gradle 裏面的項目來分別生成結果文件。

PMD/P3C
1> 下載源碼
https://github.com/pmd/pmd
https://github.com/alibaba/p3c

2> 進入到相應用項目目錄進行編譯 mvn install
pmd/pmd-java
p3c/p3c-pmd
(若pmd編譯有問題,參考https://github.com/xnfreedom/pmd/commit/93ffb8408dd428c05f8294ea5f6dec9ef7b7411f、
https://github.com/xnfreedom/pmd/commit/c32fa0f725c61f2fc7434dc0a400251c7ca9de29解決)

3> pmd工具下載、配置
https://pmd.github.io/
命令行方式運行p3c: https://github.com/alibaba/p3c/issues/50

4> 配置要使用的規則。
pmd:
文件位置:pmd/pmd-java/target/classes/category/java/high_priority_config.xml
內容:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="pmd_high_priority"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Rules specific to 1/2 priority
</description>
<!-- Rules, that have been moved into a category -->
<rule ref="category/java/bestpractices.xml/AvoidReassigningParameters" deprecated="true" />
<rule ref="category/java/bestpractices.xml/GuardLogStatement" deprecated="true" />
</ruleset>

p3c:
文件位置:p3c/p3c-pmd/target/classes/rulesets/java/high_priority_config.xml
內容:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="p3c_high_priority"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Rules specific to 1/2 priority
</description>
<!-- Rules, that have been moved into a category -->
<rule ref="rulesets/java/ali-comment.xml/EnumConstantsMustHaveCommentRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/ThreadPoolCreationRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/AvoidUseTimerRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/AvoidManuallyCreateThreadRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/ThreadShouldSetNameRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/AvoidCallStaticSimpleDateFormatRule" deprecated="true" />
<rule ref="rulesets/java/ali-concurrent.xml/ThreadLocalShouldRemoveRule" deprecated="true" />
<rule ref="rulesets/java/ali-constant.xml/UpperEllRule" deprecated="true" />
</ruleset>

注意:
deprecated="true" 是表示在pmd 7.0.0將會不再支持這條規則。
其實不是所有的都需要打上這個tag, 部分規則需要。

5> 命令行運行pmd/p3c檢查
pmd:
/Users/mz/tools/pmd/pmd-bin-6.16.0/bin/run.sh pmd -d /Users/mz/Code/apps/FileManager/FileManager -f xml -R /Users/mz/Code/github/pmd/pmd-java/target/classes/category/java/high_priority_config.xml > /Users/mz/Code/apps/FileManager/FileManager/preferdCheckResut/pmd-high_priority_result.xml
p3c:
/Users/mz/tools/pmd/pmd-bin-6.16.0/bin/run.sh pmd -d /Users/mz/Code/apps/FileManager/FileManager -f xml -R /Users/mz/Code/github/p3c/p3c-pmd/target/classes/rulesets/java/high_priority_config.xml > /Users/mz/Code/apps/FileManager/FileManager/preferdCheckResut/p3c-high_priority_result.xml

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章