mbenson 2004/06/21 15:15:05 Modified: . Tag: ANT_16_BRANCH TODO WHATSNEW docs/manual/CoreTasks Tag: ANT_16_BRANCH fail.html src/etc/testcases/taskdefs Tag: ANT_16_BRANCH fail.xml src/main/org/apache/tools/ant Tag: ANT_16_BRANCH Main.java src/main/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH Execute.java Exit.java src/testcases/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH FailTest.java Added: src/main/org/apache/tools/ant Tag: ANT_16_BRANCH ExitStatusException.java Log: Merge <fail status="" ...> and <fail> nested <condition>. Revision Changes Path No revision No revision 1.3.2.17 +0 -2 ant/Attic/TODO Index: TODO =================================================================== RCS file: /home/cvs/ant/Attic/TODO,v retrieving revision 1.3.2.16 retrieving revision 1.3.2.17 diff -u -r1.3.2.16 -r1.3.2.17 --- TODO 21 Jun 2004 22:00:11 -0000 1.3.2.16 +++ TODO 21 Jun 2004 22:15:05 -0000 1.3.2.17 @@ -11,8 +11,6 @@ * merge "<loadproperties> from resource" from HEAD [Matt] -* merge "<fail> nested <condition> " from HEAD [Matt] - * merge "<apply> differentiates between empty & up-to-date" from HEAD [Matt] * Fix or at least document support for tomcat 5.0 jsp 1.503.2.107 +5 -0 ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/ant/WHATSNEW,v retrieving revision 1.503.2.106 retrieving revision 1.503.2.107 diff -u -r1.503.2.106 -r1.503.2.107 --- WHATSNEW 14 Jun 2004 12:24:17 -0000 1.503.2.106 +++ WHATSNEW 21 Jun 2004 22:15:05 -0000 1.503.2.107 @@ -196,6 +196,11 @@ * Added Target.getIf/Unless(). Bugzilla Report 29320. +* <fail> has a status attribute that can be used to pass an exit + status back to the command line. + +* <fail> accepts a nested <condition>. + Changes from Ant 1.6.0 to Ant 1.6.1 ============================================= No revision No revision 1.8.2.3 +49 -3 ant/docs/manual/CoreTasks/fail.html Index: fail.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTasks/fail.html,v retrieving revision 1.8.2.2 retrieving revision 1.8.2.3 diff -u -r1.8.2.2 -r1.8.2.3 --- fail.html 9 Feb 2004 22:12:07 -0000 1.8.2.2 +++ fail.html 21 Jun 2004 22:15:05 -0000 1.8.2.3 @@ -3,7 +3,6 @@ <head> <meta http-equiv="Content-Language" content="en-us"> <title>Fail Task</title> -<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css"> </head> <body> @@ -37,7 +36,24 @@ exist in the current project</td> <td align="center" valign="top">No</td> </tr> + <tr> + <td valign="top">status</td> + <td valign="top">Exit using the specified status code; + assuming the generated Exception is not caught, the + JVM will exit with this status. <em>Since Ant 1.7</em></td> + <td align="center" valign="top">No</td> + </tr> </table> + +<h3>Parameters specified as nested elements</h3> + +<p>As an alternative to the <i>if</i>/<i>unless</i> attributes, + conditional failure can be achieved using a single nested + <condition> element, which should contain exactly one + core or custom condition. For information about conditions, see + <a href="conditions.html">here</a>.<br /><b>Since Ant 1.6.2</b> +</p> + <h3>Examples</h3> <pre> <fail/></pre> <p>will exit the current build with no further information given.</p> @@ -48,8 +64,9 @@ </pre> <pre> <fail message="Something wrong here."/></pre> -<p>will exit the current build and print something like the following to wherever -your output goes:</p> +<p>will exit the current build and print something + like the following to wherever your output goes: +</p> <pre> BUILD FAILED @@ -58,6 +75,35 @@ <pre> <fail>Something wrong here.</fail></pre> <p>will give the same result as above.</p> + +<pre> <fail unless="thisdoesnotexist"/></pre> +<p>will exit the current build and print something + like the following to wherever your output goes: +</p> +<pre> +BUILD FAILED + +build.xml:2: unless=thisdoesnotexist +</pre> + +Using a condition to achieve the same effect: + +<pre> + <fail> + <condition> + <not> + <isset property="thisdoesnotexist"/> + </not> + </condition> + </fail> +</pre> + +<p>Output:</p> +<pre> +BUILD FAILED + +build.xml:2: condition satisfied +</pre> <hr> <p align="center">Copyright © 2000-2001,2004 The Apache Software Foundation. All rights No revision No revision 1.5.2.1 +85 -0 ant/src/etc/testcases/taskdefs/fail.xml Index: fail.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/fail.xml,v retrieving revision 1.5 retrieving revision 1.5.2.1 diff -u -r1.5 -r1.5.2.1 --- fail.xml 6 Sep 2003 07:02:31 -0000 1.5 +++ fail.xml 21 Jun 2004 22:15:05 -0000 1.5.2.1 @@ -25,5 +25,90 @@ <target name="testIfAndUnless"> <fail unless="unless" if="if"/> </target> + + <target name="testNested1" description="should fail with default message"> + <fail> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested2" description="should pass"> + <fail> + <condition> + <or /> + </condition> + </fail> + </target> + + <target name="testNested3" description="should fail"> + <fail message="testNested3"> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested4a" description="should error"> + <fail if="if"> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested4b" description="should error"> + <fail unless="unless"> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested4c" description="should error"> + <fail if="if" unless="unless"> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested5" description="should error"> + <fail> + <condition> + <or /> + </condition> + <condition> + <and /> + </condition> + </fail> + </target> + + <target name="testNested6" description="should fail with message"> + <fail> + <condition> + <and /> + </condition> +testNested6 +testNested6 +testNested6 + </fail> + </target> + + <target name="testNested7a" description="should error"> + <fail> + <condition /> + </fail> + </target> + + <target name="testNested7b" description="should error"> + <fail> + <condition> + <and /> + <and /> + </condition> + </fail> + </target> </project> No revision No revision 1.95.2.8 +9 -2 ant/src/main/org/apache/tools/ant/Main.java Index: Main.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Main.java,v retrieving revision 1.95.2.7 retrieving revision 1.95.2.8 diff -u -r1.95.2.7 -r1.95.2.8 --- Main.java 23 Apr 2004 15:47:31 -0000 1.95.2.7 +++ Main.java 21 Jun 2004 22:15:05 -0000 1.95.2.8 @@ -184,8 +184,15 @@ // expect the worst int exitCode = 1; try { - runBuild(coreLoader); - exitCode = 0; + try { + runBuild(coreLoader); + exitCode = 0; + } catch (ExitStatusException ese) { + exitCode = ese.getStatus(); + if (exitCode != 0) { + throw ese; + } + } } catch (BuildException be) { if (err != System.err) { printMessage(be); No revision Index: Main.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Main.java,v retrieving revision 1.95.2.7 retrieving revision 1.95.2.8 diff -u -r1.95.2.7 -r1.95.2.8 --- Main.java 23 Apr 2004 15:47:31 -0000 1.95.2.7 +++ Main.java 21 Jun 2004 22:15:05 -0000 1.95.2.8 @@ -184,8 +184,15 @@ // expect the worst int exitCode = 1; try { - runBuild(coreLoader); - exitCode = 0; + try { + runBuild(coreLoader); + exitCode = 0; + } catch (ExitStatusException ese) { + exitCode = ese.getStatus(); + if (exitCode != 0) { + throw ese; + } + } } catch (BuildException be) { if (err != System.err) { printMessage(be); No revision Index: Main.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Main.java,v retrieving revision 1.95.2.7 retrieving revision 1.95.2.8 diff -u -r1.95.2.7 -r1.95.2.8 --- Main.java 23 Apr 2004 15:47:31 -0000 1.95.2.7 +++ Main.java 21 Jun 2004 22:15:05 -0000 1.95.2.8 @@ -184,8 +184,15 @@ // expect the worst int exitCode = 1; try { - runBuild(coreLoader); - exitCode = 0; + try { + runBuild(coreLoader); + exitCode = 0; + } catch (ExitStatusException ese) { + exitCode = ese.getStatus(); + if (exitCode != 0) { + throw ese; + } + } } catch (BuildException be) { if (err != System.err) { printMessage(be); 1.2.2.1 +0 -0 ant/src/main/org/apache/tools/ant/ExitStatusException.java Index: ExitStatusException.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/ExitStatusException.java,v retrieving revision 1.2 retrieving revision 1.2.2.1 diff -u -r1.2 -r1.2.2.1 No revision No revision 1.68.2.9 +25 -5 ant/src/main/org/apache/tools/ant/taskdefs/Execute.java Index: Execute.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v retrieving revision 1.68.2.8 retrieving revision 1.68.2.9 diff -u -r1.68.2.8 -r1.68.2.9 --- Execute.java 23 Apr 2004 15:14:56 -0000 1.68.2.8 +++ Execute.java 21 Jun 2004 22:15:05 -0000 1.68.2.9 @@ -431,7 +431,7 @@ } if (dir != null && !dir.exists()) { - throw new BuildException(dir + " doesn't exists."); + throw new BuildException(dir + " doesn't exist."); } return launcher.exec(project, command, env, dir); } @@ -445,7 +445,7 @@ */ public int execute() throws IOException { if (workingDirectory != null && !workingDirectory.exists()) { - throw new BuildException(workingDirectory + " doesn't exists."); + throw new BuildException(workingDirectory + " doesn't exist."); } final Process process = launch(project, getCommandline(), getEnvironment(), workingDirectory, @@ -475,6 +475,7 @@ watchdog.stop(); } streamHandler.stop(); + closeStreams(process); if (watchdog != null) { watchdog.checkException(); @@ -497,7 +498,7 @@ */ public void spawn() throws IOException { if (workingDirectory != null && !workingDirectory.exists()) { - throw new BuildException(workingDirectory + " doesn't exists."); + throw new BuildException(workingDirectory + " doesn't exist."); } final Process process = launch(project, getCommandline(), getEnvironment(), workingDirectory, @@ -574,7 +575,7 @@ */ public static boolean isFailure(int exitValue) { if (Os.isFamily("openvms")) { - // odd exit value signals failure + // even exit value signals failure return (exitValue % 2) == 0; } else { // non zero exit value signals failure @@ -649,6 +650,25 @@ } catch (java.io.IOException exc) { throw new BuildException("Could not launch " + cmdline[0] + ": " + exc, task.getLocation()); + } + } + + /** + * Close the streams belonging to the given Process. + * @param process the <CODE>Process</CODE>. + */ + public static void closeStreams(Process process) { + try { + process.getInputStream().close(); + } catch (IOException eyeOhEx) { + } + try { + process.getOutputStream().close(); + } catch (IOException eyeOhEx) { + } + try { + process.getErrorStream().close(); + } catch (IOException eyeOhEx) { } } 1.25.2.5 +86 -13 ant/src/main/org/apache/tools/ant/taskdefs/Exit.java Index: Exit.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Exit.java,v retrieving revision 1.25.2.4 retrieving revision 1.25.2.5 diff -u -r1.25.2.4 -r1.25.2.5 --- Exit.java 9 Mar 2004 17:01:33 -0000 1.25.2.4 +++ Exit.java 21 Jun 2004 22:15:05 -0000 1.25.2.5 @@ -17,9 +17,11 @@ package org.apache.tools.ant.taskdefs; -import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; - +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ExitStatusException; +import org.apache.tools.ant.taskdefs.condition.Condition; +import org.apache.tools.ant.taskdefs.condition.ConditionBase; /** * Exits the active build, giving an additional message @@ -34,13 +36,30 @@ * are true. i.e. * <pre>fail := defined(ifProperty) && !defined(unlessProperty)</pre> * + * A single nested<CODE><condition></CODE> element can be specified + * instead of using <CODE>if</CODE>/<CODE>unless</CODE> (a combined + * effect can be achieved using <CODE>isset</CODE> conditions). + * * @since Ant 1.2 * * @ant.task name="fail" category="control" */ public class Exit extends Task { + + private class NestedCondition extends ConditionBase implements Condition { + public boolean eval() { + if (countConditions() != 1) { + throw new BuildException( + "A single nested condition is required."); + } + return ((Condition)(getConditions().nextElement())).eval(); + } + } + private String message; private String ifCondition, unlessCondition; + private NestedCondition nestedCondition; + private Integer status; /** * A message giving further information on why the build exited. @@ -69,38 +88,56 @@ } /** - * evaluate both if and unless conditions, and if - * ifCondition is true or unlessCondition is false, throw a - * build exception to exit the build. - * The error message is constructed from the text fields, or from + * Set the status code to associate with the thrown Exception. + * @param i the <CODE>int</CODE> status + */ + public void setStatus(int i) { + status = new Integer(i); + } + + /** + * Throw a <CODE>BuildException</CODE> to exit (fail) the build. + * If specified, evaluate conditions: + * A single nested condition is accepted, but requires that the + * <CODE>if</CODE>/<code>unless</code> attributes be omitted. + * If the nested condition evaluates to true, or the + * ifCondition is true or unlessCondition is false, the build will exit. + * The error message is constructed from the text fields, from + * the nested condition (if specified), or finally from * the if and unless parameters (if present). * @throws BuildException */ public void execute() throws BuildException { - if (testIfCondition() && testUnlessCondition()) { + boolean fail = (nestedConditionPresent()) ? testNestedCondition() + : (testIfCondition() && testUnlessCondition()); + if (fail) { String text = null; - if (message != null && message.length() > 0) { - text = message; + if (message != null && message.trim().length() > 0) { + text = message.trim(); } else { - - if (getProject().getProperty(ifCondition) != null) { + if (ifCondition != null && ifCondition.length() > 0 + && getProject().getProperty(ifCondition) != null) { text = "if=" + ifCondition; } if (unlessCondition != null && unlessCondition.length() > 0 - && getProject().getProperty(unlessCondition) == null) { + && getProject().getProperty(unlessCondition) == null) { if (text == null) { text = ""; } else { text += " and "; } text += "unless=" + unlessCondition; + } + if (nestedConditionPresent()) { + text = "condition satisfied"; } else { if (text == null) { text = "No message"; } } } - throw new BuildException(text); + throw ((status == null) ? new BuildException(text) + : new ExitStatusException(text, status.intValue())); } } @@ -116,6 +153,19 @@ } /** + * Add a condition element. + * @return <CODE>ConditionBase</CODE>. + * @since Ant 1.6.2 + */ + public ConditionBase createCondition() { + if (nestedCondition != null) { + throw new BuildException("Only one nested condition is allowed."); + } + nestedCondition = new NestedCondition(); + return nestedCondition; + } + + /** * test the if condition * @return true if there is no if condition, or the named property exists */ @@ -136,6 +186,29 @@ return true; } return getProject().getProperty(unlessCondition) == null; + } + + /** + * test the nested condition + * @return true if there is none, or it evaluates to true + */ + private boolean testNestedCondition() { + boolean result = nestedConditionPresent(); + + if (result && ifCondition != null || unlessCondition != null) { + throw new BuildException("Nested conditions " + + "not permitted in conjunction with if/unless attributes"); + } + + return result && nestedCondition.eval(); + } + + /** + * test whether there is a nested condition. + * @return <CODE>boolean</CODE>. + */ + private boolean nestedConditionPresent() { + return (nestedCondition != null); } } No revision No revision 1.9.2.5 +59 -0 ant/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java Index: FailTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/FailTest.java,v retrieving revision 1.9.2.4 retrieving revision 1.9.2.5 diff -u -r1.9.2.4 -r1.9.2.5 --- FailTest.java 9 Mar 2004 17:02:01 -0000 1.9.2.4 +++ FailTest.java 21 Jun 2004 22:15:05 -0000 1.9.2.5 @@ -101,4 +101,63 @@ } } + public void testNested1() { + expectSpecificBuildException("testNested1", + "it is required to fail :-)", + "condition satisfied"); + } + + public void testNested2() { + try { + executeTarget("testNested2"); + } catch (BuildException be) { + fail("condition not satisfied; testNested2 must not fail"); + } + } + + public void testNested3() { + expectSpecificBuildException("testNested3", + "it is required to fail :-)", + "testNested3"); + } + + public void testNested4() { + String specificMessage = "Nested conditions " + + "not permitted in conjunction with if/unless attributes"; + + char[] c = {'a', 'b', 'c'}; + StringBuffer target = new StringBuffer("testNested4x"); + + for (int i = 0; i < c.length; i++) { + target.setCharAt(target.length() - 1, c[i]); + expectSpecificBuildException(target.toString(), + "it is required to fail :-)", specificMessage); + } + } + + public void testNested5() { + expectSpecificBuildException("testNested5", + "it is required to fail :-)", + "Only one nested condition is allowed."); + } + + public void testNested6() { + expectSpecificBuildException("testNested6", + "it is required to fail :-)", + "testNested6\ntestNested6\ntestNested6"); + } + + public void testNested7() { + String specificMessage = "A single nested condition is required."; + + char[] c = {'a', 'b'}; + StringBuffer target = new StringBuffer("testNested7x"); + + for (int i = 0; i < c.length; i++) { + target.setCharAt(target.length() - 1, c[i]); + expectSpecificBuildException(target.toString(), + "it is required to fail :-)", specificMessage); + } + } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]