An overview of the

Recently issued “alibaba Java development manual” ali, countless fans such as ali won Wu Mu letter, in ali and by the way, recently released a Java development code plug-ins > < alibaba, can easily in the code word phase to get the corresponding coding tips, so, how to apply in the Gradle ali development code for code review. See below.

Ali development protocol plugin is based on PMD code detection, so using Gradle development protocol check only needs to use the PMD plugin provided by Gradle to achieve the purpose. Currently, ali development specification provides the following rule configurations. To apply these configurations, you only need to configure them into PMD check rules.

  • ali-comment.xml
  • ali-concurrent.xml
  • ali-constant.xml
  • ali-exception.xml
  • ali-flowcontrol.xml
  • ali-naming.xml
  • ali-oop.xml
  • ali-orm.xml
  • ali-other.xml
  • ali-set.xml

PMD(Project Manager Design) is an open source tool for analyzing Java code errors. Unlike other analysis tools, PMD learns about code errors through static analysis. That is, reporting an error without running a Java program. PMD comes with a number of rules that you can use directly to find many problems with Java source programs. In addition, users can define their own rules to check that Java code conforms to certain coding specifications. At the heart of PMD is the JavaCC parser generator. PMD uses JavaCC and EBNF (Extended Backus-Naur Formal) Syntax together with JJTree to parse Java source code into an Abstract Syntax Tree (AST).

The above content is quoted from baidu Encyclopedia -PMD entry

use

apply plugin: 'java'
apply plugin: 'pmd'

ext {
    p3c = "1.3.0"
}

pmd {
    consoleOutput = true
    reportsDir = file("build/reports/pmd")

    ruleSets = [
        "java-ali-comment"
    ]
}

repositories {
   jcenter()
}

dependencies {
    pmd "com.alibaba.p3c:p3c-pmd:${p3c}"
}Copy the code

There are a couple of caveats here

  • Gradle’s PMD plugin isruleHave added the defaultjava-Prefixes, don’t lose them
  • The scope of dependencies ispmd, so the dependency will be added topmdClasspathFor thepmdused
  • For some additional configurations of this plug-in, see the PMD plug-in DSL manual

Run to check

The plug-in provides the following tasks

The name of the task describe
pmdMain Check the code under SRC /main/ Java
pmdTest Check the code under SRC /main/test
pmdSourceSet Examine the code for a given range
check Review the source code and unit test code

You can run the corresponding tasks as required for code review.

// Run the pmdMain task in short form
gradle pMCopy the code

The output inspection reports can be found in the build/ Reports/PMD directory defined by us