Home arrow Java arrow Page 9 - Development and Build System with ANT
JAVA

Development and Build System with ANT


This article will describe some of the development tools that are available from the Java Open Source community and explore how they can help you create high-quality software in a predictable and reproducible fashion, saving you precious time and money. It 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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 25
September 08, 2005
TABLE OF CONTENTS:
  1. · Development and Build System with ANT
  2. · Continuous Integration
  3. · Automating the Build Process with Apacheís Ant
  4. · Introduction to Ant
  5. · More on Targets
  6. · Properties
  7. · Case Study: Building the Technology Conference Management System with Ant
  8. · JavaDoc Generation
  9. · Checking Code Conventions with Checkstyle
  10. · Generating Source-Code Metrics
  11. · The all Target

print this article
SEARCH DEVARTICLES

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


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials