Search in sources :

Example 6 with ProjectConnection

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);
}
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 7 with ProjectConnection

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);
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) ProjectConnection(org.gradle.tooling.ProjectConnection) File(java.io.File) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) GradleConnector(org.gradle.tooling.GradleConnector)

Example 8 with ProjectConnection

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();
}
Also used : ProjectConnection(org.gradle.tooling.ProjectConnection) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 9 with ProjectConnection

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();
}
Also used : ProjectConnection(org.gradle.tooling.ProjectConnection) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 10 with ProjectConnection

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);
}
Also used : ProjectConnection(org.gradle.tooling.ProjectConnection) Test(org.junit.Test)

Aggregations

ProjectConnection (org.gradle.tooling.ProjectConnection)15 File (java.io.File)10 GradleConnector (org.gradle.tooling.GradleConnector)10 DefaultGradleConnector (org.gradle.tooling.internal.consumer.DefaultGradleConnector)6 BuildLauncher (org.gradle.tooling.BuildLauncher)4 Test (org.junit.Test)4 JarFile (java.util.jar.JarFile)3 GradleVersion (org.gradle.util.GradleVersion)3 Nullable (org.jetbrains.annotations.Nullable)3 AndroidProject (com.android.builder.model.AndroidProject)2 ExternalSystemTaskId (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId)2 ExternalSystemTaskNotificationListener (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener)2 Function (com.intellij.util.Function)2 List (java.util.List)2 CancellationTokenSource (org.gradle.tooling.CancellationTokenSource)2 NotNull (org.jetbrains.annotations.NotNull)2 GradleExecutionHelper (org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper)2 GradleExecutionSettings (org.jetbrains.plugins.gradle.settings.GradleExecutionSettings)2 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 CommandLineArgs (com.android.tools.idea.gradle.project.sync.common.CommandLineArgs)1