CleanImports

Description

Cleans the import lists of a Java source file tree.

The source directory will be recursively scanned for Java source files to clean. The import sections of the Java files are cleaned up and formatted according to the supplied format. Cleaning here means that all unneeded imports are removed. The Java files must be javac-compileable with the supplied classpath. All Java files must be changeable, otherwise the file will be skipped with a warning.

It is possible to refine the set of files that are being compiled/copied. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. With the includes or includesfile attribute you specify the files you want to have included by using patterns. The exclude or excludesfile attribute is used to specify the files you want to have excluded. This is also done with patterns. And finally with the defaultexcludes attribute, you can specify whether you want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

Parameters

Attribute Description Required
srcdir location of the java files. Yes, unless nested <src> elements are present.
includes comma-separated list of patterns of files that must beincluded; all files are included when omitted. No
includesfile the name of a file that contains include patterns. No
excludes comma-separated list of patterns of files that must be excluded; no files (except default excludes) are excluded when omitted. No
excludesfile the name of a file that contains exclude patterns. No
defaultexcludes indicates whether default excludes should be used (yes | no); default excludes are used when omitted. No
classpath the classpath to use. No
classpathref the classpath to use, given as a reference to a PATH defined elsewhere. No
encoding encoding of source files. No
cleanformat The format of the generated import block (see below) No
source Value of the -source command line switch for the javac compiler, legal values are "1.3" and "1.4" - by default, no -source argument will be used at all. No

Parameters specified as nested elements

This task forms an implicit FileSet and supports all attributes of <fileset> (dir becomes srcdir) as well as the nested <include>, <exclude> and <patternset> elements.

<src>, <classpath>

CleanImport's srcdir and classpath attributes are path-like structures and can also be set via nested <src> and <classpath> elements, respectively.

Import formatting specification

The format of the import block can be specified using the nested formatting specification. The nested <cleanformat> element can have any number of <text>, <import> and <collapse> elements. The text of the text attribute of a <text> element is always inserted in the imports block. The <import> element denotes a subset of the imports. This can either be constructed using the package attribute, which should then match the exact package prefix, or through the regexp attribute which should match some part of the imported class name. The default when no package or regexp is given is to gather all imports not matched by any other <import> at the end of the format. If the import set selected by an <import> element is not empty, its optional text attribute text is used followed by the imports. The <collapse> element sets the threshold for subsequent <import>s. If the number of imports from one package within one import sub block is equal to or exceeds the above attribute, all imports will be collapsed into one by using the ‘*’ syntax. By default no collapsing will occur, so above="99999". See the examples for details.

The default format is:

<cleanformat>
    <import/>
</cleanformat>

Import statement replacement

The import statements initially in the file will be removed and the new import statements will be inserted. Because the newly inserted import block can contain comments through the text attributes in the <cleanformat> elements, the <cleanimports> task must also replace comment. Otherwise the comments would be added every time the task is run. The task will remove all "//" style comments between the package statement and the first "/*" comment after the last import statement. So it is not advised to use "/*" comments in the <cleanformat> elements, because it would result in an accumulation of these comments in the source files.

Author

Tom Brus, The Netherlands, tomb@euronet.nl.

Examples

The basic way to invoke cleanimports:

<cleanimports srcdir="${src}" classpath="xyz.jar” />

cleans the imports of all .java files under the ${src} directory. The classpath used for compilation contains xyz.jar.

With some general formatting this would be:

<cleanimports srcdir="${src}" classpath="xyz.jar”>
    <cleanformat>
        <import comment="// java util imports:" package="java.util"/>
        <import comment="// java io   imports:" package="java.io"/>
        <import comment="// java lang imports:" package="java.lang"/>
    </cleanformat>
</cleanimports>

Adapt the format to your liking.

<cleanimports srcdir="${src}"
              includes="mypackage/p1/**,mypackage/p2/**"
              excludes="mypackage/p1/testpackage/**"
              classpath="xyz.jar"
/>

cleans the imports of .java files under the ${src} directory. The classpath used contains xyz.jar. Only files under mypackage/p1 and mypackage/p2 are used. Files in the mypackage/p1/testpackage directory are excluded.

<cleanimports srcdir="${src}:${src2}"
              includes="mypackage/p1/**,mypackage/p2/**"
              excludes="mypackage/p1/testpackage/**"
              classpath="xyz.jar"
/>

is the same as the previous example, with the addition of a second source path, defined by the property src2. This can also be represented using nested <src> elements as follows:

<cleanimports classpath="xyz.jar">
    <src path="${src}"/>
    <src path="${src2}"/>
    <include name="mypackage/p1/**"/>
    <include name="mypackage/p2/**"/>
    <exclude name="mypackage/p1/testpackage/**"/>
</cleanimports>
Here is an example of how a <cleanformat> can be used:
<cleanimports srcdir="${src}" classpath="xyz.jar" />
    <cleanformat>
        <text     comment="// The Imports"/>
        <collapse above="3"/>
        <import   comment="// java util imports:" package="java.util"/>
        <import   comment="// other imports:"/>
        <collapse above="5"/>
        <import   comment="// swing imports:" regexp="\.swing\."/>
    </cleanformat>
</cleanimports>

Copyright © 2002 Tom Brus. All rights Reserved.