I already wrote an article about using the same Checkstyle ruleset and Checkstyle suppressions file in Eclipse and Maven [1]. One arising question in the comment section was, how one can use the ruleset on a Maven multi-module project.
Posts Tagged ‘Checkstyle’
Using Checkstyle’s suppression filters on Windows and Linux
I have already shown the usage of Checkstyle’s suppression filters in Eclipse and Maven in [1]. But after switching from Linux to Windows (please no comments on that…) I wondered about occurring Checkstyle rule violations I actually had excluded in my checkstyle-suppressions.xml. Those rules were mainly simple exclusions of all files within a certain package:
<suppress checks="[a-zA-Z0-9]*" files="src/main/java/de/foo/bar" />
After searching around, I realized that this was an issue with the given regex in the files-attribute [2]. The simple slash was correctly interpreted on Linux, but didn’t work on Windows. So to make the rules work on both operation systems I replaced the path separator with “[/\\]”:
<suppress checks="[a-zA-Z0-9]*" files="src[/\\]main[/\\]java[/\\]de[/\\]foo[/\\]bar" />
Reference
- [1] https://rolf-engelhard.de/?p=46
- [2] http://checkstyle.2069334.n4.nabble.com/suppression-filter-regex-td2070198.html
Clearing Checkstyle’s Cache in Eclipse
First of all please notice the nice alliteration in the headline.
A little problem which bugged me while searching the solution for [1] was that I had to change my checkstyle.xml and checkstyle-suppression.xml for several times. Unfortunately Eclipse-CS never noticed these modifications. So I restarted Eclipse every time to make the changes apply.
Then I stumbled by chance on an entry in Eclipse-CS’s bug tracker [2]:
“Alternatively to touching the Checkstyle configuration file you can clear the plugin’s caches by clicking the little yellow refresh button on the Checkstyle preferences page.”
And hey—after putting on my glasses and lowering the nose-screen-distance to “way to near”—there it was: a little yellow button on the preference page!
Reference
- [1] https://rolf-engelhard.de/?p=239
- [2] http://sourceforge.net/tracker/?func=detail&aid=1752586&group_id=80344&atid=559497
Using the same suppression filter for Checkstyle in Eclipse and Maven
Code Quality
We all agree—code quality is an important component of the development process. And we all agree—less times is spent for code quality than it should be. According to [1] code has to be written with an eye on readability which influences the costs in development process directly. Kent argues, that the easier your code can be understood by another developer, the faster he or she could be productive in bug fixing, extending or refactoring. And: the easier your code can be understood, the less new bugs are implemented.
There are some great tools out there, which could help to improve code quality, like Checkstyle [2], Findbugs [3] and PMD [4]. Checkstyle is a static code analysis tool which—unlike e.g. FindBugs—works on the source code directly. You can use some pregiven rule sets or define your own ones, you are able to change the rules themselves (e.g. adopt thresholds) and you are able to use them in Eclipse via the plugin Eclipse-CS [5] and in Maven (using the Maven Checkstyle Plugin [6]).
The problem
Now let us assume, that you are working in a software project with more than just you as a developer and you are using Checkstyle. It’s obvious that the rule set should be shared. And if you are using a suppression filter, Eclipse and Maven—you’re in trouble.
So let me summarize the boundary values quickly:
- You are working with Eclipse and Eclipse-CS
- You are using Maven in your buildsystem
- You have your Checkstyle rules in a file “checkstyle.xml” which lies inside your project (here in the folder ‘checks’)
- You also have a configuration file “checkstyle-suppressions.xml” holding suppression rules inside your project (in the folder ‘checks’)
- (Here’s the problem:) You want to use THOSE two files for Eclipse AND for Maven
So your workspace might look like this:
By using M2Eclipse and checkstyle-m2eclipse [7] you might be out of trouble (I haven’t tried it yet). According to the Checkstyle’s website these tools will do the synchronizing for you.
I also want to mention, that there’s no problem if you are using the suppression file just in Maven—simply add the following to your pom.xml [8]:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <configuration> <configLocation>${basedir}/checks/checkstyle.xml</configLocation> <suppressionsLocation>${basedir}/checks/checkstyle-suppressions.xml</suppressionsLocation> </configuration> </plugin>
And configuring Eclipse for using the suppressions could be done in the Checkstlyle-Plugin directly (e.g. select “Window” → “Preferences” → “Checkstyle” in your IDE) or in the checkstyle.xml. Here you simpy add:
<module name="SuppressionFilter"> <property name="file" value="${config_loc}/checkstyle_suppressions.xml"/> </module>
Note that the place holder “${config_loc}” is a build-in variable of Eclipse-CS and will point to the same directory where the checkstyle.xml lies. So for Eclipse that’s all—the suppression file will be used as soon as you do a new scan with Checkstyle. But if you call ‘mvn checkstyle:checkstyle’ (assuming you have added the checkstyle-plugin to your pom.xml like above) this will fail due to the fact that Maven doesn’t know ${config_loc}.
The solution
But there’s a way to let Maven know—and that’s also the solution of the given problem. Add the SuppressionFilter to the checkstyle.xml as shown above. Then add to your pom.xml:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <configuration> <configLocation>${basedir}/checks/checkstyle.xml</configLocation> <propertiesLocation>${basedir}/checks/checkstyle_maven.properties</propertiesLocation> </configuration> </plugin>
The point is you are using a property file, declared by propertyLocation. This file (here checkstyle_maven.properties) lets Maven bind the variable ${config_loc} to the local directory ‘checks’ in which our checkstyle-suppressions.xml lies. The content of checkstyle_maven.properties is simply one single line:
config_loc=checks
Rerun Maven and a Checkstyle scan in Eclipse—now you are using the same rule set and the same suppressions.
Reference
- [1] . 2010. Implementation Patterns. Addison-Wesley
- [2] http://checkstyle.sourceforge.net
- [3] http://findbugs.sourceforge.net
- [4] http://pmd.sourceforge.net
- [5] www.eclipse-cs.sourceforge.net
- [6] http://maven.apache.org/plugins/maven-checkstyle-plugin
- [7] http://eclipse-cs.sourceforge.net/maven.html
- [8] http://maven.apache.org/plugins/maven-checkstyle-plugin/examples/suppressions-filter.html