What most annoying programmers besides reading someone else’s code? That’s right, formatting! In a large team it is difficult to impart to everyone the same requirements for the style of the program code. One way out of this situation is to automatically configure the code style check.
This can easily be configured with the help of a checkstyle.
Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java. This makes it ideal for projects that want to enforce a coding standard.
Let’s create a sample code from our article and try to check it for the google check style guide.
Checking for violations as part of the build
We need to edit pom.xml to add the checkstyle settings.
1 2 3 4 5 6 7 8 9 |
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <checkstyle.config.location>google_checks_custom.xml </checkstyle.config.location> <checkstyle.suppressions>checkstyle_suppressions.xml </checkstyle.suppressions> </properties> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>2.17</version> <configuration> <consoleOutput>true</consoleOutput> </configuration> <executions> <execution> <goals> <goal>check</goal> </goals> <configuration> <suppressionsLocation>checkstyle_suppressions.xml</suppressionsLocation> <suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.3</version> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> |
checkstyle.config.location – is the name of the file with the settings
checkstyle.suppressions – an exception file, it allows you to specify which rules can be ignored.
Let’s try to test our project.
1 |
mvn checkstyle:check |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[INFO] There are 22 errors reported by Checkstyle 8.3 with google_checks_custom.xml ruleset. .. skipped .. [ERROR] src/main/java/test/word/report/generate/DocxContext.java:[6] (whitespace) [ERROR] src/main/java/test/word/report/generate/GenerateReport.java:[36] (indentation) Indentation: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.449 s [INFO] Finished at: 2018-03-09T17:22:23+06:00 [INFO] Final Memory: 23M/77M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (default-cli) on project word-report-generate: You have 22 Checkstyle violations. -> [Help 1] [ERROR] |
So we see that in our project there are 22 inconsistencies to our checkstyle.
On each line there is a file where it happened and the string is very convenient for fixing the problem.
Generate checkstyle report as standalone
You can also generate the Checkstyle report by explicitly executing the checkstyle:checkstyle goal from the command line. You are not required to specify the Checkstyle Plugin in your pom.xml unless you want to use a specific configuration.
1 |
mvn checkstyle:checkstyle |
Generate checkstyle html report as part of the project reports
To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the <reporting> section of your pom.xml .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.0.0</version> <reportSets> <reportSet> <reports> <report>checkstyle</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting> |
and add dependencies in build/plugins sections
1 2 3 4 5 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> |
Now run mvn site
After the build, open report file target/site/checkstyle.html. We will see all checkstyle violation.
In conclusion, I’ll link to the official google style guide.
You can download full source here
Link to source?
Hello Greg!
I renewed the article and added link:
https://github.com/Tirex/checkstyle_example
I have been trying to skipCheckStyle in the automation module alone of my multi module maven project.
below is my config and maven version in my mac os is 3.1.1 and it skips the check style when I run
-test
-DtestNG=TestNgFitmentSevice.xml
-Dendpoint=xxxtest.com:8080
-Dcustomgroup=REGRESSION
-DskipCheckstyle=true
org.apache.maven.plugins
maven-checkstyle-plugin
2.17
true
${checkstyle.config.location}
UTF-8
true
false
But the very same does not work in Jemkins , In Jenkins the check-style gets kicked off even if I want to skip it . Any idea on how to fix this ? The maven version is 3.1.1 in jenkins as in local and java version is 1.8 JDK .
It depends on your Jenkins configuration. You need to sure that the maven receives your parameter -DskipCheckStyle=true. Check your Jenkins job output.