<!--
  ~ Copyright 2011 the original author or authors.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<chapter id="embedding">
    <title>Embedding Gradle</title>

    <section id='sec:embedding_introduction'>
        <title>Introduction to the Tooling API</title>
        <para>The 1.0 milestone 3 release brought a new API called the tooling API,
            which you can use for embedding Gradle into your own custom software. This API allows you to execute and monitor builds,
            and to query Gradle about the details of a build.
            The main audience for this API will be IDEs, CI servers, other UI authors, or integration testing of your Gradle plugins.
            However, it is open for anyone who needs to embed Gradle in their application.
        </para>

        <para>A fundamental characteristic of the tooling API is that it operates in a version independent way.
            This means that you can use the same API to work with different target versions of Gradle.
            The tooling API is Gradle wrapper aware and, by default,
            uses the same target Gradle version as that used by the wrapper-powered project.
        </para>

        <para>Some features that the tooling API provides today:
            <itemizedlist>
                <listitem>You can query Gradle for the details of a build,
                    including the project hierarchy and the project dependencies,
                    external dependencies (including source and Javadoc jars),
                    source directories and tasks of each project.
                </listitem>
                <listitem>You can execute a build and listen to stdout and stderr logging and progress
                    (e.g. the stuff shown in the 'status bar' when you run on the command line).
                </listitem>
                <listitem>Tooling API can download and install the appropriate Gradle version, similar to the wrapper.
                    Bear in mind that the tooling API is wrapper aware so you should not need to configure a Gradle distribution directly.
                </listitem>
                <listitem>The implementation is lightweight, with only a small number of dependencies.
                    It is also a well-behaved library, and makes no assumptions about your classloader structure or logging configuration.
                    This makes the API easy to bundle in your application.
                </listitem>
            </itemizedlist>
        </para>

        <para>In the future we may support other interesting features:
            <itemizedlist>
                <listitem>Performance. The API gives us the opportunity to do lots of caching,
                    static analysis and preemptive work, to make things faster for the user.
                </listitem>
                <listitem>Better progress monitoring and build cancellation.
                    For example, allowing test execution to be monitored.
                </listitem>
                <listitem>Notifications when things in the build change, so that UIs and models can be updated.
                    For example, your Eclipse or IDEA project will update immediately, in the background.
                </listitem>
                <listitem>Validating and prompting for user supplied configuration.
                </listitem>
                <listitem>Prompting for and managing user credentials.
                </listitem>
            </itemizedlist>
        </para>
    </section>

    <section id='sec:embedding_daemon'>
        <title>Tooling API and the Gradle Build Daemon</title>
        <para>Please take a look at <xref linkend="gradle_daemon"/>.
            The Tooling API uses the daemon all the time.  In fact, you cannot officially use the Tooling API without the daemon.
            This means that subsequent calls to the Tooling API, be it model building requests or task executing requests
            can be executed in the same long-living process. <xref linkend="gradle_daemon"/> contains more details about the daemon,
            specifically information on situations when new daemons are forked.
        </para>
    </section>

    <section id='sec:embedding_quickstart'>
        <title>Quickstart</title>
        <para>As the tooling API is an interface for developers, the Javadoc is the main documentation for it.
            This is exactly our intention - we don't expect this chapter to grow very much.
            Instead we will add more code samples and improve the Javadoc documentation.
            The main entry point to the tooling API is the
            <apilink class="org.gradle.tooling.GradleConnector"/>.
            You can navigate from there to find code samples and other instructions.
            Another very important set of resources are the <emphasis>samples</emphasis> that live
            in “<filename>$gradleHome/samples/toolingApi</filename>”.  These samples also specify all of the
            required dependencies for the Tooling API, along with the suggested repositories to obtain the jars from.
        </para>
    </section>

</chapter>
