Java
  Home arrow Java arrow Page 9 - Development and Build System with ANT
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Development and Build System with ANT
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2005-09-08

    Table of Contents:
  • Development and Build System with ANT
  • Continuous Integration
  • Automating the Build Process with Apache’s Ant
  • Introduction to Ant
  • More on Targets
  • Properties
  • Case Study: Building the Technology Conference Management System with Ant
  • JavaDoc Generation
  • Checking Code Conventions with Checkstyle
  • Generating Source-Code Metrics
  • The all Target

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More Java Articles
    More By Apress Publishing


     

    Buy this book now. 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.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT