Skip to content

Checkstyle and PMD

NikithaDidigam edited this page Apr 4, 2023 · 3 revisions

Tools used with versions:

Checkstyle : 7.8.1

PMD : 6.37.0

Checkstyle plugin:

The Checkstyle plugin performs quality checks on your project’s Java source files using Checkstyle and generates reports from these checks. Usage: To use the Checkstyle plugin, include the following in your build script:

image

For checking such rules, you first define such rules generally in checkstyle.xml file and if you want to suppress or avoid any defined rule in checkstyle.xml file as an exception for any class or module then you can define suppression rules in checkstyle-suppressions.xml file and include this file into checkstyle.xml file.

By default, the Checkstyle plugin expects configuration files to be placed in the root project, but this can be changed and you need to configure in the build.gradle script. The default location of checkstyle.xml file is /config/checkstyle/checkstyle.xml. If you have checkstyle-suppressions.xml or suppression.xml file then you can also put this file under config/checkstyle directory.

If you do not want to use the default location of the checkstyle configuration file then you can create checkstyle configuration directory and put your checkstyle configuration files. Suppose you have created directories quality/checkstyle under project’s root directory you have put your checkstyle.xml and suppressions.xml files. In this case you need the following configurations in your build.gradle script: image

The above snippet gives the value of toolVersion we are using and checkstyle.xml file location. If you want to add suppressions.xml, you can define the file location here.

Checkstyle.xml validation example: image

The validation above is mentioned with the tags . The above example shows us that the file length should not exceed 3000 lines. In the same way we can configure more validations in the same file.

Testing the application:

In my demo project, I have used the above-mentioned validation that file should not exceed 25 lines.

Now navigate to the project root directory using cmd prompt and execute the command gradle clean build or gradlew clean build. You should see the some errors in the report. image After running the command, we can see that there are some violations in the project. Some of the validations which we have mentioned in the checkstyle.xml has been failed.

You can open the error report file (java-project-checkstyle/build/reports/checkstyle/main.html) and check what are the errors you have encountered in your Java file. When you open the report file main.html then you see following errors:

image The above snippet gives us the clear idea regarding which file in the project has failed validations.

image

The above snippet tells us which file has the error and what is the error. Based on this report, we can resolve the errors and can help to comply with good programming practices which improve the code quality, readability, re-usability, and reduce the cost of development.

# Using PMD:

PMD is a static source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It’s mainly concerned with Java and Apex, but supports 12 other languages.

PMD is most useful when integrated into your build process. It can then be used as a quality gate, to enforce a coding standard for your codebase. Among other things, PMD can be run: • As a Maven Goal • As an Ant Task • As a Gradle Task • From command line

Adding pmd to project:

To use PMD plugin your project, add the below plugin in your build script.

image

For checking such rules, you first define such rules generally in pmd.xml file. By default, the pmd plugin expects configuration files to be placed in the root project, but this can be changed and you need to configure in the build.gradle script. The default location of pmd.xml file is /config/pmd/pmd.xml. If you want to use pmd base_rules you can use pmd-base.xml file and can be place under /config/pmd/pmd-base.xml

If you do not want to use the default location of the pmd configuration file then you can create pmd configuration directory and put your pmd configuration files.

image

The above snippet shows us the toolVersion we are using and ruleset file location. If you want to use any particular default rules, you can add it in ruleSets[] array.

Pmd.xml validation example:

image

The above mentioned is a rule which will give us error if a method name does not exceeds 2 characters. In the same way we can use other default rules for java which are mentioned below. https://pmd.github.io/latest/pmd_rules_java.html

Testing the application : Now navigate to the project root directory using cmd prompt and execute the command gradle clean build or gradlew clean build. You should see the some errors in the report.

image

A report will be generated with the errors. You can open the error report file (java-project-pmd/build/reports/pmd/main.html) and check what are the errors you have encountered in your Java file. When you open the report file main.html then you see errors encountered.

Based on the error report, one can help to comply with good programming practices which improve the code quality, readability, re-usability, and reduce the cost of development.

Clone this wiki locally