Development and Build System with ANT - Checking Code Conventions with Checkstyle
(Page 9 of 11 )
Even if you’re using formatting tools such as Jalopy there are still style checks beyond the realm of formatting. Checkstyle is a tool that enables code to be checked against a convention. Like Jalopy it supports the Sun convention by default, although it can check for more than just simple formatting. For example, it can check for illegal regular expressions in the code, inline conditionals, double-checked locking, and other idioms or patterns that might be considered unsafe or problematic.
You can download Checkstyle fromhttp://checkstyle.sourceforge.net.On the root of the checkstyle distribution you’ll find the checkstyle-all-3.1.jar file. Place this file in a directory named checkstyle under the lib/development/ of the TCMS project directory. The file containing the XML configuration representing the Sun convention is named sun_checks.xml and it’s located under the docs directory of the distribution directory. Copy this file to the lib/development/checkstyle directory also.
Checkstyle writes its output to the standard out by default or to a file in plain text or XML format. The checkstyle distribution also provides several XSL stylesheets that can be used to convert the XML reports to HTML format for easier viewing. You can find these stylesheets in the checkstyle distribution under the contrib directory. Copy the checkstyle-noframes-sorted.xsl file to the lib/development/ checkstyle directory.
To use Checkstyle from within Ant you first need to load the Checkstyle task. You also should define some properties and an Ant path to simplify the loading and execution of the task. You define properties for the location of the Checkstyle JARs and the generated reports. You also define the name of the generated XML report and the resulting HTML report.
<property name="checkstyle-reports" location="${docs}/checkstyle" />
<property name="checkstyle-lib" location="${lib-dev}/checkstyle" />
<property
name="checkstyle-xml-report-file"
location="${checkstyle-reports}/checkstyle-report.xml"
/>
<property
name="checkstyle-html-report-file"
location="${checkstyle-reports}/checkstyle-report.html"
/>
<property
name="checkstyle-checks-file"
location="${checkstyle-lib}/sun_checks.xml"
/>
<property
name="checkstyle-stylesheet"
location="${checkstyle-lib}/checkstyle-noframes-sorted.xsl"
/>
<path id="checkstyle.class.path">
<path refid="class.path"/>
<fileset dir="${lib-dev}/checkstyle">
<include name="*.jar"/>
</fileset>
</path>
<!-- Checkstyle - checkstyle.sourceforge.net -->
<taskdef
resource="checkstyletask.properties"
classpathref="checkstyle.class.path"
/>
Notice that the taskdef task for the Checkstyle task uses the resource attribute instead of the name or classname combination used for the Jalopy task. The target named checkstyle uses the checkstyle task to check the code under the ${src-java} directory against the conventions specified by the file ${checkstyle-checks-file} and uses a formatter of type XML to generate the report referred to
${checkstyle-xml-report-file}. The failureProperty attribute is the property that’s set if there are any errors encountered during the checking process. You can use this value to determine if any action is to be taken in the case of an error, such as emailing the report.
The second part of the target uses the style task to transform the generated XML into a HTML report. The complete target is shown here:
<!-- ====================================================== -->
<!-- Checks source code for convention violations
-->
<!-- ====================================================== -->
<target
name="checkstyle"
depends="checkstyle-init"
description="Generates Code Convention Violations Report."
>
<checkstyle
config="${checkstyle-checks-file}"
failureProperty="checkstyle.failure"
failOnViolation="false"
>
<formatter type="xml" tofile="${checkstyle-xml-report-file}"/>
<fileset dir="${src-java}">
<patternset refid="non.generated.source.set"/>
</fileset>
</checkstyle>
<style
in="${checkstyle-xml-report-file}"
out="${checkstyle-html-report-file}"
style="${checkstyle-stylesheet}"
/>
</target>
<target name="checkstyle-init">
<mkdir dir="${checkstyle-reports}"/>
</target>
<target name="checkstyle-clean">
<delete dir="${checkstyle-reports}"/>
</target>
A sample checkstyle report is shown in Figure 3-10.

Figure 3-10. Checkstyle HTML report
Next: Generating Source-Code Metrics >>
More Java Articles
More By Apress Publishing
|
This article is taken from chapter three of the book Enterprise Java Development on a Budget, written by Brian Sam-Bodden and Christopher M. Judd (Apress, 2004; ISBN: 1590591259). Check it out at your favorite bookstore today. Buy this book now.
|
|