Browse Source

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
master
Stefan Bodewig 13 years ago
parent
commit
b43f4224d8
9 changed files with 95 additions and 45 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      contributors.xml
  3. +28
    -0
      src/main/org/apache/tools/ant/MagicNames.java
  4. +6
    -0
      src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java
  5. +52
    -37
      src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java
  6. +0
    -4
      src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java
  7. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java
  8. +2
    -2
      src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java
  9. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java

+ 1
- 0
CONTRIBUTORS View File

@@ -357,6 +357,7 @@ Trejkaz Xaoza
Ulrich Schmidt Ulrich Schmidt
Valentino Miazzo Valentino Miazzo
Victor Toni Victor Toni
Vimil Saju
Vincent Legoll Vincent Legoll
Volker Leidl Volker Leidl
Waldek Herka Waldek Herka


+ 4
- 0
contributors.xml View File

@@ -1440,6 +1440,10 @@
<first>Vincent</first> <first>Vincent</first>
<last>Legoll</last> <last>Legoll</last>
</name> </name>
<name>
<first>Vimil</first>
<last>Saju</last>
</name>
<name> <name>
<first>Volker</first> <first>Volker</first>
<last>Leidl</last> <last>Leidl</last>


+ 28
- 0
src/main/org/apache/tools/ant/MagicNames.java View File

@@ -248,5 +248,33 @@ public final class MagicNames {
*/ */
public static final String PROJECT_INVOKED_TARGETS public static final String PROJECT_INVOKED_TARGETS
= "ant.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.
*
* <p>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}.</p>
*
* 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.
*
* <p>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}.</p>
*
* Value: {@value}
* @since Ant 1.9.0
*/
public static final String ANT_VM_LAUNCHER_REF_ID = "ant.vmLauncher";
} }



+ 6
- 0
src/main/org/apache/tools/ant/taskdefs/CommandLauncherTask.java View File

@@ -4,6 +4,12 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.launcher.CommandLauncher; 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 { public class CommandLauncherTask extends Task {
private boolean vmLauncher; private boolean vmLauncher;
private CommandLauncher commandLauncher; private CommandLauncher commandLauncher;


+ 52
- 37
src/main/org/apache/tools/ant/taskdefs/launcher/CommandLauncher.java View File

@@ -3,6 +3,9 @@ package org.apache.tools.ant.taskdefs.launcher;
import java.io.File; import java.io.File;
import java.io.IOException; 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.Project;
import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.taskdefs.condition.Os;
import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Commandline;
@@ -14,8 +17,6 @@ import org.apache.tools.ant.util.FileUtils;
* in the current working directory. * in the current working directory.
*/ */
public class CommandLauncher { 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(); protected static final FileUtils FILE_UTILS = FileUtils.getFileUtils();


@@ -23,13 +24,8 @@ public class CommandLauncher {
private static CommandLauncher shellLauncher = null; private static CommandLauncher shellLauncher = null;


static { 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")) { if (Os.isFamily("mac") && !Os.isFamily("unix")) {
@@ -46,23 +42,22 @@ public class CommandLauncher {
shellLauncher = new WinNTCommandLauncher(baseLauncher); shellLauncher = new WinNTCommandLauncher(baseLauncher);
} else { } else {
// Windows 98/95 - need to use an auxiliary script // 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")) { } else if (Os.isFamily("netware")) {


CommandLauncher baseLauncher = new CommandLauncher(); CommandLauncher baseLauncher = new CommandLauncher();


shellLauncher = new PerlScriptCommandLauncher("bin/antRun.pl", baseLauncher);
shellLauncher =
new PerlScriptCommandLauncher("bin/antRun.pl", baseLauncher);
} else if (Os.isFamily("openvms")) { } else if (Os.isFamily("openvms")) {
// OpenVMS // OpenVMS
try {
shellLauncher = new VmsCommandLauncher();
} catch(NoSuchMethodException exc) {
// Ignore and keep trying
}
shellLauncher = new VmsCommandLauncher();
} else { } else {
// Generic // Generic
shellLauncher = new ScriptCommandLauncher("bin/antRun", new CommandLauncher());
shellLauncher = new ScriptCommandLauncher("bin/antRun",
new CommandLauncher());
} }
} }


@@ -80,9 +75,11 @@ public class CommandLauncher {
* @throws IOException * @throws IOException
* if attempting to run a command in a specific directory. * 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) { 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); return Runtime.getRuntime().exec(cmd, env);
} }
@@ -105,23 +102,22 @@ public class CommandLauncher {
* @throws IOException * @throws IOException
* if trying to change directory. * 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); return exec(project, cmd, env);
} }
throw new IOException("Cannot execute a process in different " 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) { 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) { if (launcher == null) {
launcher = shellLauncher; launcher = shellLauncher;
} }
@@ -129,17 +125,28 @@ public class CommandLauncher {
return launcher; return launcher;
} }


/**
* Obtains the VM launcher configured for the given project or
* the default VM launcher.
*/
public static CommandLauncher getVMLauncher(Project project) { 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; CommandLauncher launcher = null;
if (project != null) { if (project != null) {
launcher = (CommandLauncher)project.getReference(ANT_VM_LAUNCHER_REF_ID);
launcher = (CommandLauncher) project.getReference(referenceName);
} }


if (launcher == null) { if (launcher == null) {
launcher = getSystemLauncher(ANT_VM_LAUNCHER_REF_ID);
}
if (launcher == null) {
launcher = vmLauncher;
launcher = getSystemLauncher(referenceName);
} }
return launcher; return launcher;
} }
@@ -169,13 +176,21 @@ public class CommandLauncher {
return launcher; 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) { if (project != null) {
project.addReference(ANT_VM_LAUNCHER_REF_ID, launcher); 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) { if (project != null) {
project.addReference(ANT_SHELL_LAUNCHER_REF_ID, launcher); project.addReference(ANT_SHELL_LAUNCHER_REF_ID, launcher);
} }


+ 0
- 4
src/main/org/apache/tools/ant/taskdefs/launcher/Java13CommandLauncher.java View File

@@ -13,10 +13,6 @@ import org.apache.tools.ant.types.Commandline;
*/ */
public class Java13CommandLauncher extends CommandLauncher { 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 * Launches the given command in a new process, in the given
* working directory. * working directory.


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/launcher/PerlScriptCommandLauncher.java View File

@@ -38,7 +38,7 @@ public class PerlScriptCommandLauncher extends CommandLauncherProxy {
public Process exec(Project project, String[] cmd, String[] env, public Process exec(Project project, String[] cmd, String[] env,
File workingDir) throws IOException { File workingDir) throws IOException {
if (project == null) { if (project == null) {
if(workingDir == null) {
if (workingDir == null) {
return exec(project, cmd, env); return exec(project, cmd, env);
} }
throw new IOException("Cannot locate antRun script: " throw new IOException("Cannot locate antRun script: "


+ 2
- 2
src/main/org/apache/tools/ant/taskdefs/launcher/ScriptCommandLauncher.java View File

@@ -46,7 +46,7 @@ public class ScriptCommandLauncher extends CommandLauncherProxy {
} }
// Locate the auxiliary script // Locate the auxiliary script
String antHome = project.getProperty(MagicNames.ANT_HOME); String antHome = project.getProperty(MagicNames.ANT_HOME);
if(antHome == null) {
if (antHome == null) {
throw new IOException("Cannot locate antRun script: " throw new IOException("Cannot locate antRun script: "
+ "Property '" + MagicNames.ANT_HOME + "Property '" + MagicNames.ANT_HOME
+ "' not found"); + "' not found");
@@ -57,7 +57,7 @@ public class ScriptCommandLauncher extends CommandLauncherProxy {


// Build the command // Build the command
File commandDir = workingDir; File commandDir = workingDir;
if(workingDir == null) {
if (workingDir == null) {
commandDir = project.getBaseDir(); commandDir = project.getBaseDir();
} }
String[] newcmd = new String[cmd.length + 2]; String[] newcmd = new String[cmd.length + 2];


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/launcher/VmsCommandLauncher.java View File

@@ -15,7 +15,7 @@ import org.apache.tools.ant.util.FileUtils;
*/ */
public class VmsCommandLauncher extends Java13CommandLauncher { public class VmsCommandLauncher extends Java13CommandLauncher {


public VmsCommandLauncher() throws NoSuchMethodException {
public VmsCommandLauncher() {
super(); super();
} }




Loading…
Cancel
Save