From 295ea16dac65b093eb59870cd59897f46e6f0199 Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Tue, 22 Apr 2003 22:12:53 +0000 Subject: [PATCH] allow to set a property with the exit code of java bugrep 19099 submitted by Donal Quinlan (donal at savvion dot com) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274500 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/java.html | 19 ++++++++++++++- src/etc/testcases/taskdefs/java.xml | 21 ++++++++++++++++ .../org/apache/tools/ant/taskdefs/Java.java | 24 +++++++++++++++++++ .../apache/tools/ant/taskdefs/JavaTest.java | 12 +++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/docs/manual/CoreTasks/java.html b/docs/manual/CoreTasks/java.html index 4b63fa7e7..28359051c 100644 --- a/docs/manual/CoreTasks/java.html +++ b/docs/manual/CoreTasks/java.html @@ -83,6 +83,13 @@ JVM. returncode other than 0. Default is "false" No + + resultproperty + 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. + No + dir The directory to invoke the VM in. (ignored if @@ -179,6 +186,16 @@ href="../using.html#path">PATH like structure and can also be set via a nest forked VM via nested env elements. See the description in the section about exec

Settings will be ignored if fork is disabled.

+ +

Errors and return codes

+By default the return code of a <java> is ignored. Alternatively, you can set resultproperty to the name +of a property and have it assigned to the result code (barring immutability, +of course). +When you set failonerror="true", the only possible value for resultproperty is 0. Any non zero response is treated as an +error and would mean the build exits. +

Similarly, if failonerror="false" and fork="false" +, then <java> must return 0 otherwise the build will exit, as the class was run by the build jvm.

+

Examples

  
        <java classname="test.Main">
@@ -218,7 +235,7 @@ and with a maximum memory of 128MB. Any non zero return code breaks the build.
 JVM, as it takes different parameters for other JVMs,
 That JVM can be started from <exec> if required.
 
-

Copyright © 2000-2002 Apache Software Foundation. All rights +

Copyright © 2000-2003 Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/taskdefs/java.xml b/src/etc/testcases/taskdefs/java.xml index fc4d4df1f..c920e36c0 100644 --- a/src/etc/testcases/taskdefs/java.xml +++ b/src/etc/testcases/taskdefs/java.xml @@ -89,5 +89,26 @@ + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 03bfc8077..2d9b9ae5f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -75,6 +75,7 @@ import org.apache.tools.ant.types.Reference; * @author Stefano Mazzocchi * stefano@apache.org * @author Stefan Bodewig + * @author Donal Quinlan * * @since Ant 1.1 * @@ -91,6 +92,7 @@ public class Java extends Task { private boolean append = false; private Long timeout = null; private Redirector redirector = new Redirector(this); + private String resultProperty; /** * Do the execution. */ @@ -106,6 +108,7 @@ public class Java extends Task { log("Java Result: " + err, Project.MSG_ERR); } } + maybeSetResultPropertyValue(err); } finally { dir = savedDir; } @@ -241,6 +244,27 @@ public class Java extends Task { 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. */ diff --git a/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java b/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java index 94896afb1..8038222f3 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java @@ -62,7 +62,8 @@ import org.apache.tools.ant.*; * stress out java task * @author steve loughran * @author Stephane Bailliez - */ + * @author Donal Quinlan + * */ public class JavaTest extends BuildFileTest { private boolean runFatalTests=false; @@ -166,6 +167,15 @@ public class JavaTest extends BuildFileTest { "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