From 35845f7053e12288a95bf9720532d772ea2fa60b Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Mon, 28 Jul 2003 10:39:31 +0000 Subject: [PATCH] This change allows exec to start a process which will run independently of ant. I have used the patch prepared by Charles Hudak and Peter Nimmervoll, but made it even simpler, in the sense that I do not connect at all the new process to stream handlers and the ant logging system, disabling input, output, error, and return exec attributes in the case of spawn. Strangely, it works well on Windows if one puts a sleep of one second after having spawned the process. Why ? No idea. PR: 5907 Submitted by: Charles Hudak ( CHudak at arrowheadgrp dot com) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274963 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ docs/manual/CoreTasks/exec.html | 9 ++++ .../apache/tools/ant/taskdefs/ExecTask.java | 43 +++++++++++++------ .../apache/tools/ant/taskdefs/Execute.java | 36 ++++++++++++++++ 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 9dc6d9a71..ac58915cc 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -521,6 +521,9 @@ Other changes: * will now work on OpenVMS (please read the notes in 's manual page). Bugzilla Report 21877. +* will now have a new attribute spawn (default false). +If set to true, the process will be spawned. Bugzilla Report 5907. + * now supports a timeout which can be used to recover from deadlocks, etc in the parallel threads. also now supports a nested element. This can be used to diff --git a/docs/manual/CoreTasks/exec.html b/docs/manual/CoreTasks/exec.html index 93ffa84dc..2cb5d4aa8 100644 --- a/docs/manual/CoreTasks/exec.html +++ b/docs/manual/CoreTasks/exec.html @@ -59,6 +59,15 @@ Notes). in the "os.name" system property. No + + spawn + whether or not you want the command to be spawned
+ Default is false.
+ If you spawn a command, its output will not be logged by ant.
+ The input, output, error, and result property settings are not active when spawning a process. + + No + output Name of a file to which to write the output. If the error stream diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index f5a0af1f4..19228529c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -71,6 +71,7 @@ import org.apache.tools.ant.util.FileUtils; * @author thomas.haas@softwired-inc.com * @author Stefan Bodewig * @author Mariusz Nowostawski + * @author Charles Hudak * * @since Ant 1.2 * @@ -90,6 +91,7 @@ public class ExecTask extends Task { private boolean failIfExecFails = true; private String executable; private boolean resolveExecutable = false; + private boolean spawn = false; private Redirector redirector = new Redirector(this); @@ -99,6 +101,16 @@ public class ExecTask extends Task { */ private boolean vmLauncher = true; + /** + * set whether or not you want the process to be spawned + * default is not spawned + * @param spawn if true you do not want ant to wait for the end of the process + * @since ant 1.6 + */ + public void setSpawn(boolean spawn) { + this.spawn = spawn; + } + /** * Timeout in milliseconds after which the process will be killed. * @@ -455,6 +467,7 @@ public class ExecTask extends Task { exe.setAntRun(getProject()); exe.setWorkingDirectory(dir); exe.setVMLauncher(vmLauncher); + exe.setSpawn(spawn); String[] environment = env.getVariables(); if (environment != null) { for (int i = 0; i < environment.length; i++) { @@ -479,22 +492,26 @@ public class ExecTask extends Task { protected final void runExecute(Execute exe) throws IOException { int returnCode = -1; // assume the worst - returnCode = exe.execute(); + if (!spawn) { + returnCode = exe.execute(); - //test for and handle a forced process death - if (exe.killedProcess()) { - log("Timeout: killed the sub-process", Project.MSG_WARN); - } - maybeSetResultPropertyValue(returnCode); - if (Execute.isFailure(returnCode)) { - if (failOnError) { - throw new BuildException(getTaskType() + " returned: " - + returnCode, getLocation()); - } else { - log("Result: " + returnCode, Project.MSG_ERR); + //test for and handle a forced process death + if (exe.killedProcess()) { + log("Timeout: killed the sub-process", Project.MSG_WARN); } + maybeSetResultPropertyValue(returnCode); + if (Execute.isFailure(returnCode)) { + if (failOnError) { + throw new BuildException(getTaskType() + " returned: " + + returnCode, getLocation()); + } else { + log("Result: " + returnCode, Project.MSG_ERR); + } + } + redirector.complete(); + } else { + exe.spawn(); } - redirector.complete(); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/Execute.java b/src/main/org/apache/tools/ant/taskdefs/Execute.java index 0e38049d4..9e76abe03 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Execute.java +++ b/src/main/org/apache/tools/ant/taskdefs/Execute.java @@ -77,6 +77,7 @@ import org.apache.tools.ant.types.Commandline; * * @author thomas.haas@softwired-inc.com * @author Jeff Tulley + * @author Charles Hudak * * @since Ant 1.2 * @@ -103,6 +104,7 @@ public class Execute { private static CommandLauncher vmLauncher = null; private static CommandLauncher shellLauncher = null; private static Vector procEnvironment = null; + private boolean spawn = false; /** Used to destroy processes when the VM exits. */ private static ProcessDestroyer processDestroyer = new ProcessDestroyer(); @@ -171,6 +173,17 @@ public class Execute { } } + /** + * set whether or not you want the process to be spawned + * default is not spawned + * + * @param spawn if true you do not want ant to wait for the end of the process + * + * @since ant 1.6 + */ + public void setSpawn(boolean spawn) { + this.spawn = spawn; + } /** * Find the list of environment variables for this process. @@ -506,6 +519,29 @@ public class Execute { } } + /** + * Starts a process defined by the command line. + * Ant will not wait for this process, nor log its output + * + * @throws java.io.IOException The exception is thrown, if launching + * of the subprocess failed + * @since ant 1.6 + */ + public void spawn() throws IOException { + final Process process = launch(project, getCommandline(), + getEnvironment(), workingDirectory, + useVMLauncher); + if (Os.isFamily("windows")) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + project.log("interruption in the sleep after having spawned a process", + Project.MSG_VERBOSE); + } + } + project.log("spawned process " + process.toString(), Project.MSG_VERBOSE); + } + /** * wait for a given process *