Java7
Posts tagged checkstyle
Integrate project specific checkstyle into your Maven build goals :)
Nov 7th
Firstly, in almost every development project that has not passed its prime, everyone adheres to some kind of coding checklist. But overtime (especially during the maintenance and support phase) the topic becomes like a lip service, in most scenarios no one would govern it, everyone cuts some slack and the source code becomes an expensive garbage.
Or worse of all, someone comes up with some coding checklist full of anti-patterns. :)
Checkstyle is a very mature code checking library developed some time ago for this purpose.
Today we are going to show you how to integrate checkstyle into your Maven build. I use Maven for project development management but you can do equally well with Ant or other build tools. (Just to note that Maven is not just a build tool! :P)
Assumptions
1. You are using Maven
2. You have installed CheckStyle plugin in your Eclipse IDE (Fine if you are not using Eclipse, you may run parts of the example off from the command line $mvn)
3. You are using Eclipse IDE@@ (Again, you don’t have to although most of example here will illustrate using Eclipse IDE)
4. By reading this post we have actually assumed you are an advanced and intelligent developer..
Steps
1. Have your external Checkstyle configuration file in hand. See http://java.sg/allow-project-specific-settings-in-checkstyle-plugin/
2. Locate and open your pom.xml (Project Object Model)
3. Insert the following code snippet into your plugins.
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.8</version>
<configuration>
<configLocation>C:\checkstyle-javasg.xml</configLocation>
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>
Substitute configLocation with your own configuration file. In addition, this value may be a URL, File, or build classpath resource reference.
4. Add the checkstyle:check goal into your Maven task.
Go to Project > Run Configurations
Browse to your Maven build configuration, add “checkstyle:check” to your goals.
Apply and run.
Note: If you are running this off the command line, this would suffice:
$mvn checkstyle:check clean package
Checkstyle stops me from continuing with the violation:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hms-web 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-checkstyle-plugin:2.8:check (default-cli) @ hms-web --- [INFO] [INFO] There are 1 checkstyle errors. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.529s [INFO] Finished at: Mon Nov 07 14:53:51 SGT 2011 [INFO] Final Memory: 19M/64M [INFO] ------------------------------------------------------------------------
Now, your codes are automatically checked by checkstyle. Checkstyle fails with an error and will not allow Maven to continue with the rest of the goals until you have rectified the Checkstyle violations. :)
In a later tutorial, i will post how Checkstyle may be integrated into Jenkins (Fork of Hudson).
Reference
Allow project specific settings in checkstyle plugin
Nov 7th
Sometimes old rules such as 80 line width limit doesn’t apply anymore in this age. New compilers are able to handle lines longer than 80 characters, and our monitors are beyond 15 inches wide.
Such rules are still checked against if you are using default check configuration such as sun_checks.xml.
This can be easily overridden by supplying your own checkstyle configuration descriptor. You will need a ‘baseline’ copy to start off however, this can be achieved by starting off with sun_checks.xml.
1. Get sun_checks.xml
If you have decided to start off with sun_checks.xml as your baseline copy, have this configuration description ready on hand.
You may get this through the binary downloads. Download checkstyle from: http://checkstyle.sourceforge.net/
Alternatively, you may also download my copy, i took this from the download above.
2. Rename sun_checks.xml and place it somewhere in your development directory.
I renamed sun_checks.xml (or sun_checks.xml.readonly.txt if you have downloaded my copy) to checkstyle-javasg.xml and have it copied to my own development directory. You can choose another name you like, e.g. check-john.xml.
C:\c\development projects\hms\01 Development\01 Configuration\01 Checkstyle\checkstyle-javasg.xml
3. Modify checkstyle-java.xml with your custom settings
In this example, i will modify checkstyle’s limit for comment line width (which is by default set to 80 in sun_checks.xml).
Open checkstyle-java.xml
Find the linelength property.
<module name="LineLength"/>
Modify it to the following:
<module name="LineLength"> <property name="max" value="140"/> </module>
Save and close the file.
4. Point the Checkstyle Eclipse plugin to the new file
Go to Windows > Preference
Search for “Checkstyle“, select the “New” option.
At the same time, you would have noticed that by default, checkstyle uses the sun_checks.xml configuration.
Select the “External Configuration File” option, give it a name, i gave mine “checkstyle-javasg” but you could change this out. Browse to the configuration file you saved in the step above. Select Ok.
Select the configuration you have just configured and set it as default. Select the ok option to finish.

5. Run style check
Checkstyle new configuration file took effect. :) Now go back to that line and change it to under 140!











