Classpath Inspector:
cpcheck Ant Task

Description

Classpath Inspector inspects a classpath, looking for duplicate class files and for incompatible class files. The tool examines each entry in the classpath, traversing the directory tree or the archive structure looking for class files. The tool reports the location of the effective class and the location of the classes that are overridden by the effective class, whether or not the overridden class is identical to the effective class. The tool is also available as a command-line application.

Classes are considered to be duplicates if they have the same fully-qualified class name. The size of each class file is reported, but no attempt is made to determine if the classes have the same bytecode.

The task can be configured to produce a report, either to the console or to a file. The report can be generated in a standard format, either plain (ASCII text) or XML, or in a custom format.

The task can be configured to fail the build if duplicate classes are encountered; by default, a warning message is produced but the build continues. The build will fail only after any reports have been generated.

Parameters

Attribute Description Required
title The title for any reports No.
pattern Inspect only classes matching this regular expression. If omitted, all classes are inspected. No.
failonduplicate fail the build if any duplicate class files are found (defaults to false). No.

Nested Elements

classpath

The cpcheck task requires exactly one nested <classpath> element to specify the classpath to be checked for duplicate classes.

formatter

The version reports can be printed in different formats. Output is sent to a file, whose name is set by the file attribute of the <formatter>. One cpcheck task can support any number of formatters. If there are no formatters, then no report is produced.

There are two predefined formatters—one prints the results in XML format, the other emits plain text. The formatter named plain prints the results as ASCII text, while the formatter named xml writes an XML representation. The report is sent to standard output, unless a file is specified. Custom formatters, which must implement com.bennettconsulting.cpcheck.ResultFormatter, can be specified.

Attribute Description Required
type Use a predefined formatter (either xml or plain). Exactly one of these.
classname Name of a custom formatter class.
duplicatesonly Should the report list all class files (false) or only class files with duplicates (true). Defaults to false. No.
file Name of file to write output to. No; predefined formatters default to standard output.

Examples

<taskdef resource="cpchecktask.properties" classpath="cpcheck.jar"/>

Establishes the cpcheck task.

<cpcheck title="Duplicate Classes in the Classpath of My App">
    <formatter type="xml" duplicatesonly="true" file="report.xml"/>
    <classpath id="java.classpath"/>
</cpcheck>

Checks the classpath named java.classpath for duplicate class files, and writes an XML-formatted report to a file named report.xml with a rather long title.

<cpcheck pattern="com\.doomedstartup\.product\..*" failonduplicate="true">
    <classpath path="build/classes"/>
</cpcheck>

Checks the classpath at build/classes for any classes under the package com.doomedstartup.product, and fails the build if any duplicate class files are found. No report is produced.

License

This tool is distributed under the Apache 2.0 License. The license is also available on the web.