Search in sources :

Example 1 with ExecResult

use of org.gradle.process.ExecResult in project gradle by gradle.

the class ForkingGradleHandle method waitForStop.

protected ExecutionResult waitForStop(boolean expectFailure) {
    ExecHandle execHandle = getExecHandle();
    ExecResult execResult = execHandle.waitForFinish();
    if (durationMeasurement != null) {
        durationMeasurement.stop();
    }
    // nop if all ok
    execResult.rethrowFailure();
    String output = getStandardOutput();
    String error = getErrorOutput();
    boolean didFail = execResult.getExitValue() != 0;
    ExecutionResult executionResult = expectFailure ? toExecutionFailure(output, error) : toExecutionResult(output, error);
    if (didFail != expectFailure) {
        String message = String.format("Gradle execution %s in %s with: %s %s%nOutput:%n%s%n-----%nError:%n%s%n-----%nExecution result:%n%s%n-----%n", expectFailure ? "did not fail" : "failed", execHandle.getDirectory(), execHandle.getCommand(), execHandle.getArguments(), output, error, execResult.toString());
        Exception exception = executionResult instanceof OutputScrapingExecutionFailure ? ((OutputScrapingExecutionFailure) executionResult).getException() : null;
        throw exception == null ? new UnexpectedBuildFailure(message) : new UnexpectedBuildFailure(message, exception);
    }
    resultAssertion.execute(executionResult);
    return executionResult;
}
Also used : IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ExecHandle(org.gradle.process.internal.ExecHandle) ExecResult(org.gradle.process.ExecResult)

Example 2 with ExecResult

use of org.gradle.process.ExecResult in project gradle by gradle.

the class JavaInstallationProbe method getMetadataInternal.

private EnumMap<SysProp, String> getMetadataInternal(File jdkPath) {
    JavaExecAction exec = factory.newJavaExecAction();
    exec.executable(javaExe(jdkPath, "java"));
    File workingDir = Files.createTempDir();
    exec.setWorkingDir(workingDir);
    exec.setClasspath(new SimpleFileCollection(workingDir));
    try {
        writeProbe(workingDir);
        exec.setMain(JavaProbe.CLASSNAME);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        exec.setStandardOutput(baos);
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        exec.setErrorOutput(errorOutput);
        exec.setIgnoreExitValue(true);
        ExecResult result = exec.execute();
        int exitValue = result.getExitValue();
        if (exitValue == 0) {
            return parseExecOutput(baos.toString());
        }
        return error("Command returned unexpected result code: " + exitValue + "\nError output:\n" + errorOutput);
    } catch (ExecException ex) {
        return error(ex.getMessage());
    } finally {
        try {
            FileUtils.deleteDirectory(workingDir);
        } catch (IOException e) {
            throw new GradleException("Unable to delete temp directory", e);
        }
    }
}
Also used : JavaExecAction(org.gradle.process.internal.JavaExecAction) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) ExecException(org.gradle.process.internal.ExecException) GradleException(org.gradle.api.GradleException) ExecResult(org.gradle.process.ExecResult)

Example 3 with ExecResult

use of org.gradle.process.ExecResult in project gradle by gradle.

the class GccVersionDeterminer method transform.

private String transform(File gccBinary, List<String> args) {
    ExecAction exec = execActionFactory.newExecAction();
    exec.executable(gccBinary.getAbsolutePath());
    exec.setWorkingDir(gccBinary.getParentFile());
    exec.args(args);
    StreamByteBuffer buffer = new StreamByteBuffer();
    exec.setStandardOutput(buffer.getOutputStream());
    exec.setErrorOutput(NullOutputStream.INSTANCE);
    exec.setIgnoreExitValue(true);
    ExecResult result = exec.execute();
    int exitValue = result.getExitValue();
    if (exitValue == 0) {
        return buffer.readAsString();
    } else {
        return null;
    }
}
Also used : ExecAction(org.gradle.process.internal.ExecAction) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) ExecResult(org.gradle.process.ExecResult)

Example 4 with ExecResult

use of org.gradle.process.ExecResult in project gradle by gradle.

the class CommandLineJavaCompiler method executeCompiler.

private void executeCompiler(ExecHandle handle) {
    handle.start();
    ExecResult result = handle.waitForFinish();
    if (result.getExitValue() != 0) {
        throw new CompilationFailedException(result.getExitValue());
    }
}
Also used : ExecResult(org.gradle.process.ExecResult)

Example 5 with ExecResult

use of org.gradle.process.ExecResult in project gradle by gradle.

the class ProcessLauncherServer method launchExternalProcess.

/**
     * This launches an external process in a thread and waits for it to exit.
     */
private void launchExternalProcess() {
    Thread thread = new Thread(new Runnable() {

        public void run() {
            ExecutionInfo executionInfo = null;
            ExecHandle execHandle = null;
            ByteArrayOutputStream output = null;
            try {
                executionInfo = protocol.getExecutionInfo(getPort());
                ExecHandleBuilder builder = new DefaultExecHandleBuilder();
                builder.workingDir(executionInfo.getWorkingDirectory());
                builder.commandLine((Object[]) executionInfo.getCommandLineArguments());
                builder.environment(executionInfo.getEnvironmentVariables());
                output = new ByteArrayOutputStream();
                builder.setStandardOutput(output);
                builder.setErrorOutput(output);
                execHandle = builder.build();
                setExternalProcess(execHandle);
                execHandle.start();
            } catch (Throwable e) {
                LOGGER.error("Starting external process", e);
                notifyClientExited(-1, e.getMessage());
                setExternalProcess(null);
                return;
            }
            ExecResult result = execHandle.waitForFinish();
            LOGGER.debug("External process completed with exit code {}", result.getExitValue());
            //clear our external process member variable (we're using our local variable below). This is so we know the process has already stopped.
            setExternalProcess(null);
            executionInfo.processExecutionComplete();
            notifyClientExited(result.getExitValue(), output.toString());
        }
    });
    thread.start();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultExecHandleBuilder(org.gradle.process.internal.DefaultExecHandleBuilder) DefaultExecHandleBuilder(org.gradle.process.internal.DefaultExecHandleBuilder) ExecHandleBuilder(org.gradle.process.internal.ExecHandleBuilder) ExecHandle(org.gradle.process.internal.ExecHandle) ExecResult(org.gradle.process.ExecResult)

Aggregations

ExecResult (org.gradle.process.ExecResult)8 ExecHandle (org.gradle.process.internal.ExecHandle)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 GradleException (org.gradle.api.GradleException)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 SimpleFileCollection (org.gradle.api.internal.file.collections.SimpleFileCollection)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1 DefaultExecHandleBuilder (org.gradle.process.internal.DefaultExecHandleBuilder)1 ExecAction (org.gradle.process.internal.ExecAction)1 ExecException (org.gradle.process.internal.ExecException)1 ExecHandleBuilder (org.gradle.process.internal.ExecHandleBuilder)1 ExecHandleListener (org.gradle.process.internal.ExecHandleListener)1 JavaExecAction (org.gradle.process.internal.JavaExecAction)1