Search in sources :

Example 6 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class ANTLR method run.

/**
 * execute in a forked VM
 */
private int run(String[] command) throws BuildException {
    PumpStreamHandler psh = new PumpStreamHandler(new LogOutputStream(this, Project.MSG_INFO), new TeeOutputStream(new LogOutputStream(this, Project.MSG_WARN), bos));
    Execute exe = new Execute(psh, null);
    exe.setAntRun(getProject());
    if (workingdir != null) {
        exe.setWorkingDirectory(workingdir);
    }
    exe.setCommandline(command);
    try {
        return exe.execute();
    } catch (IOException e) {
        throw new BuildException(e, getLocation());
    } finally {
        FileUtils.close(bos);
    }
}
Also used : TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) PumpStreamHandler(org.apache.tools.ant.taskdefs.PumpStreamHandler) Execute(org.apache.tools.ant.taskdefs.Execute) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream)

Example 7 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class Cab method execute.

/**
 * execute this task.
 * @throws BuildException on error.
 */
@Override
public void execute() throws BuildException {
    checkConfiguration();
    Vector<String> files = getFileList();
    // quick exit if the target is up to date
    if (isUpToDate(files)) {
        return;
    }
    log("Building " + archiveType + ": " + cabFile.getAbsolutePath());
    if (!Os.isFamily("windows")) {
        log("Using listcab/libcabinet", Project.MSG_VERBOSE);
        StringBuilder sb = new StringBuilder();
        files.forEach(f -> sb.append(f).append("\n"));
        sb.append("\n").append(cabFile.getAbsolutePath()).append("\n");
        try {
            Process p = Execute.launch(getProject(), new String[] { "listcab" }, null, baseDir != null ? baseDir : getProject().getBaseDir(), true);
            OutputStream out = p.getOutputStream();
            // Create the stream pumpers to forward listcab's stdout and stderr to the log
            // note: listcab is an interactive program, and issues prompts for every new line.
            // Therefore, make it show only with verbose logging turned on.
            LogOutputStream outLog = new LogOutputStream(this, Project.MSG_VERBOSE);
            LogOutputStream errLog = new LogOutputStream(this, Project.MSG_ERR);
            StreamPumper outPump = new StreamPumper(p.getInputStream(), outLog);
            StreamPumper errPump = new StreamPumper(p.getErrorStream(), errLog);
            // Pump streams asynchronously
            new Thread(outPump).start();
            new Thread(errPump).start();
            out.write(sb.toString().getBytes());
            out.flush();
            out.close();
            // A wild default for when the thread is interrupted
            int result = DEFAULT_RESULT;
            try {
                // Wait for the process to finish
                result = p.waitFor();
                // Wait for the end of output and error streams
                outPump.waitFor();
                outLog.close();
                errPump.waitFor();
                errLog.close();
            } catch (InterruptedException ie) {
                log("Thread interrupted: " + ie);
            }
            // Informative summary message in case of errors
            if (Execute.isFailure(result)) {
                log("Error executing listcab; error code: " + result);
            }
        } catch (IOException ex) {
            throw new BuildException("Problem creating " + cabFile + " " + ex.getMessage(), getLocation());
        }
    } else {
        try {
            File listFile = createListFile(files);
            ExecTask exec = createExec();
            File outFile = null;
            // die if cabarc fails
            exec.setFailonerror(true);
            exec.setDir(baseDir);
            if (!doVerbose) {
                outFile = FILE_UTILS.createTempFile("ant", "", null, true, true);
                exec.setOutput(outFile);
            }
            exec.setExecutable("cabarc");
            exec.createArg().setValue("-r");
            exec.createArg().setValue("-p");
            if (!doCompress) {
                exec.createArg().setValue("-m");
                exec.createArg().setValue("none");
            }
            if (cmdOptions != null) {
                exec.createArg().setLine(cmdOptions);
            }
            exec.createArg().setValue("n");
            exec.createArg().setFile(cabFile);
            exec.createArg().setValue("@" + listFile.getAbsolutePath());
            exec.execute();
            if (outFile != null) {
                outFile.delete();
            }
            listFile.delete();
        } catch (IOException ioe) {
            throw new BuildException("Problem creating " + cabFile + " " + ioe.getMessage(), getLocation());
        }
    }
}
Also used : OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) ExecTask(org.apache.tools.ant.taskdefs.ExecTask) IOException(java.io.IOException) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) StreamPumper(org.apache.tools.ant.taskdefs.StreamPumper) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 8 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class SunRmic method execute.

/**
 * Run the rmic compiler.
 * @return true if the compilation succeeded
 * @throws BuildException on error
 */
@Override
public boolean execute() throws BuildException {
    getRmic().log("Using SUN rmic compiler", Project.MSG_VERBOSE);
    Commandline cmd = setupRmicCommand();
    // Create an instance of the rmic, redirecting output to
    // the project log
    LogOutputStream logstr = new LogOutputStream(getRmic(), Project.MSG_WARN);
    boolean success = false;
    try {
        Class<?> c = Class.forName(RMIC_CLASSNAME);
        Constructor<?> cons = c.getConstructor(OutputStream.class, String.class);
        Object rmic = cons.newInstance(logstr, "rmic");
        Method doRmic = c.getMethod("compile", String[].class);
        boolean ok = Boolean.TRUE.equals(doRmic.invoke(rmic, (Object) cmd.getArguments()));
        success = true;
        return ok;
    } catch (ClassNotFoundException ex) {
        if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)) {
            throw new BuildException(ERROR_NO_RMIC_ON_CLASSPATH_JAVA_9, getRmic().getLocation());
        }
        throw new BuildException(ERROR_NO_RMIC_ON_CLASSPATH, getRmic().getLocation());
    } catch (Exception ex) {
        if (ex instanceof BuildException) {
            throw (BuildException) ex;
        }
        throw new BuildException(ERROR_RMIC_FAILED, ex, getRmic().getLocation());
    } finally {
        try {
            logstr.close();
        } catch (IOException e) {
            // original error will be passed up
            if (success) {
                // NOSONAR
                throw new BuildException(e);
            }
        }
    }
}
Also used : Commandline(org.apache.tools.ant.types.Commandline) Method(java.lang.reflect.Method) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 9 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project checkstyle by checkstyle.

the class CheckstyleAntTask method getListeners.

/**
 * Return the list of listeners set in this task.
 *
 * @return the list of listeners.
 * @throws BuildException if the listeners could not be created.
 */
private AuditListener[] getListeners() {
    final int formatterCount = Math.max(1, formatters.size());
    final AuditListener[] listeners = new AuditListener[formatterCount];
    // formatters
    try {
        if (formatters.isEmpty()) {
            final OutputStream debug = new LogOutputStream(this, Project.MSG_DEBUG);
            final OutputStream err = new LogOutputStream(this, Project.MSG_ERR);
            listeners[0] = new DefaultLogger(debug, AutomaticBean.OutputStreamOptions.CLOSE, err, AutomaticBean.OutputStreamOptions.CLOSE);
        } else {
            for (int i = 0; i < formatterCount; i++) {
                final Formatter formatter = formatters.get(i);
                listeners[i] = formatter.createListener(this);
            }
        }
    } catch (IOException ex) {
        throw new BuildException(String.format(Locale.ROOT, "Unable to create listeners: " + "formatters {%s}.", formatters), ex);
    }
    return listeners;
}
Also used : LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream)

Aggregations

LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)9 IOException (java.io.IOException)8 BuildException (org.apache.tools.ant.BuildException)8 OutputStream (java.io.OutputStream)5 File (java.io.File)4 Commandline (org.apache.tools.ant.types.Commandline)4 PrintStream (java.io.PrintStream)3 PumpStreamHandler (org.apache.tools.ant.taskdefs.PumpStreamHandler)3 FileNotFoundException (java.io.FileNotFoundException)2 Method (java.lang.reflect.Method)2 Execute (org.apache.tools.ant.taskdefs.Execute)2 LogStreamHandler (org.apache.tools.ant.taskdefs.LogStreamHandler)2 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)1 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1