Browse Source

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
master
Antoine Levy-Lambert 22 years ago
parent
commit
295ea16dac
4 changed files with 74 additions and 2 deletions
  1. +18
    -1
      docs/manual/CoreTasks/java.html
  2. +21
    -0
      src/etc/testcases/taskdefs/java.xml
  3. +24
    -0
      src/main/org/apache/tools/ant/taskdefs/Java.java
  4. +11
    -1
      src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java

+ 18
- 1
docs/manual/CoreTasks/java.html View File

@@ -83,6 +83,13 @@ JVM.
returncode other than 0. Default is "false"</td> returncode other than 0. Default is "false"</td>
<td align="center" valign="top">No</td> <td align="center" valign="top">No</td>
</tr> </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> <tr>
<td valign="top">dir</td> <td valign="top">dir</td>
<td valign="top">The directory to invoke the VM in. (ignored if <td valign="top">The directory to invoke the VM in. (ignored if
@@ -179,6 +186,16 @@ href="../using.html#path">PATH like structure</a> and can also be set via a nest
forked VM via nested <i>env</i> elements. See the description in the forked VM via nested <i>env</i> elements. See the description in the
section about <a href="exec.html#env">exec</a></p> section about <a href="exec.html#env">exec</a></p>
<p>Settings will be ignored if fork is disabled.</p> <p>Settings will be ignored if fork is disabled.</p>
<h3>Errors and return codes</h3>
By default the return code of a &lt;java&gt; 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>&lt;java&gt;</code> <b>must</b> return 0 otherwise the build will exit, as the class was run by the build jvm.</p>

<h3>Examples</h3> <h3>Examples</h3>
<pre> <pre>
&lt;java classname=&quot;test.Main&quot;&gt; &lt;java classname=&quot;test.Main&quot;&gt;
@@ -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, JVM, as it takes different parameters for other JVMs,
That JVM can be started from &lt;exec&gt; if required. That JVM can be started from &lt;exec&gt; if required.
<hr> <hr>
<p align="center">Copyright &copy; 2000-2002 Apache Software Foundation. All rights
<p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
Reserved.</p> Reserved.</p>


</body> </body>


+ 21
- 0
src/etc/testcases/taskdefs/java.xml View File

@@ -89,5 +89,26 @@
</java> </java>
</target> </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" /> <target name="foo" />
</project> </project>

+ 24
- 0
src/main/org/apache/tools/ant/taskdefs/Java.java View File

@@ -75,6 +75,7 @@ import org.apache.tools.ant.types.Reference;
* @author Stefano Mazzocchi * @author Stefano Mazzocchi
* <a href="mailto:stefano@apache.org">stefano@apache.org</a> * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author Stefan Bodewig * @author Stefan Bodewig
* @author <a href="mailto:donal@savvion.com">Donal Quinlan</a>
* *
* @since Ant 1.1 * @since Ant 1.1
* *
@@ -91,6 +92,7 @@ public class Java extends Task {
private boolean append = false; private boolean append = false;
private Long timeout = null; private Long timeout = null;
private Redirector redirector = new Redirector(this); private Redirector redirector = new Redirector(this);
private String resultProperty;
/** /**
* Do the execution. * Do the execution.
*/ */
@@ -106,6 +108,7 @@ public class Java extends Task {
log("Java Result: " + err, Project.MSG_ERR); log("Java Result: " + err, Project.MSG_ERR);
} }
} }
maybeSetResultPropertyValue(err);
} finally { } finally {
dir = savedDir; dir = savedDir;
} }
@@ -241,6 +244,27 @@ public class Java extends Task {
return cmdl.createArgument(); 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. * If true, execute in a new VM.
*/ */


+ 11
- 1
src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java View File

@@ -62,7 +62,8 @@ import org.apache.tools.ant.*;
* stress out java task * stress out java task
* @author steve loughran * @author steve loughran
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
*/
* @author <a href="mailto:donal@savvion.com">Donal Quinlan</a>
* */
public class JavaTest extends BuildFileTest { public class JavaTest extends BuildFileTest {
private boolean runFatalTests=false; private boolean runFatalTests=false;
@@ -166,6 +167,15 @@ public class JavaTest extends BuildFileTest {
"Java returned:"); "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 * entry point class with no dependencies other


Loading…
Cancel
Save