From b43f4224d8fe9d1e7d628e88df394a7dfed46d1b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 15 Jun 2012 04:34:04 +0000 Subject: [PATCH] PR 52706: allow command launcher to be selected via a task. Submitted by Vimil Saju git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1350461 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + contributors.xml | 4 + src/main/org/apache/tools/ant/MagicNames.java | 28 ++++++ .../ant/taskdefs/CommandLauncherTask.java | 6 ++ .../taskdefs/launcher/CommandLauncher.java | 89 +++++++++++-------- .../launcher/Java13CommandLauncher.java | 4 - .../launcher/PerlScriptCommandLauncher.java | 2 +- .../launcher/ScriptCommandLauncher.java | 4 +- .../taskdefs/launcher/VmsCommandLauncher.java | 2 +- 9 files changed, 95 insertions(+), 45 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 32d902f70..7fae70caa 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -357,6 +357,7 @@ Trejkaz Xaoza Ulrich Schmidt Valentino Miazzo Victor Toni +Vimil Saju Vincent Legoll Volker Leidl Waldek Herka diff --git a/contributors.xml b/contributors.xml index 5237b93cc..9e970355a 100644 --- a/contributors.xml +++ b/contributors.xml @@ -1440,6 +1440,10 @@ Vincent Legoll + + Vimil + Saju + Volker Leidl diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java index 87371bd74..9d021c848 100644 --- a/src/main/org/apache/tools/ant/MagicNames.java +++ b/src/main/org/apache/tools/ant/MagicNames.java @@ -248,5 +248,33 @@ public final class MagicNames { */ public static final String PROJECT_INVOKED_TARGETS = "ant.project.invoked-targets"; + + /** + * Name of the project reference holding an instance of {@link + * org.apache.tools.ant.taskdefs.launcher.CommandLauncher} to use + * when executing commands with the help of an external skript. + * + *

Alternatively this is the name of a system property holding + * the fully qualified class name of a {@link + * org.apache.tools.ant.taskdefs.launcher.CommandLauncher}.

+ * + * Value: {@value} + * @since Ant 1.9.0 + */ + public static final String ANT_SHELL_LAUNCHER_REF_ID = "ant.shellLauncher"; + + /** + * Name of the project reference holding an instance of {@link + * org.apache.tools.ant.taskdefs.launcher.CommandLauncher} to use + * when executing commands without the help of an external skript. + * + *

Alternatively this is the name of a system property holding + * the fully qualified class name of a {@link + * org.apache.tools.ant.taskdefs.launcher.CommandLauncher}.

+ * + * Value: {@value} + * @since Ant 1.9.0 + */ + public static final String ANT_VM_LAUNCHER_REF_ID = "ant.vmLauncher"; } diff --git a/src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java b/src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java index c0e60a8a0..f759784e4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java @@ -4,6 +4,12 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.launcher.CommandLauncher; +/** + * Task that configures the {@link + * org.apache.tools.ant.taskdefs.launcher.CommandLauncher} to used + * when starting external processes. + * @since Ant 1.9.0 + */ public class CommandLauncherTask extends Task { private boolean vmLauncher; private CommandLauncher commandLauncher; diff --git a/src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java b/src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java index 33de2c88b..bd6d7a7c7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java +++ b/src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java @@ -3,6 +3,9 @@ package org.apache.tools.ant.taskdefs.launcher; import java.io.File; import java.io.IOException; +import static org.apache.tools.ant.MagicNames.ANT_SHELL_LAUNCHER_REF_ID; +import static org.apache.tools.ant.MagicNames.ANT_VM_LAUNCHER_REF_ID; + import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.types.Commandline; @@ -14,8 +17,6 @@ import org.apache.tools.ant.util.FileUtils; * in the current working directory. */ public class CommandLauncher { - private static final String ANT_SHELL_LAUNCHER_REF_ID = "ant.shellLauncher"; - private static final String ANT_VM_LAUNCHER_REF_ID = "ant.vmLauncher"; protected static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); @@ -23,13 +24,8 @@ public class CommandLauncher { private static CommandLauncher shellLauncher = null; static { - // Try using a JDK 1.3 launcher - try { - if(!Os.isFamily("os/2")) { - vmLauncher = new Java13CommandLauncher(); - } - } catch(NoSuchMethodException exc) { - // Ignore and keep trying + if(!Os.isFamily("os/2")) { + vmLauncher = new Java13CommandLauncher(); } if (Os.isFamily("mac") && !Os.isFamily("unix")) { @@ -46,23 +42,22 @@ public class CommandLauncher { shellLauncher = new WinNTCommandLauncher(baseLauncher); } else { // Windows 98/95 - need to use an auxiliary script - shellLauncher = new ScriptCommandLauncher("bin/antRun.bat", baseLauncher); + shellLauncher = + new ScriptCommandLauncher("bin/antRun.bat", baseLauncher); } } else if (Os.isFamily("netware")) { CommandLauncher baseLauncher = new CommandLauncher(); - shellLauncher = new PerlScriptCommandLauncher("bin/antRun.pl", baseLauncher); + shellLauncher = + new PerlScriptCommandLauncher("bin/antRun.pl", baseLauncher); } else if (Os.isFamily("openvms")) { // OpenVMS - try { - shellLauncher = new VmsCommandLauncher(); - } catch(NoSuchMethodException exc) { - // Ignore and keep trying - } + shellLauncher = new VmsCommandLauncher(); } else { // Generic - shellLauncher = new ScriptCommandLauncher("bin/antRun", new CommandLauncher()); + shellLauncher = new ScriptCommandLauncher("bin/antRun", + new CommandLauncher()); } } @@ -80,9 +75,11 @@ public class CommandLauncher { * @throws IOException * if attempting to run a command in a specific directory. */ - public Process exec(Project project, String[] cmd, String[] env) throws IOException { + public Process exec(Project project, String[] cmd, String[] env) + throws IOException { if(project != null) { - project.log("Execute:CommandLauncher: " + Commandline.describeCommand(cmd), Project.MSG_DEBUG); + project.log("Execute:CommandLauncher: " + + Commandline.describeCommand(cmd), Project.MSG_DEBUG); } return Runtime.getRuntime().exec(cmd, env); } @@ -105,23 +102,22 @@ public class CommandLauncher { * @throws IOException * if trying to change directory. */ - public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { - if(workingDir == null) { + public Process exec(Project project, String[] cmd, String[] env, + File workingDir) throws IOException { + if (workingDir == null) { return exec(project, cmd, env); } throw new IOException("Cannot execute a process in different " - + "directory under this JVM"); + + "directory under this JVM"); } + /** + * Obtains the shell launcher configured for the given project or + * the default shell launcher. + */ public static CommandLauncher getShellLauncher(Project project) { - CommandLauncher launcher = null; - if(project != null) { - launcher = (CommandLauncher) project - .getReference(ANT_SHELL_LAUNCHER_REF_ID); - } - if (launcher == null) { - launcher = getSystemLauncher(ANT_SHELL_LAUNCHER_REF_ID); - } + CommandLauncher launcher = extractLauncher(ANT_SHELL_LAUNCHER_REF_ID, + project); if (launcher == null) { launcher = shellLauncher; } @@ -129,17 +125,28 @@ public class CommandLauncher { return launcher; } + /** + * Obtains the VM launcher configured for the given project or + * the default VM launcher. + */ public static CommandLauncher getVMLauncher(Project project) { + CommandLauncher launcher = extractLauncher(ANT_VM_LAUNCHER_REF_ID, + project); + if (launcher == null) { + launcher = vmLauncher; + } + return launcher; + } + + private static CommandLauncher extractLauncher(String referenceName, + Project project) { CommandLauncher launcher = null; if (project != null) { - launcher = (CommandLauncher)project.getReference(ANT_VM_LAUNCHER_REF_ID); + launcher = (CommandLauncher) project.getReference(referenceName); } if (launcher == null) { - launcher = getSystemLauncher(ANT_VM_LAUNCHER_REF_ID); - } - if (launcher == null) { - launcher = vmLauncher; + launcher = getSystemLauncher(referenceName); } return launcher; } @@ -169,13 +176,21 @@ public class CommandLauncher { return launcher; } - public static void setVMLauncher(Project project, CommandLauncher launcher) { + /** + * Sets the VM launcher to use for the given project. + */ + public static void setVMLauncher(Project project, + CommandLauncher launcher) { if (project != null) { project.addReference(ANT_VM_LAUNCHER_REF_ID, launcher); } } - public static void setShellLauncher(Project project, CommandLauncher launcher) { + /** + * Sets the shell launcher to use for the given project. + */ + public static void setShellLauncher(Project project, + CommandLauncher launcher) { if (project != null) { project.addReference(ANT_SHELL_LAUNCHER_REF_ID, launcher); } diff --git a/src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java b/src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java index 6cd33af25..cb81e6b90 100644 --- a/src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java +++ b/src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java @@ -13,10 +13,6 @@ import org.apache.tools.ant.types.Commandline; */ public class Java13CommandLauncher extends CommandLauncher { - public Java13CommandLauncher() throws NoSuchMethodException { - // Used to verify if Java13 is available, is prerequisite in ant 1.8 - } - /** * Launches the given command in a new process, in the given * working directory. diff --git a/src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java b/src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java index 201369784..475366fca 100644 --- a/src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java +++ b/src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java @@ -38,7 +38,7 @@ public class PerlScriptCommandLauncher extends CommandLauncherProxy { public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException { if (project == null) { - if(workingDir == null) { + if (workingDir == null) { return exec(project, cmd, env); } throw new IOException("Cannot locate antRun script: " diff --git a/src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java b/src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java index cf5a89c17..a9b64b476 100644 --- a/src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java +++ b/src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java @@ -46,7 +46,7 @@ public class ScriptCommandLauncher extends CommandLauncherProxy { } // Locate the auxiliary script String antHome = project.getProperty(MagicNames.ANT_HOME); - if(antHome == null) { + if (antHome == null) { throw new IOException("Cannot locate antRun script: " + "Property '" + MagicNames.ANT_HOME + "' not found"); @@ -57,7 +57,7 @@ public class ScriptCommandLauncher extends CommandLauncherProxy { // Build the command File commandDir = workingDir; - if(workingDir == null) { + if (workingDir == null) { commandDir = project.getBaseDir(); } String[] newcmd = new String[cmd.length + 2]; diff --git a/src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java b/src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java index 216e72339..56c6be29d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java +++ b/src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java @@ -15,7 +15,7 @@ import org.apache.tools.ant.util.FileUtils; */ public class VmsCommandLauncher extends Java13CommandLauncher { - public VmsCommandLauncher() throws NoSuchMethodException { + public VmsCommandLauncher() { super(); }