use of org.gradle.tooling.ProjectConnection 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);
}
use of org.gradle.tooling.ProjectConnection in project spring-boot by spring-projects.
the class GradleIT method test.
private void test(String name, String expected) throws Exception {
File projectDirectory = new File("target/gradleit/" + name);
File javaDirectory = new File("target/gradleit/" + name + "/src/main/java/org/test/");
projectDirectory.mkdirs();
javaDirectory.mkdirs();
File script = new File(projectDirectory, "build.gradle");
FileCopyUtils.copy(new File("src/it/" + name + "/build.gradle"), script);
FileCopyUtils.copy(new File("src/it/" + name + "/src/main/java/org/test/SampleApplication.java"), new File(javaDirectory, "SampleApplication.java"));
GradleConnector gradleConnector = GradleConnector.newConnector();
gradleConnector.useGradleVersion("2.9");
((DefaultGradleConnector) gradleConnector).embedded(true);
ProjectConnection project = gradleConnector.forProjectDirectory(projectDirectory).connect();
project.newBuild().forTasks("clean", "build").setStandardOutput(System.out).setStandardError(System.err).withArguments("-PbootVersion=" + getBootVersion()).run();
Verify.verify(new File("target/gradleit/" + name + "/build/libs/" + name + ".jar"), expected);
}
use of org.gradle.tooling.ProjectConnection in project spring-boot by spring-projects.
the class MultiProjectRepackagingTests method repackageWithCommonFileDependency.
@Test
public void repackageWithCommonFileDependency() throws Exception {
ProjectConnection project = new ProjectCreator().createProject("multi-project-common-file-dependency");
project.newBuild().forTasks("clean", "build").withArguments("-PbootVersion=" + BOOT_VERSION).run();
File buildLibs = new File("target/multi-project-common-file-dependency/build/libs");
JarFile jarFile = new JarFile(new File(buildLibs, "multi-project-common-file-dependency.jar"));
assertThat(jarFile.getEntry("BOOT-INF/lib/foo.jar")).isNotNull();
jarFile.close();
}
use of org.gradle.tooling.ProjectConnection in project spring-boot by spring-projects.
the class MultiProjectRepackagingTests method repackageWithTransitiveFileDependency.
@Test
public void repackageWithTransitiveFileDependency() throws Exception {
ProjectConnection project = new ProjectCreator().createProject("multi-project-transitive-file-dependency");
project.newBuild().forTasks("clean", "build").withArguments("-PbootVersion=" + BOOT_VERSION).run();
File buildLibs = new File("target/multi-project-transitive-file-dependency/main/build/libs");
JarFile jarFile = new JarFile(new File(buildLibs, "main.jar"));
assertThat(jarFile.getEntry("BOOT-INF/lib/commons-logging-1.1.3.jar")).isNotNull();
assertThat(jarFile.getEntry("BOOT-INF/lib/foo.jar")).isNotNull();
jarFile.close();
}
use of org.gradle.tooling.ProjectConnection in project spring-boot by spring-projects.
the class SpringLoadedTests method defaultJvmArgsArePreservedWhenLoadedAgentIsConfigured.
@Test
public void defaultJvmArgsArePreservedWhenLoadedAgentIsConfigured() throws IOException {
ProjectConnection project = new ProjectCreator().createProject("spring-loaded-jvm-args");
project.newBuild().forTasks("bootRun").withArguments("-PbootVersion=" + BOOT_VERSION, "-PspringLoadedVersion=" + SPRING_LOADED_VERSION, "--stacktrace").run();
List<String> output = getOutput();
assertOutputContains("-DSOME_ARG=someValue", output);
assertOutputContains("-Xverify:none", output);
assertOutputMatches("-javaagent:.*springloaded-" + SPRING_LOADED_VERSION + ".jar", output);
}
Aggregations