From b546691d3a2e8cc7f59435683d0c1bf7413c50cd Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 12 Dec 2001 08:48:42 +0000 Subject: [PATCH] Add if/unless attributes to . PR: 1380 This is the first step to remove the fail functionality from input and waitfor. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270126 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ docs/manual/CoreTasks/fail.html | 12 +++++++ src/etc/testcases/taskdefs/fail.xml | 8 +++++ src/main/org/apache/tools/ant/Target.java | 1 + .../org/apache/tools/ant/taskdefs/Exit.java | 34 ++++++++++++++++--- .../apache/tools/ant/taskdefs/FailTest.java | 23 +++++++++++++ 6 files changed, 77 insertions(+), 4 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 21a1c09c2..ac1b61fec 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -121,6 +121,9 @@ Other changes: unless a reference of the same name already exists in the subbuild or inheritall has been set to false. +* no supports builds to fail based on conditions via if and + unless attributes. + Changes from Ant 1.4 to Ant 1.4.1 =========================================== diff --git a/docs/manual/CoreTasks/fail.html b/docs/manual/CoreTasks/fail.html index 8a666efcc..4def70a34 100644 --- a/docs/manual/CoreTasks/fail.html +++ b/docs/manual/CoreTasks/fail.html @@ -24,6 +24,18 @@ or character data nested into the element.

A message giving further information on why the build exited No + + if + Only fail if a property of the given name exists + in the current project + No + + + unless + Only fail if a property of the given name doesn't + exist in the current project + No +

Examples

  <fail/>
diff --git a/src/etc/testcases/taskdefs/fail.xml b/src/etc/testcases/taskdefs/fail.xml index 3f8b8154e..6823cbd16 100644 --- a/src/etc/testcases/taskdefs/fail.xml +++ b/src/etc/testcases/taskdefs/fail.xml @@ -10,4 +10,12 @@ + + + + + + + + diff --git a/src/main/org/apache/tools/ant/Target.java b/src/main/org/apache/tools/ant/Target.java index 407e4cd93..a94a58eb4 100644 --- a/src/main/org/apache/tools/ant/Target.java +++ b/src/main/org/apache/tools/ant/Target.java @@ -233,4 +233,5 @@ public class Target implements TaskContainer { String test = ProjectHelper.replaceProperties(getProject(), unlessCondition); return project.getProperty(test) == null; } + } diff --git a/src/main/org/apache/tools/ant/taskdefs/Exit.java b/src/main/org/apache/tools/ant/taskdefs/Exit.java index f7f94292a..28e3e0261 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Exit.java +++ b/src/main/org/apache/tools/ant/taskdefs/Exit.java @@ -66,16 +66,27 @@ import org.apache.tools.ant.ProjectHelper; */ public class Exit extends Task { private String message; + private String ifCondition, unlessCondition; public void setMessage(String value) { this.message = value; } + public void setIf(String c) { + ifCondition = c; + } + + public void setUnless(String c) { + unlessCondition = c; + } + public void execute() throws BuildException { - if (message != null && message.length() > 0) { - throw new BuildException(message); - } else { - throw new BuildException("No message"); + if (testIfCondition() && testUnlessCondition()) { + if (message != null && message.length() > 0) { + throw new BuildException(message); + } else { + throw new BuildException("No message"); + } } } @@ -87,4 +98,19 @@ public class Exit extends Task { ProjectHelper.replaceProperties(project, msg); } + private boolean testIfCondition() { + if (ifCondition == null || "".equals(ifCondition)) { + return true; + } + + return project.getProperty(ifCondition) != null; + } + + private boolean testUnlessCondition() { + if (unlessCondition == null || "".equals(unlessCondition)) { + return true; + } + return project.getProperty(unlessCondition) == null; + } + } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java b/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java index 63686cc65..59cb2b764 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java @@ -54,10 +54,12 @@ package org.apache.tools.ant.taskdefs; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileTest; /** * @author Nico Seessle + * @author Stefan Bodewig */ public class FailTest extends BuildFileTest { @@ -76,4 +78,25 @@ public class FailTest extends BuildFileTest { public void test2() { expectBuildException("test2", "it is required to fail :-)"); } + + public void testIf() { + try { + executeTarget("testIf"); + } catch (BuildException be) { + fail("foo has not been defined, testIf must not fail"); + } + project.setProperty("foo", ""); + expectBuildException("testIf", "testIf must fail if foo has been set"); + } + + public void testUnless() { + expectBuildException("testUnless", + "testUnless must fail unless foo has been set"); + project.setProperty("foo", ""); + try { + executeTarget("testUnless"); + } catch (BuildException be) { + fail("foo has been defined, testUnless must not fail"); + } + } }