📄 execute.java
字号:
} /** * A command launcher for Windows XP/2000/NT that uses 'cmd.exe' when * launching commands in directories other than the current working * directory. */ private static class WinNTCommandLauncher extends CommandLauncherProxy { WinNTCommandLauncher(CommandLauncher launcher) { super(launcher); } /** * Launches the given command in a new process, in the given working * directory. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @param workingDir working directory where the command should run. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { File commandDir = workingDir; if (workingDir == null) { if (project != null) { commandDir = project.getBaseDir(); } else { return exec(project, cmd, env); } } // Use cmd.exe to change to the specified directory before running // the command final int preCmdLength = 6; String[] newcmd = new String[cmd.length + preCmdLength]; // CheckStyle:MagicNumber OFF - do not bother newcmd[0] = "cmd"; newcmd[1] = "/c"; newcmd[2] = "cd"; newcmd[3] = "/d"; newcmd[4] = commandDir.getAbsolutePath(); newcmd[5] = "&&"; // CheckStyle:MagicNumber ON System.arraycopy(cmd, 0, newcmd, preCmdLength, cmd.length); return exec(project, newcmd, env); } } /** * A command launcher for Mac that uses a dodgy mechanism to change * working directory before launching commands. */ private static class MacCommandLauncher extends CommandLauncherProxy { MacCommandLauncher(CommandLauncher launcher) { super(launcher); } /** * Launches the given command in a new process, in the given working * directory. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @param workingDir working directory where the command should run. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { if (workingDir == null) { return exec(project, cmd, env); } System.getProperties().put("user.dir", workingDir.getAbsolutePath()); try { return exec(project, cmd, env); } finally { System.getProperties().put("user.dir", antWorkingDirectory); } } } /** * A command launcher that uses an auxiliary script to launch commands * in directories other than the current working directory. */ private static class ScriptCommandLauncher extends CommandLauncherProxy { ScriptCommandLauncher(String script, CommandLauncher launcher) { super(launcher); myScript = script; } /** * Launches the given command in a new process, in the given working * directory. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @param workingDir working directory where the command should run. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { if (project == null) { if (workingDir == null) { return exec(project, cmd, env); } throw new IOException("Cannot locate antRun script: " + "No project provided"); } // Locate the auxiliary script String antHome = project.getProperty(MagicNames.ANT_HOME); if (antHome == null) { throw new IOException("Cannot locate antRun script: " + "Property '" + MagicNames.ANT_HOME + "' not found"); } String antRun = FILE_UTILS.resolveFile(project.getBaseDir(), antHome + File.separator + myScript).toString(); // Build the command File commandDir = workingDir; if (workingDir == null) { commandDir = project.getBaseDir(); } String[] newcmd = new String[cmd.length + 2]; newcmd[0] = antRun; newcmd[1] = commandDir.getAbsolutePath(); System.arraycopy(cmd, 0, newcmd, 2, cmd.length); return exec(project, newcmd, env); } private String myScript; } /** * A command launcher that uses an auxiliary perl script to launch commands * in directories other than the current working directory. */ private static class PerlScriptCommandLauncher extends CommandLauncherProxy { private String myScript; PerlScriptCommandLauncher(String script, CommandLauncher launcher) { super(launcher); myScript = script; } /** * Launches the given command in a new process, in the given working * directory. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @param workingDir working directory where the command should run. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { if (project == null) { if (workingDir == null) { return exec(project, cmd, env); } throw new IOException("Cannot locate antRun script: " + "No project provided"); } // Locate the auxiliary script String antHome = project.getProperty(MagicNames.ANT_HOME); if (antHome == null) { throw new IOException("Cannot locate antRun script: " + "Property '" + MagicNames.ANT_HOME + "' not found"); } String antRun = FILE_UTILS.resolveFile(project.getBaseDir(), antHome + File.separator + myScript).toString(); // Build the command File commandDir = workingDir; if (workingDir == null) { commandDir = project.getBaseDir(); } // CheckStyle:MagicNumber OFF String[] newcmd = new String[cmd.length + 3]; newcmd[0] = "perl"; newcmd[1] = antRun; newcmd[2] = commandDir.getAbsolutePath(); System.arraycopy(cmd, 0, newcmd, 3, cmd.length); // CheckStyle:MagicNumber ON return exec(project, newcmd, env); } } /** * A command launcher for VMS that writes the command to a temporary DCL * script before launching commands. This is due to limitations of both * the DCL interpreter and the Java VM implementation. */ private static class VmsCommandLauncher extends Java13CommandLauncher { public VmsCommandLauncher() throws NoSuchMethodException { super(); } /** * Launches the given command in a new process. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env) throws IOException { File cmdFile = createCommandFile(cmd, env); Process p = super.exec(project, new String[] {cmdFile.getPath()}, env); deleteAfter(cmdFile, p); return p; } /** * Launches the given command in a new process, in the given working * directory. Note that under Java 1.3.1, 1.4.0 and 1.4.1 on VMS this * method only works if <code>workingDir</code> is null or the logical * JAVA$FORK_SUPPORT_CHDIR needs to be set to TRUE. * @param project the Ant project. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @param workingDir working directory where the command should run. * @return the created Process. * @throws IOException forwarded from the exec method of the * command launcher. */ public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { File cmdFile = createCommandFile(cmd, env); Process p = super.exec(project, new String[] {cmdFile.getPath()}, env, workingDir); deleteAfter(cmdFile, p); return p; } /* * Writes the command into a temporary DCL script and returns the * corresponding File object. The script will be deleted on exit. * @param cmd the command line to execute as an array of strings. * @param env the environment to set as an array of strings. * @return the command File. * @throws IOException if errors are encountered creating the file. */ private File createCommandFile(String[] cmd, String[] env) throws IOException { File script = FILE_UTILS.createTempFile("ANT", ".COM", null, true, true); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(script)); // add the environment as logicals to the DCL script if (env != null) { int eqIndex; for (int i = 0; i < env.length; i++) { eqIndex = env[i].indexOf('='); if (eqIndex != -1) { out.print("$ DEFINE/NOLOG "); out.print(env[i].substring(0, eqIndex)); out.print(" \""); out.print(env[i].substring(eqIndex + 1)); out.println('\"'); } } } out.print("$ " + cmd[0]); for (int i = 1; i < cmd.length; i++) { out.println(" -"); out.print(cmd[i]); } } finally { if (out != null) { out.close(); } } return script; } private void deleteAfter(final File f, final Process p) { new Thread() { public void run() { try { p.waitFor(); } catch (InterruptedException e) { //ignore } FileUtils.delete(f); } } .start(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -