Search in sources :

Example 6 with Execute

use of org.apache.tools.ant.taskdefs.Execute in project ceylon-compiler by ceylon.

the class CeylonAntTask method executeCommandline.

/**
     * Executes a Commandline
     * @param cmd The commandline
     */
protected void executeCommandline(Commandline cmd) {
    File tmpFile = null;
    try {
        int exitValue;
        if (getFork()) {
            log("Spawning new process: " + Arrays.toString(cmd.getCommandline()), Project.MSG_VERBOSE);
            String[] args = cmd.getCommandline();
            if (Commandline.toString(args).length() > MAX_COMMAND_LENGTH) {
                BufferedWriter out = null;
                try {
                    tmpFile = File.createTempFile("ceylon-ant-", "cmd");
                    out = new BufferedWriter(new FileWriter(tmpFile));
                    for (int i = 1; i < args.length; i++) {
                        out.write(args[i]);
                        out.newLine();
                    }
                    out.flush();
                    String[] newArgs = new String[2];
                    newArgs[0] = args[0];
                    newArgs[1] = "@" + tmpFile.getAbsolutePath();
                    args = newArgs;
                } finally {
                    FileUtils.close(out);
                }
            }
            Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
            exe.setAntRun(getProject());
            exe.setWorkingDirectory(getProject().getBaseDir());
            exe.setCommandline(args);
            exe.execute();
            exitValue = exe.getExitValue();
        } else {
            log("Launching Launcher in this JVM: " + Arrays.toString(cmd.getArguments()), Project.MSG_VERBOSE);
            exitValue = Launcher.runInJava7Checked(getLoader(), cmd.getArguments());
        }
        if (exitValue != 0) {
            String message = formatFailureMessage(cmd);
            exitHandler.handleExit(this, exitValue, message);
        } else {
            exitHandler.handleExit(this, exitValue, null);
        }
    } catch (BuildException e) {
        // let build exceptions through, since we throw them ourselves in handleExit!
        throw e;
    } catch (Throwable e) {
        throw new BuildException("Error running Ceylon " + toolName + " tool (an exception was thrown, run ant with -v parameter to see the exception)", e, getLocation());
    } finally {
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
}
Also used : Execute(org.apache.tools.ant.taskdefs.Execute) FileWriter(java.io.FileWriter) BuildException(org.apache.tools.ant.BuildException) LogStreamHandler(org.apache.tools.ant.taskdefs.LogStreamHandler) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

BuildException (org.apache.tools.ant.BuildException)6 Execute (org.apache.tools.ant.taskdefs.Execute)6 IOException (java.io.IOException)4 File (java.io.File)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 Path (java.nio.file.Path)2 SuiteHint (com.carrotsearch.ant.tasks.junit4.balancers.SuiteHint)1 BootstrapEvent (com.carrotsearch.ant.tasks.junit4.events.BootstrapEvent)1 QuitEvent (com.carrotsearch.ant.tasks.junit4.events.QuitEvent)1 AggregatedQuitEvent (com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedQuitEvent)1 AggregatingListener (com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatingListener)1 ChildBootstrap (com.carrotsearch.ant.tasks.junit4.events.aggregated.ChildBootstrap)1 SlaveMainSafe (com.carrotsearch.ant.tasks.junit4.slave.SlaveMainSafe)1 TeeOutputStream (com.carrotsearch.randomizedtesting.TeeOutputStream)1 SuppressForbidden (com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)1 EventBus (com.google.common.eventbus.EventBus)1 Subscribe (com.google.common.eventbus.Subscribe)1 Closer (com.google.common.io.Closer)1 BufferedInputStream (java.io.BufferedInputStream)1