<antcall> needs a way to be called conditionally.
This patch adds two attributes to <antcall>, 'if' and 'unless'. They work
the same way as they do in <target>.
I found this useful when creating a set of targets specifically designed to
be invoked via <antcall>.
Please review, [comment], and apply this patch if acceptable.
Thanks,
Michael
Index: src/main/org/apache/tools/ant/taskdefs/CallTarget.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/CallTarget.java,v
retrieving revision 1.25
diff -u -r1.25 CallTarget.java
--- src/main/org/apache/tools/ant/taskdefs/CallTarget.java 25 Jul 2002
15:21:04 -0000 1.25
+++ src/main/org/apache/tools/ant/taskdefs/CallTarget.java 22 Nov 2002
21:07:52 -0000
@@ -55,6 +55,7 @@
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
/**
@@ -92,6 +93,12 @@
// must match the default value of Ant#inheritRefs
private boolean inheritRefs = false;
+ /** The "if" condition to test on execution. */
+ private String ifCondition = "";
+
+ /** The "unless" condition to test on execution. */
+ private String unlessCondition = "";
+
/**
* If true, pass all properties to the new Ant project.
* Defaults to true.
@@ -110,6 +117,40 @@
}
/**
+ * Sets the "if" condition to test on execution. This is the
+ * name of a property to test for existence - if the property
+ * is not set, the task will not execute. The property goes
+ * through property substitution once before testing, so if
+ * property <code>foo</code> has value <code>bar</code>, setting
+ * the "if" condition to <code>${foo}_x</code> will mean that the
+ * task will only execute if property <code>bar_x</code> is set.
+ *
+ * @param property The property condition to test on execution.
+ * May be <code>null</code>, in which case
+ * no "if" test is performed.
+ */
+ public void setIf(String property) {
+ this.ifCondition = (property == null) ? "" : property;
+ }
+
+ /**
+ * Sets the "unless" condition to test on execution. This is the
+ * name of a property to test for existence - if the property
+ * is set, the task will not execute. The property goes
+ * through property substitution once before testing, so if
+ * property <code>foo</code> has value <code>bar</code>, setting
+ * the "unless" condition to <code>${foo}_x</code> will mean that the
+ * task will only execute if property <code>bar_x</code> isn't set.
+ *
+ * @param property The property condition to test on execution.
+ * May be <code>null</code>, in which case
+ * no "unless" test is performed.
+ */
+ public void setUnless(String property) {
+ this.unlessCondition = (property == null) ? "" : property;
+ }
+
+ /**
* init this task by creating new instance of the ant task and
* configuring it's by calling its own init method.
*/
@@ -127,6 +168,7 @@
* execute
*/
public void execute() throws BuildException {
+ if (testIfCondition() && testUnlessCondition()) {
if (callee == null) {
init();
}
@@ -141,6 +183,15 @@
callee.setInheritAll(inheritAll);
callee.setInheritRefs(inheritRefs);
callee.execute();
+ } else if (!testIfCondition()) {
+ project.log(this, "Skipped because property '"
+ + project.replaceProperties(this.ifCondition)
+ + "' not set.", Project.MSG_VERBOSE);
+ } else {
+ project.log(this, "Skipped because property '"
+ + project.replaceProperties(this.unlessCondition)
+ + "' set.", Project.MSG_VERBOSE);
+ }
}
/**
@@ -198,4 +249,39 @@
}
}
+
+ /**
+ * Tests whether or not the "if" condition is satisfied.
+ *
+ * @return whether or not the "if" condition is satisfied. If no
+ * condition (or an empty condition) has been set,
+ * <code>true</code> is returned.
+ *
+ * @see #setIf(String)
+ */
+ private boolean testIfCondition() {
+ if ("".equals(ifCondition)) {
+ return true;
+ }
+
+ String test = project.replaceProperties(ifCondition);
+ return project.getProperty(test) != null;
+ }
+
+ /**
+ * Tests whether or not the "unless" condition is satisfied.
+ *
+ * @return whether or not the "unless" condition is satisfied. If no
+ * condition (or an empty condition) has been set,
+ * <code>true</code> is returned.
+ *
+ * @see #setUnless(String)
+ */
+ private boolean testUnlessCondition() {
+ if ("".equals(unlessCondition)) {
+ return true;
+ }
+ String test = project.replaceProperties(unlessCondition);
+ return project.getProperty(test) == null;
+ }
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>