Selectors and How they Work

Selectors are a mechanism whereby the files that make up a fileset can be selected based on criteria other than filename as provided by the <include> and <exclude> tags.

How to use a Selector

A selector is an element of PatternSet, and appears within it. FileSets contain a default PatternSet, so they can support selectors directly.

Different selectors have different attributes. Some selectors can also contain other selectors, and these are called Selector Containers. There is also a category of selectors that allow user-defined extensions, called Extend Selectors.

[Note] I've altered PatternSet so that it is a Selector, and FileSet so that it is a subclass of PatternSet. This shouldn't affect anyone since the inheritance of DataType is still being followed, but it has the very nice feature that all static selectors can be defined in one place: SelectorContainer. It also makes intuitive sense to me that FileSet is a type of PatternSet, one that is applied to a specific directory. Others may disagree. Once a selector is defined in SelectorContainer, it is available for use in PatternSets, FileSets, AndSelectors, OrSelectors, NotSelectors, and any other selector that inherits from SelectorContainer.

Right now, PatternSet inherits from AndSelector. This was done in the mistaken belief that the includes and excludes could be emulated with <filenameselect> and <not><filenameselect></not>. In fact, the structure is really more like

    <and>
        <or>
            <filenameselect/>    <!-- include -->
            <filenameselect/>    <!-- include -->
        </or>
        <not>
            <filenameselect/>    <!-- exclude -->
            <filenameselect/>    <!-- exclude -->
        </not>
    </and>
    
which isn't quite the same. I'll be creating an includes/excludes selector container for PatternSet to inherit from.

Here is a list of the selectors implemented so far.

Size Selector

The <sizeselect> tag in a PatternSet will put a limit on the files specified by the include tag, so that tags which do not meet the size limits specified by the selector will not end up being selected.

Attribute Description Required
size The size of the file which should be tested for. Yes
units The units that the size is expressed in. When using the standard single letter SI designations, such as "k","M", or "G", multiples of 1000 are used. If you want to use power of 2 units, use the IEC standard: "Ki" for 1024, "Mi" for 1048576, and so on. The default is no units, which means the size attributes expresses the exact number of bytes. No
when Indicates how to interpret the size, whether the files to be selected should be larger, smaller, or equal to that value. Acceptable values for this attribute are:
  • less - select files less than the indicated size
  • more - select files greater than the indicated size
  • equal - select files this exact size
The default is less.
No

Here is an example of how to use the Size Selector:

<fileset dir="${jar.path}">
  <patternset>
    <include name="**/*.jar"/>
    <sizeselect size="4" units="Ki" when="more"/>
  </patternset>
</fileset>

Selects all JAR files that are larger than 4096 bytes.

Date Selector

The <dateselect> tag in a PatternSet will put a limit on the files specified by the include tag, so that tags whose last modified date does not meet the date limits specified by the selector will not end up being selected.

Attribute Description Required
datetime Specifies the date and time to test for using a string of the format MM/DD/YYYY HH:MM AM_or_PM. At least one of the two.
millis The number of milliseconds since 1970 that should be tested for. It is usually much easier to use the datetime attribute. If neither datetime nor millis is specified, then the current date and time is used.
when Indicates how to interpret the date, whether the files to be selected are those whose last modified times should be before, after, or equal to the specified value. Acceptable values for this attribute are:
  • before - select files whose last modified date is before the indicated date
  • after - select files whose last modified date is after the indicated date
  • equal - select files whose last modified date is this exact date
The default is before.
No

Here is an example of how to use the Date Selector:

<fileset dir="${jar.path}" includes="**/*.jar">
    <dateselect datetime="01/01/2001 12:00 AM" when="before"/>
</fileset>

Selects all JAR files which were last modified before midnight January 1, 2001.

Other selectors already written that need documenting: <filenameselect>, <extendselect> (along with details as to how to go about writing your own selectors with it), <and>, <or>, <not>, and <majority>

The selectors are coming!

Since this is just a demonstration right now, there are only a few selectors to look at. However, I hope to have many more within a few weeks. These include:

Programming API

Want to define your own selectors? It's easy!

First, pick the type of selector that you want to define. There are three types, and a recipe for each one follows:

  1. Static Selectors
    These are the selectors used by Ant itself. To implement one of these, here is all you need to do.

  2. Selector Containers
    Got an idea for a new Selector Container? Creating a new one is no problem:

  3. Extend Selectors
    If you want to use your selector with the Extend selector, you can either add it to the selector.properties file, or specify the class name in the selectordef element. Make sure that you don't try to define the selectordef from the extendselect you are trying to use it from, since the elements aren't created until after the attributes are assigned, and that will get you an error.
    To create a new Extend Selector, you have to:

For your convenience, the beginning of a test file is available in the test directory under the selectors directory. Eventually this will have to be integrated into Ant's testing framework, but in the meantime it can provide a quick test to see if things work the way you expect them to.

And that's about all I've got to say about that.