Using the PMD plugin

Another tool for analyzing the Java source code is PMD. It finds unused variables, empty catch blocks, unnecessary object creation, and so forth. We can configure our own rule sets and even define our own rules. To use PMD with Gradle, we have to apply the PMD plugin to our build. After we have added the plugin, we have the pmdMain and pmdTest tasks already installed. These tasks will run PMD rules for the main and test source sets. If we have a custom source set, then the plugin adds a pmd<SourceSet> task as well. These tasks are also dependency tasks of the check task. So if we invoke the check task, all the pmd tasks are executed as well.

This plugin only defines a structure to work with PMD, but doesn't contain the actual PMD library dependencies. Gradle will download the PMD dependencies the first time that we invoke the pmd tasks. We have to define a repository that contains the PMD libraries, such as the Maven Central repository or a corporate intranet repository.

In the following build file, we apply the PMD plugin and define a custom source set:

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

repositories {
  mavenCentral()
}

sourceSets {
  util
}

When we invoke the check task, and if there are no rule violations, we get the following output:

$ gradle check
:pmdMain
:pmdTest UP-TO-DATE
:pmdUtil UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
:check

BUILD SUCCESSFUL

Total time: 6.497 secs

Note the pmdMain, pmdTest, and pmdUtil tasks that are executed.

If one of the files has a violation, then the build will fail by default. We can set the ignoreFailures property for the pmd tasks to true, so the build will not fail. The following sample build shows how we can set the property ignoreFailures to true:

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

repositories {
  mavenCentral()
}

sourceSets {
  util
}

pmd {
  // Don't fail the build process when
  // rule violations are found.
  ignoreFailures = true
}

Rule violations will be reported in an XML and HTML file in the build/reports/pmd directory. The name of the file is the same as the source set name. We can change the name of the reporting directory and the output filename, or we can also disable the report generation.

The following example build file changes several properties of the reporting by the pmd tasks:

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

repositories {
  mavenCentral()
}

sourceSets {
  util
}

pmd {
  // Change base reporting dir for PDM reports.
  reportsDir = file("${reporting.baseDir}/pmd-output")
}

configure(tasks.withType(Pmd)) {
  // Disable HTML report generation for all PDM tasks.
  reports.html.enabled = false
}

// Change output file for the single task pmdMain.
pmdMain {
  reports {
    xml.destination = file("${pmd.reportsDir}/pmd.xml")
  }
}

Only the basic rule set of PMD is applied if we don't define anything else in the build file. To change which rules are applied, we can use the rules property and the rules() method. With the rules() method, we have a convenient way to add new rules. With the rules property, we have to define all the rules we want to use as a property assignment.

Besides configuring the rules, we can also assign rule set files for the pmd tasks. A rule set file contains several rules and allows customization of the rules. To add a rule set file, we can use the ruleSetFiles property or the ruleSetFiles() method. We need to reference a file to set the property or pass it as a method argument.

The following sample build file shows how we can set rules and rule set files:

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

repositories {
  mavenCentral()
}

pmd {
  // Add rules.
  ruleSets 'design', 'braces'
  // Or use property syntax.
  // ruleSets = ['design', 'braces']

  // Set rule set file.
  ruleSetFiles = [file('config/pmd/customRules.xml')]
  // Or use method.
  //ruleSetFiles file('config/pmd/customRules.xml')
}

To change the version of PMD that we want to use, we must set the property toolVersion of the PDM plugin. When this book was being written, this was set to version 4.3, but we can change it to other versions if required. In the following example build file, we simply change the version to 4.2 with the toolVersion property:

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

pmd.toolVersion = 4.2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.212