Browse Source

Added unless attribute to target.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267763 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
09a656d526
3 changed files with 48 additions and 15 deletions
  1. +17
    -7
      docs/index.html
  2. +7
    -3
      src/main/org/apache/tools/ant/ProjectHelper.java
  3. +24
    -5
      src/main/org/apache/tools/ant/Target.java

+ 17
- 7
docs/index.html View File

@@ -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.</p>
<p>A target gets executed only once. Even when more targets depend on it (see
the previous example).</p>
<p>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 <i>sense</i> this property you should add the <i>if</i> attribute
with the name of the property that the target should react to, for example</p>
<p>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
<i>sense</i> this property you should add the <i>if</i> (or
<i>unless</i>) attribute with the name of the property that the target
should react to, for example</p>
<blockquote>
<pre>&lt;target name=&quot;build-module-A&quot; if=&quot;module-A-present&quot;/&gt;</pre>
<pre>&lt;target name=&quot;build-own-fake-module-A&quot; unless=&quot;module-A-present&quot;/&gt;</pre>
</blockquote>
<p>If no <i>if</i> attribute is present, the target will always be executed.</p>
<p>It is a good practice to place your <a href="#property">property</a> and <a
<p>If no <i>if</i> and no <i>unless</i> attribute is present, the target will
always be executed.</p>
<p>It is a good practice to place your <a
href="#tstamp">tstamp</a> 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".</p>
target to execute.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">unless</td>
<td valign="top">the name of the property that must not be set in order
for this target to execute.</td>
<td align="center" valign="top">No</td>
</tr>
</table>
<h3>Tasks</h3>
<p>A task is a piece of code that can be executed.</p>


+ 7
- 3
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -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(""))


+ 24
- 5
src/main/org/apache/tools/ant/Target.java View File

@@ -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;
}
}

Loading…
Cancel
Save