From 00b218efe5527702354b56e4308217c70b6ac048 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Thu, 17 Jun 2004 20:33:13 +0000 Subject: [PATCH] Add status attribute to . Idea/design by Aurele Venet. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276588 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 + docs/manual/CoreTasks/fail.html | 7 +++ .../apache/tools/ant/ExitStatusException.java | 56 +++++++++++++++++++ src/main/org/apache/tools/ant/Main.java | 11 +++- .../org/apache/tools/ant/taskdefs/Exit.java | 13 ++++- 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/main/org/apache/tools/ant/ExitStatusException.java diff --git a/WHATSNEW b/WHATSNEW index 52e5e1288..4198302db 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -37,6 +37,9 @@ Other changes: which will allow nonexistent files specified via s to be passed to the executable. Bugzilla Report 29585. +* has a status attribute that can be used to pass an exit + status back to the command line. + Changes from Ant 1.6.1 to current Ant 1.6 CVS version ===================================================== diff --git a/docs/manual/CoreTasks/fail.html b/docs/manual/CoreTasks/fail.html index 7801fe7f4..5ac9994c6 100644 --- a/docs/manual/CoreTasks/fail.html +++ b/docs/manual/CoreTasks/fail.html @@ -36,6 +36,13 @@ or character data nested into the element.

exist in the current project No + + status + Exit using the specified status code; + assuming the generated Exception is not caught, the + JVM will exit with this status. Since Ant 1.7 + No +

Parameters specified as nested elements

diff --git a/src/main/org/apache/tools/ant/ExitStatusException.java b/src/main/org/apache/tools/ant/ExitStatusException.java new file mode 100644 index 000000000..cfc28be82 --- /dev/null +++ b/src/main/org/apache/tools/ant/ExitStatusException.java @@ -0,0 +1,56 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant; + +/** + * BuildException + exit status. + * + * @since Ant 1.7 + */ +public class ExitStatusException extends BuildException { + + /** Status code */ + private int status; + + /** + * Constructs an ExitStatusException. + * @param status the associated status code + */ + public ExitStatusException(int status) { + super(); + this.status = status; + } + + /** + * Constructs an exit exception. + * @param msg the associated message + * @param status the associated status code + */ + public ExitStatusException(String msg, int status) { + super(msg); + this.status = status; + } + + /** + * Get the status code + * + * @return int + */ + public int getStatus() { + return status; + } +} diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index 6dd940dd7..05b19440c 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -184,8 +184,15 @@ public class Main implements AntMain { // 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); diff --git a/src/main/org/apache/tools/ant/taskdefs/Exit.java b/src/main/org/apache/tools/ant/taskdefs/Exit.java index 8d339d41f..731750c08 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Exit.java +++ b/src/main/org/apache/tools/ant/taskdefs/Exit.java @@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs; 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; @@ -58,6 +59,7 @@ public class Exit extends Task { private String message; private String ifCondition, unlessCondition; private NestedCondition nestedCondition; + private Integer status; /** * A message giving further information on why the build exited. @@ -85,6 +87,14 @@ public class Exit extends Task { unlessCondition = c; } + /** + * Set the status code to associate with the thrown Exception. + * @param i the int status + */ + public void setStatus(int i) { + status = new Integer(i); + } + /** * Throw a BuildException to exit (fail) the build. * If specified, evaluate conditions: @@ -126,7 +136,8 @@ public class Exit extends Task { } } } - throw new BuildException(text); + throw ((status == null) ? new BuildException(text) + : new ExitStatusException(text, status.intValue())); } }