Ali's development specification plug-in is based on PMD -based code detection, and there is already a Gradle plug-in for PMD.
After referring to the article on the use of custom rules for the gradle pmd plugin , the remaining question is how to introduce the rules formulated by Ali.
Custom RuleSet
Create a new file etc/pmd/ruleset.xml with the following contents:
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
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 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
自定义Rule set
</description>
<!-- 引入PMD制定的Rule, 来源于https://github.com/pmd/pmd/tree/master/pmd-java/src/main/resources/rulesets/java -->
<rule ref="rulesets/java/android.xml">
<exclude name="CallSuperLast"/>
</rule>
<rule ref="rulesets/java/basic.xml">
<exclude name="CollapsibleIfStatements"/>
</rule>
<rule ref="rulesets/java/clone.xml"/>
<rule ref="rulesets/java/finalizers.xml"/>
<rule ref="rulesets/java/imports.xml"/>
<rule ref="rulesets/java/javabeans.xml"/>
<rule ref="rulesets/java/optimizations.xml">
<exclude name="LocalVariableCouldBeFinal"/>
<exclude name="MethodArgumentCouldBeFinal"/>
</rule>
<rule ref="rulesets/java/sunsecure.xml"/>
<rule ref="rulesets/java/unnecessary.xml">
<exclude name="UselessParentheses"/>
</rule>
<!-- 引入阿里的Rule, 来源于 https://github.com/alibaba/p3c/tree/master/p3c-pmd/src/main/resources/rulesets/java -->
<rule ref="rulesets/java/ali-comment.xml">
</rule>
<rule ref="rulesets/java/ali-concurrent.xml">
</rule>
<rule ref="rulesets/java/ali-constant.xml">
</rule>
<rule ref="rulesets/java/ali-exception.xml">
</rule>
<rule ref="rulesets/java/ali-flowcontrol.xml">
</rule>
<rule ref="rulesets/java/ali-naming.xml">
</rule>
<rule ref="rulesets/java/ali-oop.xml">
</rule>
<rule ref="rulesets/java/ali-orm.xml">
</rule>
<rule ref="rulesets/java/ali-other.xml">
</rule>
<rule ref="rulesets/java/ali-set.xml">
</rule>
</ruleset>
As can be seen from the above example, it is very convenient to customize the rule. You can even queue unwanted rules using the exclude tag.
build.gradle
apply plugin: 'java'
apply plugin: 'pmd'
ext {
p3cPmdVersion = "1.3.0"
pmdVersion = '5.5.2' # 与p3c-pmd 中使用pmd的版本一致
}
// PMD配置
pmd {
toolVersion = pmdVersion // 避免使用比较旧的版本,需要指定pmd的版本
ignoreFailures = true
ruleSetConfig = resources.text.fromFile("etc/pmd/ruleset.xml")
}
dependencies {
pmd "com.alibaba.p3c:p3c-pmd:${p3cPmdVersion}"
}
As can be seen from the above example, using p3c-pmd is actually very simple, just import some of its rules into the ruleSetConfig file.
perform inspection
The PMD plugin provides the following tasks:
Task Name... Description
pmdMain ...... check the code under src/main/java
pmdTest ...... check the code under src/main/test
pmdSourceSet ...... check the code for the given scope
check ...... check the source code and unit test code
Execute the following command:
gradle check
The execution results will be output to files such as main.html and test.html in the build/reports/pmd directory.