@@ -71,9 +71,6 @@ public class Ant extends Task {
*/
private String antFile = null;
/** the target to call if any */
private String target = null;
/** the output */
private String output = null;
@@ -98,6 +95,12 @@ public class Ant extends Task {
/** the sets of properties to pass to the new project */
private Vector propertySets = new Vector();
/** the targets to call on the new project */
private Vector targets = new Vector();
/** whether the target attribute was specified **/
private boolean targetAttributeSet = false;
/**
* If true, pass all properties to the new Ant project.
* Defaults to true.
@@ -285,7 +288,7 @@ public class Ant extends Task {
public void execute() throws BuildException {
File savedDir = dir;
String savedAntFile = antFile;
String savedTarget = target ;
Vector locals = new Vector(targets) ;
try {
if (newProject == null) {
reinit();
@@ -317,8 +320,9 @@ public class Ant extends Task {
File file = FileUtils.newFileUtils().resolveFile(dir, antFile);
antFile = file.getAbsolutePath();
log("calling target " + (target != null ? target : "[default]")
+ " in build file " + antFile, Project.MSG_VERBOSE);
log("calling target(s) "
+ ((locals.size() == 0) ? locals.toString() : "[default]")
+ " in build file " + antFile, Project.MSG_VERBOSE);
newProject.setUserProperty("ant.file" , antFile);
String thisAntFile = getProject().getProperty("ant.file");
@@ -348,8 +352,11 @@ public class Ant extends Task {
ex, getLocation());
}
if (target == null) {
target = newProject.getDefaultTarget();
if (locals.size() == 0) {
String defaultTarget = newProject.getDefaultTarget();
if (defaultTarget != null) {
locals.add(defaultTarget);
}
}
if (newProject.getProperty("ant.file")
@@ -358,13 +365,18 @@ public class Ant extends Task {
String owningTargetName = getOwningTarget().getName();
if (owningTargetName.equals(target )) {
if (locals.contains( owningTargetName)) {
throw new BuildException(getTaskName() + " task calling "
+ "its own parent target.");
} else {
Target other =
(Target) getProject().getTargets().get(target);
if (other != null && other.dependsOn(owningTargetName)) {
boolean circular = false;
for (Iterator it = locals.iterator(); !circular && it.hasNext();) {
Target other = (Target)(getProject().getTargets().get(
(String)(it.next())));
circular |= (other != null
&& other.dependsOn(owningTargetName));
}
if (circular) {
throw new BuildException(getTaskName()
+ " task calling a target"
+ " that depends on"
@@ -377,12 +389,20 @@ public class Ant extends Task {
addReferences();
if (target != null && !"".equals(target )) {
if (locals.size() > 0 && !(locals.size() == 1 && locals.get(0) == "" )) {
Throwable t = null;
try {
log("Entering " + antFile + "...", Project.MSG_VERBOSE);
newProject.fireSubBuildStarted();
newProject.executeTarget(target);
String[] nameArray =
(String[])(locals.toArray(new String[locals.size()]));
Hashtable targets = newProject.getTargets();
Vector sortedTargets = newProject.topoSort(nameArray, targets);
sortedTargets.setSize(sortedTargets.indexOf(targets.get(
locals.lastElement())) + 1);
newProject.executeSortedTargets(sortedTargets);
} catch (BuildException ex) {
t = ProjectHelper
.addLocationToBuildException(ex, getLocation());
@@ -410,7 +430,6 @@ public class Ant extends Task {
}
dir = savedDir;
antFile = savedAntFile;
target = savedTarget;
}
}
@@ -601,7 +620,8 @@ public class Ant extends Task {
throw new BuildException("target attribute must not be empty");
}
this.target = s;
targets.add(s);
targetAttributeSet = true;
}
/**
@@ -640,6 +660,23 @@ public class Ant extends Task {
references.addElement(r);
}
/**
* Add a target to this Ant invocation.
* @param target the <CODE>TargetElement</CODE> to add.
* @since Ant 1.7
*/
public void addConfiguredTarget(TargetElement t) {
if (targetAttributeSet) {
throw new BuildException(
"nested target is incompatible with the target attribute");
}
String name = t.getName();
if (name.equals("")) {
throw new BuildException("target name must not be empty");
}
targets.add(name);
}
/**
* Set of properties to pass to the new project.
*
@@ -691,4 +728,34 @@ public class Ant extends Task {
return targetid;
}
}
/**
* Helper class that implements the nested <target>
* element of <ant> and <antcall>.
* @since Ant 1.7
*/
public static class TargetElement {
private String name;
/**
* Default constructor.
*/
public TargetElement() {}
/**
* Set the name of this TargetElement.
* @param name the <CODE>String</CODE> target name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the name of this TargetElement.
* @return <CODE>String</CODE>.
*/
public String getName() {
return name;
}
}
}