ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Dabbs" <>
Subject Re: Parameterized "task-function"
Date Thu, 11 Jan 2001 04:17:41 GMT

Here's one possibility. I've successfuly employed this for 'aggregating'
together several tasks I wanted to 'drive' with a fixed list of files loaded
from a Manifest file.

Create a new, custom task called <TGZ>. It should extend MatchingTask and
accept whatever common attributes you're interested in passing, e.g.

Define the following methods:
    getGunzip()   // may not be necessary
    getUntar()   // may not be necessary

The createXXX() methods will create and return an instance of the requested
They should also use the requested task object's setXX() method(s) to set
common parameters such as filename, destination, etc.

Execute will call, in sequence, unzip.execute() and untar.execute();

To use the aggregate task without having to retype the tags verytime you
with to use it, simply create a dummy target and id the <TGZ> tag with a
reference id. Any target wishing to <tgz> will simply use

    <tgz refid="foo" option="bar" option="baz" />

Here's a sample. Note I just banged this out and did not try to compile it.



public class TGZ extends MatchingTask {

    private GUnzip gunzip;
    private Untar untar;

    public GUnzip createGunzip() {
        if (gunzip == null) {
            gunzip = (GUnzip) project.createTask("gunzip");  //XX
        } else {
            //XX error if more than one gunzip encountered
        return gunzip;

    public Untar createUntar() {
        if (untar == null) {
            untar = (untar) project.createTask("untar");  //XX
        } else {
            //XX error if more than one untar encountered
        return untar;

    setXXX methods for attributes this task (TGZ) defines. setFile( File
file), for example

    public void execute() throws BuildException
        set appropriate options on gunzip and untar task objects
        for each file specified ("fileset" instance variable)...

        See MatchingTask-derived clases such as or others for
        examples of how to scan filesets for pattern matched files.

    private void checkParameters() throws BuildException


        if ( javac==null) {
            throw new BuildException("javac element is required.",
        if ( jspc != null ) {
            //log("JSP precompilation enabled.", Project.MSG_INFO);

    * Returns whether the given String parameter has been defined.
    private boolean checkParam(String param)
        return !((param == null) || (param.length()==0) ||
(param.equals("null")) ) ;

This is a lot of hassle to simply avoid copying two task definitions, but
with more complex "functions" it might be worth it I suppose.

Hope this helps,


----- Original Message -----
From: "David Corbin" <>
To: <>
Sent: Wednesday, January 10, 2001 6:47 AM
Subject: Re: Parameterized "task-function"

> > David Dabbs wrote:
> >
> > > Has any thought been given to being able to construct a task from
> > > several tasks, and have it parameterized?  I realize this can be
> > cobbled
> > > together with targets and properties, but we all know that's a lousy
> >
> > > solution, don't we?
> >
> > Do you have a use case? I'm not sure what you're looking for. I could
> > see a generic task that, given a list of refIds, could execute() them
> > in series assuming they've already been defined. Not sure if that
> > would buy you much ovcer the ugly way.
> >
> > David Dabbs
> A) I have dozens of .tgz files I want to extract.  To do this, I have to
> specify two tasks <gunzip> and <untar>.  I'd like to make one task that
> incorporates both.
> B) eventually, I hope ANT will support an include.  When it does, I'll
> want to be able to "pass arguments" for standard actions.
> (That's just starters).  As was pointed out to me, ANT is a scripting
> language.  I want function/procedures.   Is that so odd?
> --
> David Corbin
> Mach Turtle Technologies, Inc.

View raw message