use of org.gradle.tooling.GradleConnector in project gradle by gradle.
the class ToolingApiGradleExecutor method run.
@Override
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 2.6 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())));
if (parameters.getStandardInput() != null) {
launcher.setStandardInput(parameters.getStandardInput());
}
launcher.addProgressListener(new TaskExecutionProgressListener(tasks), OperationType.TASK);
launcher.withArguments(parameters.getBuildArgs().toArray(new String[0]));
launcher.setJvmArguments(parameters.getJvmArgs().toArray(new String[0]));
launcher.setEnvironmentVariables(parameters.getEnvironment());
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);
}
use of org.gradle.tooling.GradleConnector in project paraphrase by JakeWharton.
the class ToolingApiGradleHandleFactory method start.
public GradleHandle start(File directory, List<String> arguments) {
GradleConnector connector = GradleConnector.newConnector();
connector.forProjectDirectory(directory);
ProjectConnection connection = connector.connect();
BuildLauncher launcher = connection.newBuild();
String[] argumentArray = new String[arguments.size()];
arguments.toArray(argumentArray);
launcher.withArguments(argumentArray);
return new BuildLauncherBackedGradleHandle(launcher);
}
use of org.gradle.tooling.GradleConnector in project sts4 by spring-projects.
the class GradleCore method getModel.
public <T> T getModel(File projectDir, Class<T> modelType) throws GradleException {
ProjectConnection connection = null;
try {
GradleConnector gradleConnector = GradleConnector.newConnector().forProjectDirectory(projectDir);
/*
* Shut down Gradle daemons right away. Necessary project data is
* queried once and then cached, hence no need need to have the
* daemon running
*/
((DefaultGradleConnector) gradleConnector).daemonMaxIdleTime(1, TimeUnit.SECONDS);
configuration.configure(gradleConnector);
// Use patched Gradle 4.4 distribution or higher as a workaround for https://github.com/gradle/gradle/issues/2483
gradleConnector.useGradleVersion("4.6");
connection = gradleConnector.connect();
return connection.getModel(modelType);
} catch (GradleConnectionException e) {
Log.log(e);
throw new GradleException(e);
} finally {
if (connection != null) {
connection.close();
}
}
}
Aggregations