Search in sources :

Example 1 with ExecuteStreamHandler

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

the class Rpm method execute.

/**
 * Execute the task
 *
 * @throws BuildException is there is a problem in the task execution.
 */
@Override
public void execute() throws BuildException {
    Commandline toExecute = new Commandline();
    toExecute.setExecutable(rpmBuildCommand == null ? guessRpmBuildCommand() : rpmBuildCommand);
    if (topDir != null) {
        toExecute.createArgument().setValue("--define");
        toExecute.createArgument().setValue("_topdir " + topDir);
    }
    toExecute.createArgument().setLine(command);
    if (cleanBuildDir) {
        toExecute.createArgument().setValue("--clean");
    }
    if (removeSpec) {
        toExecute.createArgument().setValue("--rmspec");
    }
    if (removeSource) {
        toExecute.createArgument().setValue("--rmsource");
    }
    toExecute.createArgument().setValue("SPECS/" + specFile);
    ExecuteStreamHandler streamhandler = null;
    OutputStream outputstream = null;
    OutputStream errorstream = null;
    if (error == null && output == null) {
        if (!quiet) {
            streamhandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
        } else {
            streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG, Project.MSG_DEBUG);
        }
    } else {
        if (output != null) {
            OutputStream fos = null;
            try {
                // NOSONAR
                fos = Files.newOutputStream(output.toPath());
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                outputstream = new PrintStream(bos);
            } catch (IOException e) {
                FileUtils.close(fos);
                throw new BuildException(e, getLocation());
            }
        } else if (!quiet) {
            outputstream = new LogOutputStream(this, Project.MSG_INFO);
        } else {
            outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
        }
        if (error != null) {
            OutputStream fos = null;
            try {
                fos = Files.newOutputStream(error.toPath());
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                errorstream = new PrintStream(bos);
            } catch (IOException e) {
                FileUtils.close(fos);
                throw new BuildException(e, getLocation());
            }
        } else if (!quiet) {
            errorstream = new LogOutputStream(this, Project.MSG_WARN);
        } else {
            errorstream = new LogOutputStream(this, Project.MSG_DEBUG);
        }
        streamhandler = new PumpStreamHandler(outputstream, errorstream);
    }
    Execute exe = getExecute(toExecute, streamhandler);
    try {
        log("Building the RPM based on the " + specFile + " file");
        int returncode = exe.execute();
        if (Execute.isFailure(returncode)) {
            String msg = "'" + toExecute.getExecutable() + "' failed with exit code " + returncode;
            if (failOnError) {
                throw new BuildException(msg);
            }
            log(msg, Project.MSG_ERR);
        }
    } catch (IOException e) {
        throw new BuildException(e, getLocation());
    } finally {
        FileUtils.close(outputstream);
        FileUtils.close(errorstream);
    }
}
Also used : PrintStream(java.io.PrintStream) Commandline(org.apache.tools.ant.types.Commandline) Execute(org.apache.tools.ant.taskdefs.Execute) ExecuteStreamHandler(org.apache.tools.ant.taskdefs.ExecuteStreamHandler) OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) LogStreamHandler(org.apache.tools.ant.taskdefs.LogStreamHandler) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) PumpStreamHandler(org.apache.tools.ant.taskdefs.PumpStreamHandler) BuildException(org.apache.tools.ant.BuildException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 BuildException (org.apache.tools.ant.BuildException)1 Execute (org.apache.tools.ant.taskdefs.Execute)1 ExecuteStreamHandler (org.apache.tools.ant.taskdefs.ExecuteStreamHandler)1 LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)1 LogStreamHandler (org.apache.tools.ant.taskdefs.LogStreamHandler)1 PumpStreamHandler (org.apache.tools.ant.taskdefs.PumpStreamHandler)1 Commandline (org.apache.tools.ant.types.Commandline)1