Creating ANT task which invokes target with dynamic properties

Discussion in 'Java' started by Don, Oct 20, 2003.

  1. Don

    Don Guest

    I want to create an Ant task which loops through subdirectories and
    then calls an Ant target – this is easy enough through the
    project.executeTarget method. But I want to pass the subdirectory
    name as a property to the "called" target. This I do through the
    project.setProperty method. Yet the target only has the original
    value of the property and not the modified value. I am assuming that
    this is because once a target and its tasks are loaded, the properties
    are then fixed – immutable.


    So my strategy is to "refresh" the target before my task executes the
    target. I was thinking to create a new target, do a shallow copy of
    info, copy to the project and then execute the new target. Thoughts?
    Other ideas?


    Here is the sample ANT build code:


    <target name="build" >
    <IterateDirectoryTask RootDirectory="."
    ExecuteTarget="TestTarget"/>
    </target>

    <target name="TestTarget" >
    <echo message="Hello from TestTarget" />
    <echo message=" subdirectory = ${subdirectory}" />
    </target>



    And here is the TASK:



    public class IterateDirectoryTask extends Task {
    private String _rootDirectory;
    private String _executeTargetName;


    public void setRootDirectory(String aDirectoryName) {
    _rootDirectory = aDirectoryName;
    }


    public void setExecuteTarget(String aTargetName) {
    _executeTargetName = aTargetName;
    }


    public void execute() throws BuildException {
    if (_executeTargetName == null) {
    throw new BuildException("ExecuteTarget must be
    specified");
    }

    File lRootDirectory = new File(_rootDirectory);
    if (!lRootDirectory.isDirectory()) {
    throw new BuildException("'"+_rootDirectory+"' is
    not a directory");
    }

    File[] lFiles = lRootDirectory.listFiles();
    for (int i=0; i < lFiles.length; i++) {
    if (lFiles.isDirectory()) {
    project.setProperty("subdirectory",
    lFiles.getName());
    Target lTarget =
    (Target)project.getTargets().get(_executeTargetName);
    project.executeTarget(_executeTargetName);
    }
    }
    }
    }
     
    Don, Oct 20, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scout
    Replies:
    0
    Views:
    522
    Scout
    Aug 4, 2003
  2. Victor
    Replies:
    0
    Views:
    8,849
    Victor
    Sep 1, 2004
  3. krabhi
    Replies:
    1
    Views:
    9,323
    Marco Meschieri
    Aug 9, 2006
  4. Mich
    Replies:
    5
    Views:
    540
  5. teggy
    Replies:
    0
    Views:
    822
    teggy
    May 29, 2007
Loading...

Share This Page