You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by co...@apache.org on 2001/07/06 13:15:02 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs ExecTask.java Execute.java

conor       01/07/06 04:15:01

  Modified:    src/main/org/apache/tools/ant/taskdefs ExecTask.java
                        Execute.java
  Log:
  Add new vmlauncher attribute to exec. This defaults to true to execute
  commands using the VM's capabilities, where available. If it is set to false
  the underlying OS's shell will be used, either directly, or through the
  antRun scripts. Allows use of some shell features (such as associating scripts
  with their interpreters under Windows
  
  PR:	413
  
  Revision  Changes    Path
  1.14      +12 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
  
  Index: ExecTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ExecTask.java	2001/03/13 03:04:06	1.13
  +++ ExecTask.java	2001/07/06 11:14:53	1.14
  @@ -80,6 +80,9 @@
       protected Commandline cmdl = new Commandline();
       private FileOutputStream fos = null;
   
  +    /** Controls whether the VM (1.3 and above) is used to execute the command */
  +    private boolean vmLauncher = true;
  +     
       /**
        * Timeout in milliseconds after which the process will be killed.
        */
  @@ -194,6 +197,14 @@
       }
   
       /**
  +     * Control whether the VM is used to launch the new process or
  +     * whether the OS's shell is used.
  +     */
  +    public void setVMLauncher(boolean vmLauncher) {
  +        this.vmLauncher = vmLauncher;
  +    }
  +    
  +    /**
        * Create an Execute instance with the correct working directory set.
        */
       protected Execute prepareExec() throws BuildException {
  @@ -205,6 +216,7 @@
           Execute exe = new Execute(createHandler(), createWatchdog());
           exe.setAntRun(project);
           exe.setWorkingDirectory(dir);
  +        exe.setVMLauncher(vmLauncher);
           String[] environment = env.getVariables();
           if (environment != null) {
               for (int i=0; i<environment.length; i++) {
  
  
  
  1.18      +44 -17    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execute.java
  
  Index: Execute.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Execute.java	2001/04/21 02:20:36	1.17
  +++ Execute.java	2001/07/06 11:14:55	1.18
  @@ -89,18 +89,21 @@
       private Project project = null;
       private boolean newEnvironment = false;
   
  +    /** Controls whether the VM is used to launch commands, where possible */
  +    private boolean useVMLauncher = true;    
  +    
       private static String antWorkingDirectory = System.getProperty("user.dir");
  -    private static CommandLauncher launcher = createCommandLauncher();
  +    private static CommandLauncher vmLauncher = null;
  +    private static CommandLauncher shellLauncher = null;
       private static Vector procEnvironment = null;
   
       /** 
        * Builds a command launcher for the OS and JVM we are running under
        */
  -    private static CommandLauncher createCommandLauncher()
  -    {
  +    static {
           // Try using a JDK 1.3 launcher
           try {
  -            return new Java13CommandLauncher();
  +            vmLauncher = new Java13CommandLauncher();
           }
           catch ( NoSuchMethodException exc ) {
               // Ignore and keep try
  @@ -109,11 +112,11 @@
           String osname = System.getProperty("os.name").toLowerCase();
           if ( osname.indexOf("mac os") >= 0 ) {
               // Mac
  -            return new MacCommandLauncher(new CommandLauncher());
  +            shellLauncher = new MacCommandLauncher(new CommandLauncher());
           }
           else if ( osname.indexOf("os/2") >= 0 ) {
               // OS/2 - use same mechanism as Windows 2000
  -            return new WinNTCommandLauncher(new CommandLauncher());
  +            shellLauncher = new WinNTCommandLauncher(new CommandLauncher());
           }
           else if ( osname.indexOf("windows") >= 0 ) {
               // Windows.  Need to determine which JDK we're running in
  @@ -130,16 +133,16 @@
               // Determine if we're running under 2000/NT or 98/95
               if ( osname.indexOf("nt") >= 0 || osname.indexOf("2000") >= 0 ) {
                   // Windows 2000/NT
  -                return new WinNTCommandLauncher(baseLauncher);
  +                shellLauncher = new WinNTCommandLauncher(baseLauncher);
               }
               else {
                   // Windows 98/95 - need to use an auxiliary script
  -                return new ScriptCommandLauncher("bin/antRun.bat", baseLauncher);
  +                shellLauncher = new ScriptCommandLauncher("bin/antRun.bat", baseLauncher);
               }
           }
           else {
               // Generic
  -            return new ScriptCommandLauncher("bin/antRun", new CommandLauncher());
  +            shellLauncher = new ScriptCommandLauncher("bin/antRun", new CommandLauncher());
           }
       }
   
  @@ -353,6 +356,19 @@
       }
   
       /**
  +     * 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 vmLauncher true if exec should launch through thge VM, 
  +     *                   false if the shell should be used to launch the command.
  +     */
  +    public void setVMLauncher(boolean useVMLauncher) {
  +        this.useVMLauncher = useVMLauncher;
  +    }
  +    
  +    /**
        * Runs a process defined by the command line and returns its exit status.
        *
        * @return the exit status of the subprocess or <code>INVALID</code>
  @@ -360,6 +376,11 @@
        *            of the subprocess failed
        */
       public int execute() throws IOException {
  +        CommandLauncher launcher = vmLauncher != null ? vmLauncher : shellLauncher;
  +        if (!useVMLauncher) {
  +            launcher = shellLauncher;
  +        }
  +        
           final Process process = launcher.exec(project, getCommandline(), getEnvironment(), workingDirectory);
           try {
               streamHandler.setProcessInputStream(process.getOutputStream());
  @@ -605,8 +626,9 @@
            */
           public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException
           {
  +            File commandDir = workingDir;
               if ( workingDir == null ) {
  -                return exec(project, cmd, env);
  +                commandDir = project.getBaseDir();
               }
   
               // Use cmd.exe to change to the specified directory before running
  @@ -617,7 +639,7 @@
               newcmd[1] = "/c";
               newcmd[2] = "cd";
               newcmd[3] = "/d";
  -            newcmd[4] = workingDir.getAbsolutePath();
  +            newcmd[4] = commandDir.getAbsolutePath();
               newcmd[5] = "&&";
               System.arraycopy(cmd, 0, newcmd, preCmdLength, cmd.length);
   
  @@ -674,14 +696,14 @@
            */
           public Process exec(Project project, String[] cmd, String[] env, File workingDir) throws IOException
           {
  -            if ( workingDir == null ) {
  -                return exec(project, cmd, env);
  -            }
  -
  -            // Locate the auxiliary script
               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("ant.home");
               if ( antHome == null ) {
                   throw new IOException("Cannot locate antRun script: Property 'ant.home' not found");
  @@ -689,9 +711,14 @@
               String antRun = project.resolveFile(antHome + File.separator + _script).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] = workingDir.getAbsolutePath();
  +            newcmd[1] = commandDir.getAbsolutePath();
               System.arraycopy(cmd, 0, newcmd, 2, cmd.length);
               
               return exec(project, newcmd, env);