Home arrow Java arrow Page 10 - Development and Build System with ANT

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
  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

Development and Build System with ANT - Generating Source-Code Metrics
(Page 10 of 11 )

Although we don’t advocate counting code lines, classes, or methods as a measure of a project’s success, static code analysis can help you pinpoint some areas of unnecessary complexity that can lead to the discovery of potential bugs or high-maintenance code.

JavaNCSS is a simple source-measurement tool for Java that provides the following basic types of analysis:


  • NCSS: Noncommenting source statements provide counts of many features of the code such as lines of code, declarations, methods, statements, constructors, and so on.


  • CCN: Cyclomatic complexity number (McCabe metric). McCabe’s cyclomatic complexity metric looks at a program’s control flow graph as a measure of its complexity.


You can download JavaNCSS fromhttp://www.kclee.com/clemens/java/javancss/as a simple ZIP file that includes an Ant task. Place all JAR files located under the distribution’s lib directory in a directory named javancss under the lib/development directory of the TCMS project. Next, create a directory named xslt under the lib/javancss and copy the contents of the xslt directory under the JavaNCSS distribution directory.

The Ant task can generate a report in plain text of the XML format. As with the Checkstyle target, you’ll use the style task to transform the reports to HTML, as shown here:

<!-- Javancss - kclee.com/clemens/java/javancss -->
<!-- ====================================================== -->
<!-- Source Code Metrics
<!-- ====================================================== -->
description="Generates Code Metrics Reports."
    <!-- business tier source metrics -->

<target name="metrics-init">
    <mkdir dir="${metrics-reports}"/>
<target name="metrics-clean">
    <delete dir="${metrics-reports}"/>

The generated HTML reports look like the one shown in Figure 3-11.

Figure 3-11.  A JavaNCSS HTML report

Generating Browseable Source Code

One useful feature for sharing knowledge about a project is the ability to generate a browseable version of the code for viewing online. Many Open Source projects use this as a way to allow others to view the source to a particular class without having to download a source distribution or having to use CVS. Java2Html is a tool that enables you to take a Java class or a snippet of Java code and generate a syntax-highlighted HTML version of the code.

The Java2Html tool can be obtained fromhttp://www.java2html.deas a single ZIP file that contains one JAR file (java2html.jar). As with the other third-party Ant tasks, place the JAR file in a directory named java2html under the TCMS lib directory.

As mentioned previously, you should load the task using the taskdef task. The generated HTML source will be placed under the location pointed to by the property ${browseable-source}, as shown here:

<!-- Java2Html - java2html.de -->
<property name="browseable-source" location="${docs}/browseable-source" />
<!-- ===================================================== -->
<!-- Generates browseable source code in HTML format
<!-- ===================================================== -->
    description="Generates browseable HTML version of the source code."
<target name="java2html-init">
<mkdir dir="${browseable-source}"/>
<target name="java2html-clean">
<delete dir="${browseable-source}"/>

Figure 3-12 shows an example of an HTML page generated by Java2HTML

Cleaning Up

The build process produces many files and directories. Getting the project directory to the same state as when the source was checked out of a repository is important for determining what has changed. Many Ant users recommend having a “clean” target that can remove all the products of the build process.

The problem with this approach is that for large builds it’s easy to accidentally delete files that are needed and it’s also easy to miss files or directories that need to be deleted. For this reason you should include a clean subtarget for each main target in the buildfile. By doing this you’ll easily be able to determine what needs to be clean at the target level. Then for the global clean target you can simply invoke all individual clean subtargets by adding them to its list of dependencies as shown here:

<!-- ====================================================== -->
<!-- Cleans everything
<!-- ====================================================== -->
depends="compile-clean,...,target N"
description="Cleans all build products"
<delete dir="${build}"/>

Figure 3-12.  An HTML page generated by Java2HTML

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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