Using the PMD plugin

Another tool to analyze the Java source code is PMD. It finds unused variables, empty catch blocks, unnecessary object creation, and so on. 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 it 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 Bintray JCenter 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 { 
    // We need a repository, so the 
    // PMD dependencies can be downloaded. 
    jcenter() 
} 
 
sourceSets { 
    // New source set with 
    // the name util. 
    util 
} 

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

$ gradle check
:pmdMain
:compileJava
:processResources UP-TO-DATE
:classes
:pmdTest UP-TO-DATE
:pmdUtil
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check
BUILD SUCCESSFUL
Total time: 1.735 secs

Note the pmdMainpmdTest, 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 that the build does not fail. The following sample build shows how to set the ignoreFailures property to true:

apply plugin: 'java' 
apply plugin: 'pmd' 
 
repositories { 
    jcenter() 
} 
 
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 output filename or we can also disable the report generation.

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

apply plugin: 'java' 
apply plugin: 'pmd' 
 
repositories { 
    jcenter() 
} 
 
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 
} 
 
// Special configuration for the pmd task 
// that runs for the source set main. 
pmdMain { 
    reports { 
        xml { 
            // Change output file for XML report. 
            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 the rule sets that are applied, we can use the ruleSets property and ruleSets() method. With the ruleSets() method, we have a convenient way to add new rules. With the ruleSets property, we have to define all the rules that we want to use as a property assignment.

Besides configuring the rule sets, we can also assign rule set files for 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 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 to set rules and rule set files:

apply plugin: 'java' 
apply plugin: 'pmd' 
 
repositories { 
    jcenter() 
} 
 
pmd { 
    // Add rule sets with the ruleSets method. 
    ruleSets 'design', 'braces' 
 
    // Or use property syntax. 
    // ruleSets = ['design', 'braces'] 
 
    // Set rule set files via the task 
    // property ruleSetFiles. 
    ruleSetFiles = files('config/pmd/customRules.xml') 
 
    // Or use ruleSetFiles method to add new file 
    // to existing collection of files. 
    //ruleSetFiles file('config/pmd/customRules.xml') 
} 

To change the version of PMD that we want to use, we must set the toolVersion property of the PMD plugin. At the time of writing this book, this was set to version 5.2.3, but we can change it to other versions if required. In the following example build file, we will simply change the version to 5.2.3 with the toolVersion property:

apply plugin: 'java' 
apply plugin: 'pmd' 
 
repositories { 
    jcenter() 
} 
 
pmd { 
    // Use a different version of PMD. 
    toolVersion = '5.4.1' 
} 
..................Content has been hidden....................

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