Search in sources :

Example 1 with GradleConnectionException

use of org.gradle.tooling.GradleConnectionException in project gradle by gradle.

the class ToolingApiGradleExecutor method run.

public GradleExecutionResult run(GradleExecutionParameters parameters) {
    final StreamByteBuffer outputBuffer = new StreamByteBuffer();
    final OutputStream syncOutput = new SynchronizedOutputStream(outputBuffer.getOutputStream());
    final List<BuildTask> tasks = new ArrayList<BuildTask>();
    maybeRegisterCleanup();
    GradleConnector gradleConnector = buildConnector(parameters.getGradleUserHome(), parameters.getProjectDir(), parameters.isEmbedded(), parameters.getGradleProvider());
    ProjectConnection connection = null;
    GradleVersion targetGradleVersion = null;
    try {
        connection = gradleConnector.connect();
        targetGradleVersion = determineTargetGradleVersion(connection);
        if (targetGradleVersion.compareTo(TestKitFeature.RUN_BUILDS.getSince()) < 0) {
            throw new UnsupportedFeatureException(String.format("The version of Gradle you are using (%s) is not supported by TestKit. TestKit supports all Gradle versions 1.2 and later.", targetGradleVersion.getVersion()));
        }
        DefaultBuildLauncher launcher = (DefaultBuildLauncher) connection.newBuild();
        launcher.setStandardOutput(new NoCloseOutputStream(teeOutput(syncOutput, parameters.getStandardOutput())));
        launcher.setStandardError(new NoCloseOutputStream(teeOutput(syncOutput, parameters.getStandardError())));
        launcher.addProgressListener(new TaskExecutionProgressListener(tasks), OperationType.TASK);
        launcher.withArguments(parameters.getBuildArgs().toArray(new String[0]));
        launcher.setJvmArguments(parameters.getJvmArgs().toArray(new String[0]));
        if (!parameters.getInjectedClassPath().isEmpty()) {
            if (targetGradleVersion.compareTo(TestKitFeature.PLUGIN_CLASSPATH_INJECTION.getSince()) < 0) {
                throw new UnsupportedFeatureException("support plugin classpath injection", targetGradleVersion, TestKitFeature.PLUGIN_CLASSPATH_INJECTION.getSince());
            }
            launcher.withInjectedClassPath(parameters.getInjectedClassPath());
        }
        launcher.run();
    } catch (UnsupportedVersionException e) {
        throw new InvalidRunnerConfigurationException("The build could not be executed due to a feature not being supported by the target Gradle version", e);
    } catch (BuildException t) {
        return new GradleExecutionResult(new BuildOperationParameters(targetGradleVersion, parameters.isEmbedded()), outputBuffer.readAsString(), tasks, t);
    } catch (GradleConnectionException t) {
        StringBuilder message = new StringBuilder("An error occurred executing build with ");
        if (parameters.getBuildArgs().isEmpty()) {
            message.append("no args");
        } else {
            message.append("args '");
            message.append(CollectionUtils.join(" ", parameters.getBuildArgs()));
            message.append("'");
        }
        message.append(" in directory '").append(parameters.getProjectDir().getAbsolutePath()).append("'");
        String capturedOutput = outputBuffer.readAsString();
        if (!capturedOutput.isEmpty()) {
            message.append(". Output before error:").append(SystemProperties.getInstance().getLineSeparator()).append(capturedOutput);
        }
        throw new IllegalStateException(message.toString(), t);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return new GradleExecutionResult(new BuildOperationParameters(targetGradleVersion, parameters.isEmbedded()), outputBuffer.readAsString(), tasks);
}
Also used : BuildTask(org.gradle.testkit.runner.BuildTask) UnsupportedFeatureException(org.gradle.testkit.runner.UnsupportedFeatureException) GradleConnectionException(org.gradle.tooling.GradleConnectionException) SynchronizedOutputStream(org.gradle.testkit.runner.internal.io.SynchronizedOutputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) NoCloseOutputStream(org.gradle.testkit.runner.internal.io.NoCloseOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ProjectConnection(org.gradle.tooling.ProjectConnection) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) InvalidRunnerConfigurationException(org.gradle.testkit.runner.InvalidRunnerConfigurationException) DefaultBuildLauncher(org.gradle.tooling.internal.consumer.DefaultBuildLauncher) GradleConnector(org.gradle.tooling.GradleConnector) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) SynchronizedOutputStream(org.gradle.testkit.runner.internal.io.SynchronizedOutputStream) BuildException(org.gradle.tooling.BuildException) GradleVersion(org.gradle.util.GradleVersion) NoCloseOutputStream(org.gradle.testkit.runner.internal.io.NoCloseOutputStream) UnsupportedVersionException(org.gradle.tooling.UnsupportedVersionException)

Example 2 with GradleConnectionException

use of org.gradle.tooling.GradleConnectionException in project gradle by gradle.

the class DefaultToolingImplementationLoader method create.

public ConsumerConnection create(Distribution distribution, ProgressLoggerFactory progressLoggerFactory, InternalBuildProgressListener progressListener, ConnectionParameters connectionParameters, BuildCancellationToken cancellationToken) {
    LOGGER.debug("Using tooling provider from {}", distribution.getDisplayName());
    ClassLoader serviceClassLoader = createImplementationClassLoader(distribution, progressLoggerFactory, progressListener, connectionParameters.getGradleUserHomeDir(), cancellationToken);
    ServiceLocator serviceLocator = new DefaultServiceLocator(serviceClassLoader);
    try {
        Factory<ConnectionVersion4> factory = serviceLocator.findFactory(ConnectionVersion4.class);
        if (factory == null) {
            return new NoToolingApiConnection(distribution);
        }
        // ConnectionVersion4 is a part of the protocol and cannot be easily changed.
        ConnectionVersion4 connection = factory.create();
        ProtocolToModelAdapter adapter = new ProtocolToModelAdapter(new ConsumerTargetTypeProvider());
        ModelMapping modelMapping = new ModelMapping();
        // Adopting the connection to a refactoring friendly type that the consumer owns
        AbstractConsumerConnection adaptedConnection;
        if (connection instanceof InternalTestExecutionConnection) {
            adaptedConnection = new TestExecutionConsumerConnection(connection, modelMapping, adapter);
        } else if (connection instanceof StoppableConnection) {
            adaptedConnection = new ShutdownAwareConsumerConnection(connection, modelMapping, adapter);
        } else if (connection instanceof InternalCancellableConnection) {
            adaptedConnection = new CancellableConsumerConnection(connection, modelMapping, adapter);
        } else if (connection instanceof ModelBuilder && connection instanceof InternalBuildActionExecutor) {
            adaptedConnection = new ActionAwareConsumerConnection(connection, modelMapping, adapter);
        } else if (connection instanceof ModelBuilder) {
            adaptedConnection = new ModelBuilderBackedConsumerConnection(connection, modelMapping, adapter);
        } else if (connection instanceof BuildActionRunner) {
            adaptedConnection = new BuildActionRunnerBackedConsumerConnection(connection, modelMapping, adapter);
        } else {
            return new UnsupportedOlderVersionConnection(connection, adapter);
        }
        adaptedConnection.configure(connectionParameters);
        if (!adaptedConnection.getVersionDetails().supportsCancellation()) {
            return new ParameterValidatingConsumerConnection(adaptedConnection.getVersionDetails(), new NonCancellableConsumerConnectionAdapter(adaptedConnection));
        }
        return new ParameterValidatingConsumerConnection(adaptedConnection.getVersionDetails(), adaptedConnection);
    } catch (UnsupportedVersionException e) {
        throw e;
    } catch (Throwable t) {
        throw new GradleConnectionException(String.format("Could not create an instance of Tooling API implementation using the specified %s.", distribution.getDisplayName()), t);
    }
}
Also used : NoToolingApiConnection(org.gradle.tooling.internal.consumer.connection.NoToolingApiConnection) ShutdownAwareConsumerConnection(org.gradle.tooling.internal.consumer.connection.ShutdownAwareConsumerConnection) UnsupportedOlderVersionConnection(org.gradle.tooling.internal.consumer.connection.UnsupportedOlderVersionConnection) TestExecutionConsumerConnection(org.gradle.tooling.internal.consumer.connection.TestExecutionConsumerConnection) DefaultServiceLocator(org.gradle.internal.service.DefaultServiceLocator) BuildActionRunner(org.gradle.tooling.internal.protocol.BuildActionRunner) ModelBuilder(org.gradle.tooling.internal.protocol.ModelBuilder) ProtocolToModelAdapter(org.gradle.tooling.internal.adapter.ProtocolToModelAdapter) CancellableConsumerConnection(org.gradle.tooling.internal.consumer.connection.CancellableConsumerConnection) ParameterValidatingConsumerConnection(org.gradle.tooling.internal.consumer.connection.ParameterValidatingConsumerConnection) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) BuildActionRunnerBackedConsumerConnection(org.gradle.tooling.internal.consumer.connection.BuildActionRunnerBackedConsumerConnection) InternalTestExecutionConnection(org.gradle.tooling.internal.protocol.test.InternalTestExecutionConnection) NonCancellableConsumerConnectionAdapter(org.gradle.tooling.internal.consumer.connection.NonCancellableConsumerConnectionAdapter) ConsumerTargetTypeProvider(org.gradle.tooling.internal.consumer.converters.ConsumerTargetTypeProvider) StoppableConnection(org.gradle.tooling.internal.protocol.StoppableConnection) ActionAwareConsumerConnection(org.gradle.tooling.internal.consumer.connection.ActionAwareConsumerConnection) GradleConnectionException(org.gradle.tooling.GradleConnectionException) ModelBuilderBackedConsumerConnection(org.gradle.tooling.internal.consumer.connection.ModelBuilderBackedConsumerConnection) InternalCancellableConnection(org.gradle.tooling.internal.protocol.InternalCancellableConnection) ModelMapping(org.gradle.tooling.internal.consumer.versioning.ModelMapping) InternalBuildActionExecutor(org.gradle.tooling.internal.protocol.InternalBuildActionExecutor) DefaultServiceLocator(org.gradle.internal.service.DefaultServiceLocator) ServiceLocator(org.gradle.internal.service.ServiceLocator) ConnectionVersion4(org.gradle.tooling.internal.protocol.ConnectionVersion4) AbstractConsumerConnection(org.gradle.tooling.internal.consumer.connection.AbstractConsumerConnection) UnsupportedVersionException(org.gradle.tooling.UnsupportedVersionException)

Aggregations

GradleConnectionException (org.gradle.tooling.GradleConnectionException)2 UnsupportedVersionException (org.gradle.tooling.UnsupportedVersionException)2 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)1 FilteringClassLoader (org.gradle.internal.classloader.FilteringClassLoader)1 VisitableURLClassLoader (org.gradle.internal.classloader.VisitableURLClassLoader)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1 DefaultServiceLocator (org.gradle.internal.service.DefaultServiceLocator)1 ServiceLocator (org.gradle.internal.service.ServiceLocator)1 BuildTask (org.gradle.testkit.runner.BuildTask)1 InvalidRunnerConfigurationException (org.gradle.testkit.runner.InvalidRunnerConfigurationException)1 UnsupportedFeatureException (org.gradle.testkit.runner.UnsupportedFeatureException)1 NoCloseOutputStream (org.gradle.testkit.runner.internal.io.NoCloseOutputStream)1 SynchronizedOutputStream (org.gradle.testkit.runner.internal.io.SynchronizedOutputStream)1 BuildException (org.gradle.tooling.BuildException)1 GradleConnector (org.gradle.tooling.GradleConnector)1 ProjectConnection (org.gradle.tooling.ProjectConnection)1 ProtocolToModelAdapter (org.gradle.tooling.internal.adapter.ProtocolToModelAdapter)1 DefaultBuildLauncher (org.gradle.tooling.internal.consumer.DefaultBuildLauncher)1