📄 execute.java
字号:
//By default, use the shell launcher for VMS // if (Os.isFamily("openvms")) { useVMLauncher = false; } } /** * Set the stream handler to use. * @param streamHandler ExecuteStreamHandler. * @since Ant 1.6 */ public void setStreamHandler(ExecuteStreamHandler streamHandler) { this.streamHandler = streamHandler; } /** * Returns the commandline used to create a subprocess. * * @return the commandline used to create a subprocess. */ public String[] getCommandline() { return cmdl; } /** * Sets the commandline of the subprocess to launch. * * @param commandline the commandline of the subprocess to launch. */ public void setCommandline(String[] commandline) { cmdl = commandline; } /** * Set whether to propagate the default environment or not. * * @param newenv whether to propagate the process environment. */ public void setNewenvironment(boolean newenv) { newEnvironment = newenv; } /** * Returns the environment used to create a subprocess. * * @return the environment used to create a subprocess. */ public String[] getEnvironment() { return (env == null || newEnvironment) ? env : patchEnvironment(); } /** * Sets the environment variables for the subprocess to launch. * * @param env array of Strings, each element of which has * an environment variable settings in format <em>key=value</em>. */ public void setEnvironment(String[] env) { this.env = env; } /** * Sets the working directory of the process to execute. * * <p>This is emulated using the antRun scripts unless the OS is * Windows NT in which case a cmd.exe is spawned, * or MRJ and setting user.dir works, or JDK 1.3 and there is * official support in java.lang.Runtime. * * @param wd the working directory of the process. */ public void setWorkingDirectory(File wd) { workingDirectory = (wd == null || wd.getAbsolutePath().equals(antWorkingDirectory)) ? null : wd; } /** * Return the working directory. * @return the directory as a File. * @since Ant 1.7 */ public File getWorkingDirectory() { return workingDirectory == null ? new File(antWorkingDirectory) : workingDirectory; } /** * Set the name of the antRun script using the project's value. * * @param project the current project. * * @throws BuildException not clear when it is going to throw an exception, but * it is the method's signature. */ public void setAntRun(Project project) throws BuildException { this.project = project; } /** * Launch this execution through the VM, where possible, rather than through * the OS's shell. In some cases and operating systems using the shell will * allow the shell to perform additional processing such as associating an * executable with a script, etc. * * @param useVMLauncher true if exec should launch through the VM, * false if the shell should be used to launch the * command. */ public void setVMLauncher(boolean useVMLauncher) { this.useVMLauncher = useVMLauncher; } /** * Creates a process that runs a command. * * @param project the Project, only used for logging purposes, may be null. * @param command the command to run. * @param env the environment for the command. * @param dir the working directory for the command. * @param useVM use the built-in exec command for JDK 1.3 if available. * @return the process started. * @throws IOException forwarded from the particular launcher used. * * @since Ant 1.5 */ public static Process launch(Project project, String[] command, String[] env, File dir, boolean useVM) throws IOException { if (dir != null && !dir.exists()) { throw new BuildException(dir + " doesn't exist."); } CommandLauncher launcher = ((useVM && vmLauncher != null) ? vmLauncher : shellLauncher); return launcher.exec(project, command, env, dir); } /** * Runs a process defined by the command line and returns its exit status. * * @return the exit status of the subprocess or <code>INVALID</code>. * @exception java.io.IOException The exception is thrown, if launching * of the subprocess failed. */ public int execute() throws IOException { if (workingDirectory != null && !workingDirectory.exists()) { throw new BuildException(workingDirectory + " doesn't exist."); } final Process process = launch(project, getCommandline(), getEnvironment(), workingDirectory, useVMLauncher); try { streamHandler.setProcessInputStream(process.getOutputStream()); streamHandler.setProcessOutputStream(process.getInputStream()); streamHandler.setProcessErrorStream(process.getErrorStream()); } catch (IOException e) { process.destroy(); throw e; } streamHandler.start(); try { // add the process to the list of those to destroy if the VM exits // processDestroyer.add(process); if (watchdog != null) { watchdog.start(process); } waitFor(process); if (watchdog != null) { watchdog.stop(); } streamHandler.stop(); closeStreams(process); if (watchdog != null) { watchdog.checkException(); } return getExitValue(); } catch (ThreadDeath t) { // #31928: forcibly kill it before continuing. process.destroy(); throw t; } finally { // remove the process to the list of those to destroy if // the VM exits // processDestroyer.remove(process); } } /** * 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 { if (workingDirectory != null && !workingDirectory.exists()) { throw new BuildException(workingDirectory + " doesn't exist."); } final Process process = launch(project, getCommandline(), getEnvironment(), workingDirectory, useVMLauncher); if (Os.isFamily("windows")) { try { Thread.sleep(ONE_SECOND); } catch (InterruptedException e) { project.log("interruption in the sleep after having spawned a" + " process", Project.MSG_VERBOSE); } } OutputStream dummyOut = new OutputStream() { public void write(int b) throws IOException { } }; ExecuteStreamHandler handler = new PumpStreamHandler(dummyOut); handler.setProcessErrorStream(process.getErrorStream()); handler.setProcessOutputStream(process.getInputStream()); handler.start(); process.getOutputStream().close(); project.log("spawned process " + process.toString(), Project.MSG_VERBOSE); } /** * Wait for a given process. * * @param process the process one wants to wait for. */ protected void waitFor(Process process) { try { process.waitFor(); setExitValue(process.exitValue()); } catch (InterruptedException e) { process.destroy(); } } /** * Set the exit value. * * @param value exit value of the process. */ protected void setExitValue(int value) { exitValue = value; } /** * Query the exit value of the process. * @return the exit value or Execute.INVALID if no exit value has * been received. */ public int getExitValue() { return exitValue; } /** * Checks whether <code>exitValue</code> signals a failure on the current * system (OS specific). * * <p><b>Note</b> that this method relies on the conventions of * the OS, it will return false results if the application you are * running doesn't follow these conventions. One notable * exception is the Java VM provided by HP for OpenVMS - it will * return 0 if successful (like on any other platform), but this * signals a failure on OpenVMS. So if you execute a new Java VM * on OpenVMS, you cannot trust this method.</p> * * @param exitValue the exit value (return code) to be checked. * @return <code>true</code> if <code>exitValue</code> signals a failure. */ public static boolean isFailure(int exitValue) { //on openvms even exit value signals failure; // for other platforms nonzero exit value signals failure return Os.isFamily("openvms") ? (exitValue % 2 == 0) : (exitValue != 0); } /** * Did this execute return in a failure. * @see #isFailure(int) * @return true if and only if the exit code is interpreted as a failure * @since Ant1.7 */ public boolean isFailure() { return isFailure(getExitValue()); } /** * Test for an untimely death of the process. * @return true if a watchdog had to kill the process. * @since Ant 1.5 */ public boolean killedProcess() { return watchdog != null && watchdog.killedProcess(); } /** * Patch the current environment with the new values from the user. * @return the patched environment. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -