ant-user mailing list archives

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

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.
filename.

Define the following methods:
    createGunzip()
    getGunzip()   // may not be necessary
    createUntar()
    getUntar()   // may not be necessary
    execute();

The createXXX() methods will create and return an instance of the requested
task.
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.

/***************************************************************/
package org.apache.tools.ant.taskdefs.optional;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.GUnzip;
import org.apache.tools.ant.taskdefs.Untar;
import org.apache.tools.ant.taskdefs.MatchingTask;


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
    {
        checkParameters();
        /*
        set appropriate options on gunzip and untar task objects
        for each file specified ("fileset" instance variable)...
            gunzip.execute();
            untar.execute();

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

    private void checkParameters() throws BuildException
    {
        /*

        YOUR ATTRIBUTE VALIDATION HERE....

        if ( javac==null) {
            throw new BuildException("javac element is required.",
location);
        }
        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,

David


----- Original Message -----
From: "David Corbin" <david.corbin@machturtle.com>
To: <ant-user@jakarta.apache.org>
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.
> http://www.machturtle.com
> dcorbin@machturtle.com
>


Mime
View raw message