On 01/03/2013, at 11:41 AM, alex.ruiz.05 wrote:

> I got it working by using an embedded daemon. Doing all the wiring required
> used of reflection though. The key was to override some functionality in the
> creation of the GradleConnector.
> 
> Here is the code:
> 
>  private static GradleConnector getGradleConnector() {
>    ToolingImplementationLoader delegate = new
> DefaultToolingImplementationLoader();
>    final SynchronizedToolingImplementationLoader loader = new
> SynchronizedToolingImplementationLoader(delegate);
>    final ProtocolToModelAdapter adapter = new ProtocolToModelAdapter();
>    final DefaultExecutorFactory executorFactory = new
> DefaultExecutorFactory();
>    ConnectionFactory connectionFactory = new ConnectionFactory(loader) {
>      @Override
>      public ProjectConnection create(Distribution distribution,
> ConnectionParameters parameters) {
>        try {
>          Field field =
> DefaultConnectionParameters.class.getDeclaredField("embedded");
>          field.setAccessible(true);
>          field.set(parameters, Boolean.TRUE);
>        } catch (Throwable e) {
>          throw new RuntimeException(e);
>        }
>        SynchronizedLogging synchronizedLogging = new SynchronizedLogging();
>        ConsumerConnection lazyConnection = new LazyConnection(distribution,
> loader, synchronizedLogging, parameters.getVerboseLogging());
>        ConsumerConnection progressLoggingConnection = new
> ProgressLoggingConnection(lazyConnection, synchronizedLogging);
>        final ConsumerConnection initializingConnection = new
> LoggingInitializerConnection(progressLoggingConnection,
> synchronizedLogging);
>        AsyncConnection asyncConnection = new
> DefaultAsyncConnection(initializingConnection, executorFactory) {
>          @Override
>          public <T> void run(Class<T> type, ConsumerOperationParameters
> operationParameters, ResultHandlerVersion1<? super T> handler) throws
> UnsupportedOperationException, IllegalStateException {
>            System.out.println(operationParameters.isEmbedded());
>            super.run(type, operationParameters, handler);
>            // initializingConnection.run(type, operationParameters);
>          }
>        };
>        try {
>          Class<?> type =
> Class.forName("org.gradle.tooling.internal.consumer.DefaultProjectConnection");
>          Constructor<?> constructor =
>              type.getConstructor(AsyncConnection.class,
> ProtocolToModelAdapter.class, ConnectionParameters.class);
>          constructor.setAccessible(true);
>          return (ProjectConnection)
> constructor.newInstance(asyncConnection, adapter, parameters);
>        } catch (Throwable e) {
>          throw new RuntimeException(e);
>        }
>      }
>    };
>    DistributionFactory distributionFactory = new DistributionFactory(new
> File("/Users/alruiz"));
>    return new DefaultGradleConnector(connectionFactory,
> distributionFactory);
>  }
> 
> Is there a better/easier way to do this?

You can just cast GradleConnector to DefaultGradleConnector:

DefaultGradleConnector connector = 
(DefaultGradleConnector)GradleConnector.newConnector();
connector.embedded(true);
//... configure connector as you would normally ...
ProjectConnection connection = connector.connect();


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Reply via email to