Post tutorial RSS Java, Ant and Checkstyle

This is the first installment of my Java, Ant and QA tutorials. They continue where my Blog started.

Posted by on - Basic QA/Testing

This is the first installment of my Java, Ant and QA tutorials. They continue where my Blog (Indiedb.com) started. It covers setting up Checkstyle in Java Ant. By setting up Checkstyle in Java Ant, you can make sure it is tested each time you run your Ant script.

Personally, I'm using the jMonkeyEngine SDK. As such, I have a few predefined Ant target I can extend. I have put all my QA in the Ant target "test-report". This way, the QA is run each time I run a full project test.

Checkstyle

Checkstyle is a QA tool which helps in enforcing a coding standard. It does this by checking your entire source code against a predefined coding standard. This coding standard is defined in an XML based configuration file. I won't go into the details of how to setup your coding style in the XML document. This tutorial is intended to show you in a few easy steps how to setup the Checkstyle Ant tasks to enable checking your coding standard through Ant. To find out more about how to setup your Checkstyle coding standard configuration, please go to Checkstyle.sourceforge.net. The Checkstyle download also comes with a predefined configuration file which follows the SUN coding standard. This is a good place to start with your own coding standard.

Prerequisites

Before you can start setting up the Checkstyle Ant task, first you need to download Checkstyle. This can be done from their website: Sourceforge.net. This archive contains a few different things, but for this tutorial we are only interested in the main jar library, checkstyle-5.6-all.jar and the default sun configuration file, sun_checks.xml.
The checkstyle-5.6-all.jar goes into the lib folder. The sun_checks.xml goes into your main project folder.
Now you're all set to start incorporating Checkstyle into your Ant build script.

Ant

Now that the prerequisites are taken care of, it is time to start setting up the Ant build script. To make this happen, the first thing that needs to be done is setup the taskdef. The taskdef declaration sets up the Checkstyle task to be used elsewhere in the Ant build script. The taskdef is done via the following Ant xml tag:

<taskdef resource="checkstyletask.properties" classpath="${basedir}/lib/checkstyle-5.6-all.jar"/>

In the above piece of XML there are two pieces of information which are of interest. The classpath, this is a reference to the Checkstyle jar file we downloaded earlier. There is also the resource. This properties file is part of the Checkstyle jar file and should work as it is.

The next step in setting up Checkstyle in Ant is to add the actual Checkstyle task to the an Ant target. As mentioned earlier, I am using the jMonkeyEngine SDK. The following description therefor is a bit tailored to that IDE. With a few minor modifications it should work just as well in any Ant build script though.
To add the Checkstyle task to the Ant build script, you need to add the following piece of XML to one of the Ant targets:


<checkstyle config="${basedir}/sun_checks.xml" failonviolation="false">
            <fileset dir="${src.src.dir}" includes="**/*.java"/>
            <formatter type="plain" />
            <formatter type="plain" toFile="${build.test.results.dir}/checkstyle_errors.txt" />
        </checkstyle>

In this piece of XML several things happen. First, there is the overal "checkstyle" tag. This is the actual Checkstyle task. It needs a reference to the checkstyle coding standard configuration. In this case the default sun coding standard configuration which comes with the Checkstyle download. Next is tells Ant not to fail the build when the coding standard is violated.
Inside the main Checkstyle task, there are three other XML tags, a "fileset" tag and two "formatter" tags. The "fileset" tag tells the Checkstyle task which files need to be checked against the coding standard. The two formatter tags tell the Checkstyle task how to format the output of the Checkstyle task. In this case, there are two formatters. The first just outputs everything straight into the Ant console in plain format. The second outputs everything into a textfile in the plain format.

Output

After all this work, you are off course interested in a preview of the output. Well, that's easily done. Here's an example of the output I get when I put a few violations into my codebase.

Example wrote:
Running Checkstyle 5.6 on 84 files/home/ractoc/projects/fs-invasion/src/com/ractoc/fs/invasion/GameClient.java:65:5: Missing a Javadoc comment./home/ractoc/projects/fs-invasion/src/com/ractoc/fs/invasion/GameClient.java:78: Comment matches to-do format 'TODO:'.

Conclusion

That's really all there is to it. As it stands, the Checkstyle QA test is one of the easier ones. In the next tutorial, I will delve into the combined jUnit and Cobertura Ant tasks and how they help to make sure every bit of code is tested as good as possible.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.