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 -->
<taskdef
name="javancss"
classname="javancss.JavancssAntTask"
classpathref="javancss.class.path"
/>
<!-- ====================================================== -->
<!-- Source Code Metrics
-->
<!-- ====================================================== -->
<target
name="metrics"
depends="metrics-init"
description="Generates Code Metrics Reports."
>
<!-- business tier source metrics -->
<javancss
srcdir="${src-java}"
includes="**/*.java"
excludes="**/entities/*Bean.java"
generateReport="true"
outputfile="${javancss-xml-business}"
format="xml"
functionMetrics="false"
/>
<style
in="${javancss-xml-business}"
out="${javancss-html-business}"
style="${javancss-stylesheet}"
/>
/>
<target name="metrics-init">
<mkdir dir="${metrics-reports}"/>
</target>
<target name="metrics-clean">
<delete dir="${metrics-reports}"/>
</target>
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 -->
<taskdef
name="java2html"
classname="de.java2html.anttasks.Java2HtmlTask"
classpathref="java2html.class.path"
/>
<property name="browseable-source" location="${docs}/browseable-source" />
<!-- ===================================================== -->
<!-- Generates browseable source code in HTML format
-->
<!-- ===================================================== -->
<target
name="java2html"
depends="java2html-init"
description="Generates browseable HTML version of the source code."
>
<java2html
srcdir="${src}"
destdir="${browseable-source}"
includes="**/*.java"
outputFormat="html"
tabs="4"
style="eclipse"
showLineNumbers="true"
showFileName="true"
showTableBorder="true"
includeDocumentHeader="true"
includeDocumentFooter="true"
addLineAnchors="true"
lineAnchorPrefix="fff"/>
</target>
<target name="java2html-init">
<mkdir dir="${browseable-source}"/>
</target>
<target name="java2html-clean">
<delete dir="${browseable-source}"/>
</target>
Figure 3-12 shows an example of an HTML page generated by Java2HTML
Cleaning UpThe 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
-->
<!-- ====================================================== -->
<target
name="clean"
depends="compile-clean,...,target N"
description="Cleans all build products"
>
<delete dir="${build}"/>
</target>

Figure 3-12. An HTML page generated by Java2HTML
Next: The all Target >>
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.
|
|