Browse Source

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
master
Antoine Levy-Lambert 22 years ago
parent
commit
35845f7053
4 changed files with 78 additions and 13 deletions
  1. +3
    -0
      WHATSNEW
  2. +9
    -0
      docs/manual/CoreTasks/exec.html
  3. +30
    -13
      src/main/org/apache/tools/ant/taskdefs/ExecTask.java
  4. +36
    -0
      src/main/org/apache/tools/ant/taskdefs/Execute.java

+ 3
- 0
WHATSNEW View File

@@ -521,6 +521,9 @@ Other changes:
* <exec> will now work on OpenVMS (please read the notes in
<exec>'s manual page). Bugzilla Report 21877.

* <exec> will now have a new attribute spawn (default false).
If set to true, the process will be spawned. Bugzilla Report 5907.

* <parallel> now supports a timeout which can be used to recover
from deadlocks, etc in the parallel threads. <parallel> also
now supports a <daemons> nested element. This can be used to


+ 9
- 0
docs/manual/CoreTasks/exec.html View File

@@ -59,6 +59,15 @@ Notes</i>).
in the &quot;os.name&quot; system property.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">spawn</td>
<td valign="top">whether or not you want the command to be spawned<br/>
Default is false.<br>
If you spawn a command, its output will not be logged by ant.<br/>
The input, output, error, and result property settings are not active when spawning a process.
</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">output</td>
<td valign="top">Name of a file to which to write the output. If the error stream


+ 30
- 13
src/main/org/apache/tools/ant/taskdefs/ExecTask.java View File

@@ -71,6 +71,7 @@ import org.apache.tools.ant.util.FileUtils;
* @author thomas.haas@softwired-inc.com
* @author Stefan Bodewig
* @author <a href="mailto:mariusz@rakiura.org">Mariusz Nowostawski</a>
* @author <a href="mailto:CHudak@arrowheadgrp.com">Charles Hudak</a>
*
* @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();
}

/**


+ 36
- 0
src/main/org/apache/tools/ant/taskdefs/Execute.java View File

@@ -77,6 +77,7 @@ import org.apache.tools.ant.types.Commandline;
*
* @author thomas.haas@softwired-inc.com
* @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a>
* @author <a href="mailto:CHudak@arrowheadgrp.com">Charles Hudak</a>
*
* @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
*


Loading…
Cancel
Save