Browse Source

Add if/unless attributes to <fail>.

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
master
Stefan Bodewig 23 years ago
parent
commit
b546691d3a
6 changed files with 77 additions and 4 deletions
  1. +3
    -0
      WHATSNEW
  2. +12
    -0
      docs/manual/CoreTasks/fail.html
  3. +8
    -0
      src/etc/testcases/taskdefs/fail.xml
  4. +1
    -0
      src/main/org/apache/tools/ant/Target.java
  5. +30
    -4
      src/main/org/apache/tools/ant/taskdefs/Exit.java
  6. +23
    -0
      src/testcases/org/apache/tools/ant/taskdefs/FailTest.java

+ 3
- 0
WHATSNEW View File

@@ -121,6 +121,9 @@ Other changes:
<antcall> unless a reference of the same name already exists in the
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
===========================================



+ 12
- 0
docs/manual/CoreTasks/fail.html View File

@@ -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 align="center" valign="top">No</td>
</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>
<h3>Examples</h3>
<pre> &lt;fail/&gt;</pre>


+ 8
- 0
src/etc/testcases/taskdefs/fail.xml View File

@@ -10,4 +10,12 @@
<fail message="test"/>
</target>

<target name="testIf">
<fail if="foo" />
</target>

<target name="testUnless">
<fail unless="foo" />
</target>

</project>

+ 1
- 0
src/main/org/apache/tools/ant/Target.java View File

@@ -233,4 +233,5 @@ public class Target implements TaskContainer {
String test = ProjectHelper.replaceProperties(getProject(), unlessCondition);
return project.getProperty(test) == null;
}

}

+ 30
- 4
src/main/org/apache/tools/ant/taskdefs/Exit.java View File

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

}

+ 23
- 0
src/testcases/org/apache/tools/ant/taskdefs/FailTest.java View File

@@ -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 <nico@seessle.de>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
*/
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");
}
}
}

Loading…
Cancel
Save