antoine 2003/04/22 15:12:53
Modified: src/main/org/apache/tools/ant/taskdefs Java.java docs/manual/CoreTasks java.html src/etc/testcases/taskdefs java.xml src/testcases/org/apache/tools/ant/taskdefs JavaTest.java Log: allow to set a property with the exit code of java bugrep 19099 submitted by Donal Quinlan (donal at savvion dot com) Revision Changes Path 1.58 +24 -0 ant/src/main/org/apache/tools/ant/taskdefs/Java.java Index: Java.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v retrieving revision 1.57 retrieving revision 1.58 diff -u -r1.57 -r1.58 --- Java.java 7 Mar 2003 11:23:01 -0000 1.57 +++ Java.java 22 Apr 2003 22:12:53 -0000 1.58 @@ -75,6 +75,7 @@ * @author Stefano Mazzocchi * <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @author Stefan Bodewig + * @author <a href="mailto:[EMAIL PROTECTED]">Donal Quinlan</a> * * @since Ant 1.1 * @@ -91,6 +92,7 @@ private boolean append = false; private Long timeout = null; private Redirector redirector = new Redirector(this); + private String resultProperty; /** * Do the execution. */ @@ -106,6 +108,7 @@ log("Java Result: " + err, Project.MSG_ERR); } } + maybeSetResultPropertyValue(err); } finally { dir = savedDir; } @@ -241,6 +244,27 @@ return cmdl.createArgument(); } + /** + * The name of a property in which the return code of the + * command should be stored. Only of interest if failonerror=false. + * + * @since Ant 1.6 + */ + public void setResultProperty(String resultProperty) { + this.resultProperty = resultProperty; + } + + /** + * helper method to set result property to the + * passed in value if appropriate + */ + protected void maybeSetResultPropertyValue(int result) { + String res = Integer.toString(result); + if (resultProperty != null) { + project.setNewProperty(resultProperty, res); + } + } + /** * If true, execute in a new VM. */ 1.17 +18 -1 ant/docs/manual/CoreTasks/java.html Index: java.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTasks/java.html,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- java.html 9 Feb 2003 22:10:57 -0000 1.16 +++ java.html 22 Apr 2003 22:12:53 -0000 1.17 @@ -84,6 +84,13 @@ <td align="center" valign="top">No</td> </tr> <tr> + <td valign="top">resultproperty</td> + <td valign="top">The name of a property in which the return code of the + command should be stored. Only of interest if failonerror=false + and if fork=true.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> <td valign="top">dir</td> <td valign="top">The directory to invoke the VM in. (ignored if fork is disabled)</td> @@ -179,6 +186,16 @@ forked VM via nested <i>env</i> elements. See the description in the section about <a href="exec.html#env">exec</a></p> <p>Settings will be ignored if fork is disabled.</p> + +<h3>Errors and return codes</h3> +By default the return code of a <java> is ignored. Alternatively, you can set <code>resultproperty</code> to the name +of a property and have it assigned to the result code (barring immutability, +of course). +When you set <code>failonerror="true"</code>, the only possible value for <code>resultproperty</code> is 0. Any non zero response is treated as an +error and would mean the build exits. +<p> Similarly, if <code>failonerror="false"</code> and <code>fork="false"</code> +, then <code><java></code> <b>must</b> return 0 otherwise the build will exit, as the class was run by the build jvm.</p> + <h3>Examples</h3> <pre> <java classname="test.Main"> @@ -218,7 +235,7 @@ JVM, as it takes different parameters for other JVMs, That JVM can be started from <exec> if required. <hr> -<p align="center">Copyright © 2000-2002 Apache Software Foundation. All rights +<p align="center">Copyright © 2000-2003 Apache Software Foundation. All rights Reserved.</p> </body> 1.5 +21 -0 ant/src/etc/testcases/taskdefs/java.xml Index: java.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/java.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- java.xml 24 Jul 2002 15:43:28 -0000 1.4 +++ java.xml 22 Apr 2003 22:12:53 -0000 1.5 @@ -89,5 +89,26 @@ </java> </target> + <target name="testResultPropertyZero"> + <java classname="${app}" + classpath="${tests-classpath.value}" + resultproperty="exitcode" + > + </java> + <echo message="exitcode = ${exitcode}"/> + </target> + + <target name="testResultPropertyNonZero"> + <java classname="${app}" + classpath="${tests-classpath.value}" + resultproperty="exitcode" + failonerror="false" + fork="true" + > + <arg value="-1"/> + </java> + <echo message="exitcode = ${exitcode}"/> + </target> + <target name="foo" /> </project> 1.10 +11 -1 ant/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java Index: JavaTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- JavaTest.java 10 Feb 2003 14:14:45 -0000 1.9 +++ JavaTest.java 22 Apr 2003 22:12:53 -0000 1.10 @@ -62,7 +62,8 @@ * stress out java task * @author steve loughran * @author <a href="mailto:[EMAIL PROTECTED]">Stephane Bailliez</a> - */ + * @author <a href="mailto:[EMAIL PROTECTED]">Donal Quinlan</a> + * */ public class JavaTest extends BuildFileTest { private boolean runFatalTests=false; @@ -166,6 +167,15 @@ "Java returned:"); } + public void testResultPropertyZero() { + executeTarget("testResultPropertyZero"); + assertEquals("0",project.getProperty("exitcode")); + } + + public void testResultPropertyNonZero() { + executeTarget("testResultPropertyNonZero"); + assertEquals("-1",project.getProperty("exitcode")); + } /** * entry point class with no dependencies other