Creating New Types
When creating new tasks, it's sometimes useful to create
new datatypes to be used by those tasks. In this example, I'll create a
new datatype that extends the Ant FileList type and interfaces easily to
attributes in a custom task.
This example centers around a new data type, ProjectType, which extends the Ant FileList class. To use that data type, I'll
develop a new custom task, projectTask, that supports nested elements
named multiFile, each of which
supports an attribute named files.
You can set the files attribute to a
string containing multiple file-names such as "ed.txt george.txt", and
the code will create an object of a custom data type, ProjectType, to contain that list of
files.
Example 12-22 holds
the code for the new datatype, ProjectType, which extends the org.apache.tools.ant.types.FileList class. In
this example, the code simply passes the list of files on back to the
FileSet base class, but you can adapt
this code to handle the list of files any way you want.
Example 12-22. The new data type (ch12/type/src/ProjectType.java)
package data;
import org.apache.tools.ant.types.FileList;
public class ProjectType extends FileList
{
public void setFiles(String files)
{
super.setFiles(files);
}
}The build file for the custom task appears in Example 12-23; the nested multiFile element's files attribute takes the list of files that
will be stored in internal filelist of the object of the custom data
type.
Example 12-23. Build file for the datatype example (ch12/type/build.xml) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access