1. The background
Using code analysis tool PMD to check Android Java code defects, this article is a tidy up.
introduce
PMD is a static source code analyzer. It finds common programming flaws such as unused variables, empty catch blocks, unnecessary object creation, and so on. It focuses on Java and Apex, but supports six other languages. PMD has a number of built-in checks (in PMD terminology, rules) that are documented in the rule reference for each language. We also support extensive apis to write your own rules, which you can execute using Java or as self-contained XPath queries. PMD is most useful when integrated into the build process. It can then be used as a quality gate to enforce coding standards for the code base. Among other things, PMD can run:
- As a goal for Maven
- As an Ant task
- As a Gradle task
- From the command line
Website address PMD. Making. IO/PMD/index. H…
Using PMD cli Demo github.com/vir56k/demo…
Gradle using PMD Demo github.com/vir56k/demo…
2. Run the PMD command
2.1 Understand the user manual first
PMD. Making. IO/PMD/pmd_use…
2.2 download
Go to the official website, download the zip package, unzip the download address github.com/pmd/pmd/rel…
2.3 Command Line Format for Performing Detection
./run.sh pmd -d .. /.. /.. /src/main/java/ -f text -R rulesets/java/basic.xmlCopy the code
2.4 My script
${CUR} basepath=$(CD 'dirname' $0 '; PWD) echo Parent directory of the currently executed script file :${basepath} PMD_HOME=$basepath/pmd-bin-6.12.0 echo PMD_HOME:${PMD_HOME} PROJ_DIR=$(CD) ${basepath}; cd .. /.. /; pwd) echo PROJ_DIR:${PROJ_DIR} SRC=${PROJ_DIR}/app/src/main/java FORMAT=html RULE=rulesets/java/basic.xml ${PMD_HOME}/bin/run.sh pmd -d ${SRC} -f ${FORMAT} -R ${RULE}Copy the code
3. Use PMD in Gradle mode
3.1 Referencing Plug-ins
apply plugin: ‘pmd’
3.2 writing task
def configDir = "${project.rootDir}/scripts"
def reportsDir = "${project.buildDir}/reports"
task pmd(type: Pmd) {
ignoreFailures = true
ruleSetFiles = files("$configDir/pmd/pmd-ruleset.xml")
ruleSets = []
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$reportsDir/pmd/pmd.xml"
}
html {
destination "$reportsDir/pmd/pmd.html"
}
}
}
Copy the code
3.3 Associating a Task with a Check Task
check.dependsOn 'pmd'
Copy the code
3.4 to perform the check
./gradlew check
Copy the code
Check the report
Located in: Build /reports/ PMD folder
image.png
4. Reference
PMD. Making. IO/PMD/index. H…