diff --git a/docs/index.html b/docs/index.html index 17d7e07f9..0534af4fa 100644 --- a/docs/index.html +++ b/docs/index.html @@ -274,16 +274,20 @@ might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.

A target gets executed only once. Even when more targets depend on it (see the previous example).

-

A target has also the ability to perform its execution if a property has been -set. This allows, for example, better control on the building process depending -on the state of the system (java version, OS, command line properties, etc...). -To make target sense this property you should add the if attribute -with the name of the property that the target should react to, for example

+

A target has also the ability to perform its execution if (or +unless) a property has been set. This allows, for example, better +control on the building process depending on the state of the system +(java version, OS, command line properties, etc...). To make target +sense this property you should add the if (or +unless) attribute with the name of the property that the target +should react to, for example

<target name="build-module-A" if="module-A-present"/>
+
<target name="build-own-fake-module-A" unless="module-A-present"/>
-

If no if attribute is present, the target will always be executed.

-

It is a good practice to place your property and If no if and no unless attribute is present, the target will +always be executed.

+

It is a good practice to place your tstamp tasks in a so called initialization target, on which all other targets depend. Make sure that that target is always the first one in the depends list of the other targets. In this manual, most initialization targets @@ -312,6 +316,12 @@ have the name "init".

target to execute. No + + unless + the name of the property that must not be set in order + for this target to execute. + No +

Tasks

A task is a piece of code that can be executed.

diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 8a82977d3..60dcb8d8d 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -276,7 +276,8 @@ public class ProjectHelper { public void init(String tag, AttributeList attrs) throws SAXParseException { String name = null; String depends = ""; - String cond = null; + String ifCond = null; + String unlessCond = null; String id = null; for (int i = 0; i < attrs.getLength(); i++) { @@ -288,7 +289,9 @@ public class ProjectHelper { } else if (key.equals("depends")) { depends = value; } else if (key.equals("if")) { - cond = value; + ifCond = value; + } else if (key.equals("unless")) { + unlessCond = value; } else if (key.equals("id")) { id = value; } else { @@ -302,7 +305,8 @@ public class ProjectHelper { target = new Target(); target.setName(name); - target.setCondition(cond); + target.setIf(ifCond); + target.setUnless(unlessCond); project.addTarget(name, target); if (id != null && !id.equals("")) diff --git a/src/main/org/apache/tools/ant/Target.java b/src/main/org/apache/tools/ant/Target.java index a3ae35c0f..3ef9078ed 100644 --- a/src/main/org/apache/tools/ant/Target.java +++ b/src/main/org/apache/tools/ant/Target.java @@ -65,7 +65,8 @@ import java.util.*; public class Target { private String name; - private String condition = ""; + private String ifCondition = ""; + private String unlessCondition = ""; private Vector dependencies = new Vector(2); private Vector tasks = new Vector(5); private Project project; @@ -108,8 +109,12 @@ public class Target { return dependencies.elements(); } - public void setCondition(String property) { - this.condition = (property == null) ? "" : property; + public void setIf(String property) { + this.ifCondition = (property == null) ? "" : property; + } + + public void setUnless(String property) { + this.unlessCondition = (property == null) ? "" : property; } public String toString() { @@ -117,7 +122,7 @@ public class Target { } public void execute() throws BuildException { - if (("".equals(this.condition)) || (project.getProperty(this.condition) != null)) { + if (testIfCondition() && testUnlessCondition()) { Enumeration enum = tasks.elements(); while (enum.hasMoreElements()) { Task task = (Task) enum.nextElement(); @@ -135,8 +140,22 @@ public class Target { throw exc; } } + } else if (!testIfCondition()) { + project.log(this, "Skipped because property '" + this.ifCondition + "' not set.", + Project.MSG_VERBOSE); } else { - project.log(this, "Skipped because property '" + this.condition + "' not set.", Project.MSG_VERBOSE); + project.log(this, "Skipped because property '" + this.unlessCondition + "' set.", + Project.MSG_VERBOSE); } } + + private boolean testIfCondition() { + return "".equals(ifCondition) + || project.getProperty(ifCondition) != null; + } + + private boolean testUnlessCondition() { + return "".equals(unlessCondition) + || project.getProperty(unlessCondition) == null; + } }