We have seen how simple it is to create a Gradle build job in Jenkins. We will now add Checkstyle and PMD plugins to our project for quality checking purposes. There are different approaches that we can follow in order to use these plugins. We can directly add these plugins to Jenkins and run it for our project, or we can use Gradle Checkstyle and PMD plugins and evaluate the project.
We will use the Gradle approach to add Checkstyle and PMD plugins for code quality check, and execute this using Jenkins. Let's create two Gradle files, one for Checkstyle and the other for PMD:
build_checkstyle.gradle
apply plugin: 'groovy' apply plugin: 'eclipse' apply plugin: 'checkstyle' version = '1.0' repositories { mavenCentral() } checkstyle { toolVersion = 6.5 ignoreFailures = true } dependencies { compile gradleApi() compile localGroovy() compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
In the build file, we have added additional configuration in the closure checkstyle { … }
. If the source code does not pass the CheckStyle rules, it results in build failure. To ignore any build failure due to Checkstyle rule violation; we need to add the ignoreFailures=true
property in the checkstyle
closure.
Checkstyle plugin provides the following tasks:
checkstyleMain
: This executes Checkstyle against the Java source filescheckstyleTest
: This executes Checkstyle against the Java test source filescheckstyleSourceSet
: This executes Checkstyle against the given source set's Java source filesFor Checkstyle plugin, we need a checkstyle.xml
file in the <Project>/config/checkstyle/
directory. This is the default location. You can find a sample checkstyle.xml
at: https://github.com/google/google-api-java-client/blob/dev/checkstyle.xml.
It provides a standard quality checks for projects. You can write customized checkstyle.xml
for your requirements as well.
To use PMD plugin, you can copy the above file and replace checkstyle
closure with pmd
closure and remove the toolVersion
property. If you don't specify a version, Gradle downloads PMD version 5.1.1 by default. You will also need to add apply plugin: pmd
.
build_pmd.gradle
apply plugin: 'groovy' apply plugin: 'pmd' version = '1.0' repositories { mavenCentral() } pmd{ ignoreFailures = true } dependencies { compile gradleApi() compile localGroovy() compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
PMD plugin provides the following tasks:
pmdMain
: This executes PMD against the Java source files.pmdTest
: This executes PMD against the Java test source files.pmdSourceSet
: This executes PMD against the given source set's Java source filesBoth the Checkstyle and PMD plugins can be executed using check
task.
We will create a new project QualityCheck
and add the following files to the project:
build_checkstyle.gradle
build_pmd.gradle
config/checkstyle/checkstyle.xml
Checkstyle and PMD plugin are executed in Java code, so we will add some sample Java files under the src/main/java/
directory. To create a build step in Jenkins, we will create a build step to execute a Checkstyle task (check
task), as shown in Figure 7.15. You can repeat the same steps for PMD plugin.
For a new configuration, Root Build script
is set to ${workspace}/Chapter7/QualityCheck
. Also, we added the Build file name in the text box as build_checkstyle.gradle
.
Save this configuration and execute the job again. As configured, build_checkstyle.gradle
file executed on java source code and generated CheckStyle reports for it. You can find the reports under ${workspace}Chapter7QualityCheckuild
eportscheckstylemain.xml
18.220.53.93