FileDocCategorySizeDatePackage
Exec.javaAPI DocApache Ant 1.708744Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs

Exec

public class Exec extends org.apache.tools.ant.Task
Executes a given command if the os platform is appropriate.

As of Ant 1.2, this class is no longer the implementation of Ant's <exec> task - it is considered to be dead code by the Ant developers and is unmaintained. Don't use it.

deprecated
since 1.2. delegate to {@link org.apache.tools.ant.taskdefs.Execute Execute} instead.

Fields Summary
private String
os
private String
out
private File
dir
private String
command
protected PrintWriter
fos
private boolean
failOnError
Constructors Summary
public Exec()
Constructor for Exec. Prints a warning message to std error.


                   
      
        System.err.println("As of Ant 1.2 released in October 2000, "
            + "the Exec class");
        System.err.println("is considered to be dead code by the Ant "
            + "developers and is unmaintained.");
        System.err.println("Don\'t use it!");
    
Methods Summary
public voidexecute()
Execute the task.

throws
BuildException on error

        run(command);
    
protected voidlogFlush()
Close output.

        if (fos != null) {
          fos.close();
        }
    
protected voidoutputLog(java.lang.String line, int messageLevel)
Log an output message.

param
line the line to putput
param
messageLevel the level of logging - ignored if output is going to a file

        if (fos == null) {
            log(line, messageLevel);
        } else {
            fos.println(line);
        }
    
protected intrun(java.lang.String command)
Execute the command.

param
command the command to exec
return
the exit value of the command
throws
BuildException on error


        int err = -1; // assume the worst

        // test if os match
        String myos = System.getProperty("os.name");
        log("Myos = " + myos, Project.MSG_VERBOSE);
        if ((os != null) && (os.indexOf(myos) < 0)) {
            // this command will be executed only on the specified OS
            log("Not found in " + os, Project.MSG_VERBOSE);
            return 0;
        }

        // default directory to the project's base directory
        if (dir == null) {
          dir = getProject().getBaseDir();
        }

        if (myos.toLowerCase().indexOf("windows") >= 0) {
            if (!dir.equals(getProject().resolveFile("."))) {
                if (myos.toLowerCase().indexOf("nt") >= 0) {
                    command = "cmd /c cd " + dir + " && " + command;
                } else {
                    String ant = getProject().getProperty(MagicNames.ANT_HOME);
                    if (ant == null) {
                        throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not "
                            + "found", getLocation());
                    }

                    String antRun = getProject().resolveFile(ant + "/bin/antRun.bat").toString();
                    command = antRun + " " + dir + " " + command;
                }
            }
        } else {
            String ant = getProject().getProperty(MagicNames.ANT_HOME);
            if (ant == null) {
              throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not found",
                                       getLocation());
            }
            String antRun = getProject().resolveFile(ant + "/bin/antRun").toString();

            command = antRun + " " + dir + " " + command;
        }

        try {
            // show the command
            log(command, Project.MSG_VERBOSE);

            // exec command on system runtime
            Process proc = Runtime.getRuntime().exec(command);

            if (out != null)  {
                fos = new PrintWriter(new FileWriter(out));
                log("Output redirected to " + out, Project.MSG_VERBOSE);
            }

            // copy input and error to the output stream
            StreamPumper inputPumper =
                new StreamPumper(proc.getInputStream(), Project.MSG_INFO);
            StreamPumper errorPumper =
                new StreamPumper(proc.getErrorStream(), Project.MSG_WARN);

            // starts pumping away the generated output/error
            inputPumper.start();
            errorPumper.start();

            // Wait for everything to finish
            proc.waitFor();
            inputPumper.join();
            errorPumper.join();
            proc.destroy();

            // close the output file if required
            logFlush();

            // check its exit value
            err = proc.exitValue();
            if (err != 0) {
                if (failOnError) {
                    throw new BuildException("Exec returned: " + err, getLocation());
                } else {
                    log("Result: " + err, Project.MSG_ERR);
                }
            }
        } catch (IOException ioe) {
            throw new BuildException("Error exec: " + command, ioe, getLocation());
        } catch (InterruptedException ex) {
            //ignore
        }

        return err;
    
public voidsetCommand(java.lang.String command)
Set the command to exec.

param
command a String value

        this.command = command;
    
public voidsetDir(java.lang.String d)
Set the directory.

param
d a String value

        this.dir = getProject().resolveFile(d);
    
public voidsetFailonerror(boolean fail)
Set the failOnError attribute. Default is false.

param
fail a boolean value

        failOnError = fail;
    
public voidsetOs(java.lang.String os)
Set the Operating System that this exec is to run in.

param
os a String value

        this.os = os;
    
public voidsetOutput(java.lang.String out)
Set the output filename.

param
out a String value

        this.out = out;