D
Don
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);
}
}
}
}
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);
}
}
}
}