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-ffa450edef68master
@@ -121,6 +121,9 @@ Other changes: | |||||
<antcall> unless a reference of the same name already exists in the | <antcall> unless a reference of the same name already exists in the | ||||
subbuild or inheritall has been set to false. | subbuild or inheritall has been set to false. | ||||
* <fail> no supports builds to fail based on conditions via if and | |||||
unless attributes. | |||||
Changes from Ant 1.4 to Ant 1.4.1 | Changes from Ant 1.4 to Ant 1.4.1 | ||||
=========================================== | =========================================== | ||||
@@ -24,6 +24,18 @@ or character data nested into the element.</p> | |||||
<td valign="top">A message giving further information on why the build exited</td> | <td valign="top">A message giving further information on why the build exited</td> | ||||
<td align="center" valign="top">No</td> | <td align="center" valign="top">No</td> | ||||
</tr> | </tr> | ||||
<tr> | |||||
<td valign="top">if</td> | |||||
<td valign="top">Only fail if a property of the given name exists | |||||
in the current project</td> | |||||
<td align="center" valign="top">No</td> | |||||
</tr> | |||||
<tr> | |||||
<td valign="top">unless</td> | |||||
<td valign="top">Only fail if a property of the given name doesn't | |||||
exist in the current project</td> | |||||
<td align="center" valign="top">No</td> | |||||
</tr> | |||||
</table> | </table> | ||||
<h3>Examples</h3> | <h3>Examples</h3> | ||||
<pre> <fail/></pre> | <pre> <fail/></pre> | ||||
@@ -10,4 +10,12 @@ | |||||
<fail message="test"/> | <fail message="test"/> | ||||
</target> | </target> | ||||
<target name="testIf"> | |||||
<fail if="foo" /> | |||||
</target> | |||||
<target name="testUnless"> | |||||
<fail unless="foo" /> | |||||
</target> | |||||
</project> | </project> |
@@ -233,4 +233,5 @@ public class Target implements TaskContainer { | |||||
String test = ProjectHelper.replaceProperties(getProject(), unlessCondition); | String test = ProjectHelper.replaceProperties(getProject(), unlessCondition); | ||||
return project.getProperty(test) == null; | return project.getProperty(test) == null; | ||||
} | } | ||||
} | } |
@@ -66,16 +66,27 @@ import org.apache.tools.ant.ProjectHelper; | |||||
*/ | */ | ||||
public class Exit extends Task { | public class Exit extends Task { | ||||
private String message; | private String message; | ||||
private String ifCondition, unlessCondition; | |||||
public void setMessage(String value) { | public void setMessage(String value) { | ||||
this.message = value; | this.message = value; | ||||
} | } | ||||
public void setIf(String c) { | |||||
ifCondition = c; | |||||
} | |||||
public void setUnless(String c) { | |||||
unlessCondition = c; | |||||
} | |||||
public void execute() throws BuildException { | 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); | 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; | |||||
} | |||||
} | } |
@@ -54,10 +54,12 @@ | |||||
package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.BuildFileTest; | import org.apache.tools.ant.BuildFileTest; | ||||
/** | /** | ||||
* @author Nico Seessle <nico@seessle.de> | * @author Nico Seessle <nico@seessle.de> | ||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | */ | ||||
public class FailTest extends BuildFileTest { | public class FailTest extends BuildFileTest { | ||||
@@ -76,4 +78,25 @@ public class FailTest extends BuildFileTest { | |||||
public void test2() { | public void test2() { | ||||
expectBuildException("test2", "it is required to fail :-)"); | 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"); | |||||
} | |||||
} | |||||
} | } |