org.gradle.tooling
Interface ModelBuilder<T extends Model>

Type Parameters:
T - The type of model to build
All Superinterfaces:
LongRunningOperation

public interface ModelBuilder<T extends Model>
extends LongRunningOperation

A ModelBuilder allows you to fetch a snapshot of the model for a project. Instances of ModelBuilder are not thread-safe.

You use a ModelBuilder as follows:

Example:
 ProjectConnection connection = GradleConnector.newConnector()
    .forProjectDirectory(new File("someFolder"))
    .connect();

 try {
    ModelBuilder<GradleProject> builder = connection.model(GradleProject.class);

    //if you use a different than usual build file name:
    builder.withArguments("--build-file", "theBuild.gradle");

    //configure the standard input in case your build is interactive:
    builder.setStandardInput(new ByteArrayInputStream("consume this!".getBytes()));

    //if you want to listen to the progress events:
    ProgressListener listener = null; // use your implementation
    builder.addProgressListener(listener);

    //get the model:
    GradleProject project = builder.get();

    //query the model for information:
    System.out.println("Available tasks: " + project.getTasks());
 } finally {
    connection.close();
 }
 

Since:
1.0-milestone-3

Method Summary
 ModelBuilder<T> addProgressListener(ProgressListener listener)
          Adds a progress listener which will receive progress events as the operation runs.
 ModelBuilder<T> forTasks(String... tasks)
          Specifies the tasks to execute before building the model.
 T get()
          Fetch the model, blocking until it is available.
 void get(ResultHandler<? super T> handler)
          Starts fetching the build.
 ModelBuilder<T> setJavaHome(File javaHome)
          If the target Gradle version supports it you can use this setting to specify the Java home directory to use for the long running operation.
 ModelBuilder<T> setJvmArguments(String... jvmArguments)
          If the target Gradle version supports it you can use this setting to specify the Java vm arguments to use for the long running operation.
 ModelBuilder<T> setStandardError(OutputStream outputStream)
          Sets the OutputStream which should receive standard error logging generated while running the operation.
 ModelBuilder<T> setStandardInput(InputStream inputStream)
          If the target Gradle version supports it you can use this setting to set the standard InputStream that will be used by builds.
 ModelBuilder<T> setStandardOutput(OutputStream outputStream)
          Sets the OutputStream which should receive standard output logging generated while running the operation.
 ModelBuilder<T> withArguments(String... arguments)
          Specify the command line build arguments.
 

Method Detail

withArguments

ModelBuilder<T> withArguments(String... arguments)
Specify the command line build arguments. Useful mostly for running tasks via BuildLauncher.

Be aware that not all of the Gradle command line options are supported! Only the build arguments that configure the build execution are supported. They are modelled in the Gradle API via StartParameter. Examples of supported build arguments: '--info', '-u', '-p'. The command line instructions that are actually separate commands (like '-?' and '-v') are not supported. Some other instructions like '--daemon' are also not supported - the tooling API always runs with the daemon.

If an unknown or unsupported command line option is specified, UnsupportedBuildArgumentException will be thrown at the time the operation is executed via BuildLauncher.run() or get().

For the list of all Gradle command line options please refer to the user guide or take a look at the output of the 'gradle -?' command. Supported arguments are those modeled by StartParameter.

The arguments can potentially override some other settings you have configured. For example, the project directory or Gradle user home directory that are configured in the GradleConnector. Also, the task names configured by BuildLauncher.forTasks(String...) can be overridden if you happen to specify other tasks via the build arguments.

See the example in the docs for BuildLauncher

Specified by:
withArguments in interface LongRunningOperation
Parameters:
arguments - Gradle command line arguments
Returns:
this
Since:
1.0-rc-1

setStandardOutput

ModelBuilder<T> setStandardOutput(OutputStream outputStream)
Sets the OutputStream which should receive standard output logging generated while running the operation. The default is to discard the output.

Specified by:
setStandardOutput in interface LongRunningOperation
Parameters:
outputStream - The output stream.
Returns:
this
Since:
1.0-milestone-3

setStandardError

ModelBuilder<T> setStandardError(OutputStream outputStream)
Sets the OutputStream which should receive standard error logging generated while running the operation. The default is to discard the output.

Specified by:
setStandardError in interface LongRunningOperation
Parameters:
outputStream - The output stream.
Returns:
this
Since:
1.0-milestone-3

setStandardInput

ModelBuilder<T> setStandardInput(InputStream inputStream)
If the target Gradle version supports it you can use this setting to set the standard InputStream that will be used by builds. Useful when the tooling api drives interactive builds.

If the target Gradle version does not support it the long running operation will fail eagerly with UnsupportedOperationConfigurationException when the operation is started.

If not configured or null passed the dummy input stream with zero bytes is used to avoid the build hanging problems.

Specified by:
setStandardInput in interface LongRunningOperation
Parameters:
inputStream - The input stream
Returns:
this
Since:
1.0-milestone-7

setJavaHome

ModelBuilder<T> setJavaHome(File javaHome)
If the target Gradle version supports it you can use this setting to specify the Java home directory to use for the long running operation.

If the target Gradle version does not support it the long running operation will fail eagerly with UnsupportedOperationConfigurationException when the operation is started.

BuildEnvironment model contains information such as Java or Gradle environment. If you want to get hold of this information you can ask tooling API to build this model.

If not configured or null passed the sensible default will be used.

Specified by:
setJavaHome in interface LongRunningOperation
Parameters:
javaHome - to use for the Gradle process
Returns:
this
Since:
1.0-milestone-8

setJvmArguments

ModelBuilder<T> setJvmArguments(String... jvmArguments)
If the target Gradle version supports it you can use this setting to specify the Java vm arguments to use for the long running operation.

If the target Gradle version does not support it the long running operation will fail eagerly with UnsupportedOperationConfigurationException when the operation is started.

BuildEnvironment model contains information such as Java or Gradle environment. If you want to get hold of this information you can ask tooling API to build this model.

If not configured, null an empty array passed then the reasonable default will be used.

Specified by:
setJvmArguments in interface LongRunningOperation
Parameters:
jvmArguments - to use for the Gradle process
Returns:
this
Since:
1.0-milestone-9

addProgressListener

ModelBuilder<T> addProgressListener(ProgressListener listener)
Adds a progress listener which will receive progress events as the operation runs.

Specified by:
addProgressListener in interface LongRunningOperation
Parameters:
listener - The listener
Returns:
this
Since:
1.0-milestone-3

forTasks

@Incubating
ModelBuilder<T> forTasks(String... tasks)
Specifies the tasks to execute before building the model. By default, no tasks are executed.

Parameters:
tasks - The paths of the tasks to be executed. Relative paths are evaluated relative to the project for which this launcher was created.
Returns:
this
Since:
1.2

get

T get()
                    throws GradleConnectionException
Fetch the model, blocking until it is available.

Returns:
The model.
Throws:
UnsupportedVersionException - When the target Gradle version does not support the features required to build this model.
UnsupportedOperationConfigurationException - when you have configured the long running operation with a settings like: setStandardInput(java.io.InputStream), setJavaHome(java.io.File), setJvmArguments(String...) but those settings are not supported on the target Gradle.
BuildException - On some failure executing the Gradle build.
GradleConnectionException - On some other failure using the connection.
IllegalStateException - When the connection has been closed or is closing.
Since:
1.0-milestone-3

get

void get(ResultHandler<? super T> handler)
         throws IllegalStateException
Starts fetching the build. This method returns immediately, and the result is later passed to the given handler.

Parameters:
handler - The handler to supply the result to.
Throws:
IllegalStateException - When the connection has been closed or is closing.
Since:
1.0-milestone-3