Search in sources :

Example 1 with LogStreamHandler

use of org.apache.tools.ant.taskdefs.LogStreamHandler 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

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 BuildException (org.apache.tools.ant.BuildException)1 Execute (org.apache.tools.ant.taskdefs.Execute)1 LogStreamHandler (org.apache.tools.ant.taskdefs.LogStreamHandler)1