api / org.gradle.tooling / ProjectConnection

ProjectConnection

interface ProjectConnection : Closeable

Represents a long-lived connection to a Gradle project. You obtain an instance of a ProjectConnection by using org.gradle.tooling.GradleConnector#connect().

 try (ProjectConnection connection = GradleConnector.newConnector() .forProjectDirectory(new File("someFolder")) .connect()) { //obtain some information from the build BuildEnvironment environment = connection.model(BuildEnvironment.class).get(); //run some tasks connection.newBuild() .forTasks("tasks") .setStandardOutput(System.out) .run(); } 
Thread safety information

All implementations of ProjectConnection are thread-safe, and may be shared by any number of threads.

All notifications from a given ProjectConnection instance are delivered by a single thread at a time. Note, however, that the delivery thread may change over time.

Since
1.0-milestone-3

Functions

action

abstract fun <T : Any> action(buildAction: BuildAction<T>): BuildActionExecuter<T>

Creates an executer which can be used to run the given action when the build has finished. The action is serialized into the build process and executed, then its result is serialized back to the caller.

Requires Gradle 1.8 or later.

abstract fun action(): Builder

Creates a builder for an executer which can be used to run actions in different phases of the build. The actions are serialized into the build process and executed, then its result is serialized back to the caller.

Requires Gradle 4.8 or later.

close

abstract fun close(): Unit

Closes this connection. Blocks until any pending operations are complete. Once this method has returned, no more notifications will be delivered by any threads.

getModel

abstract fun <T : Any> getModel(modelType: Class<T>): T

Fetches a snapshot of the model of the given type for this project. This method blocks until the model is available.

This method is simply a convenience for calling model(modelType).get()

abstract fun <T : Any> getModel(modelType: Class<T>, handler: ResultHandler<in T>): Unit

Starts fetching a snapshot of the given model, passing the result to the given handler when complete. This method returns immediately, and the result is later passed to the given handler's ResultHandler#onComplete(Object) method.

If the operation fails, the handler's ResultHandler#onFailure(GradleConnectionException) method is called with the appropriate exception. See #getModel(Class) for a description of the various exceptions that the operation may fail with.

This method is simply a convenience for calling model(modelType).get(handler)

model

abstract fun <T : Any> model(modelType: Class<T>): ModelBuilder<T>

Creates a builder which can be used to query the model of the given type.

Any of following models types may be available, depending on the version of Gradle being used by the target build:

  • org.gradle.tooling.model.gradle.GradleBuild
  • org.gradle.tooling.model.build.BuildEnvironment
  • org.gradle.tooling.model.GradleProject
  • org.gradle.tooling.model.gradle.BuildInvocations
  • org.gradle.tooling.model.gradle.ProjectPublications
  • org.gradle.tooling.model.idea.IdeaProject
  • org.gradle.tooling.model.idea.BasicIdeaProject
  • org.gradle.tooling.model.eclipse.EclipseProject
  • org.gradle.tooling.model.eclipse.HierarchicalEclipseProject

A build may also expose additional custom tooling models. You can use this method to query these models.

Requires Gradle 1.0-milestone-8 or later.

newBuild

abstract fun newBuild(): BuildLauncher

Creates a launcher which can be used to execute a build.

Requires Gradle 1.0-milestone-8 or later.

newTestLauncher

abstract fun newTestLauncher(): TestLauncher

Creates a test launcher which can be used to execute tests.

Requires Gradle 2.6 or later.