<!--
  ~ Copyright 2009 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='plugins'>
    <title>Gradle Plugins</title>
    <para>
        Gradle at its core intentionally provides little useful functionality for real world automation. All of the useful
        features, such as the ability to compile Java code for example, are added by <emphasis>plugins</emphasis>.
        Plugins add new tasks (e.g. <apilink class='org.gradle.api.tasks.compile.JavaCompile'/>), domain objects (e.g.
        <apilink class="org.gradle.api.tasks.SourceSet"/>), conventions (e.g. main Java source is located at
        <literal>src/main/java</literal>) as well as extending core objects and objects from other plugins.
    </para>
    <para>
        In this chapter we will discuss how to use plugins and the terminology and concepts surrounding plugins.
    </para>
    <section id='sec:using_plugins'>
        <title>Applying plugins</title>
        <para>
            Plugins are said to be <emphasis>applied</emphasis>, which is done via the <apilink class="org.gradle.api.Project" method="apply(java.util.Map)" /> method.
        </para>
        <sample id="useJavaPlugin" dir="java/quickstart" title="Applying a plugin">
            <sourcefile file="build.gradle" snippet="use-plugin"/>
        </sample>
        <para>
            Plugins advertise a short name for themselves. In the above case, we are using the short name ‘<literal>java</literal>’ to apply
            the <apilink class="org.gradle.api.plugins.JavaPlugin" />.
        </para>
        <para>
            We could also have used the following syntax:
        </para>
        <sample id="pluginIntro" dir="userguide/tutorial/pluginIntro" title="Applying a plugin by type">
            <sourcefile file="build.gradle" snippet="apply-by-type"/>
        </sample>
        <para>
            Thanks to Gradle's default imports (see <xref linkend='ide_support'/>) you could also write:
        </para>
        <sample id="pluginIntro" dir="userguide/tutorial/pluginIntro" title="Applying a plugin by type">
            <sourcefile file="build.gradle" snippet="apply-by-type-with-import"/>
        </sample>
        <para>
            The application of plugins is <emphasis>idempotent</emphasis>. That is, a plugin can be applied multiple times. If the plugin
            has previously been applied, any further applications will have no effect.
        </para>
        <para>
            A plugin is simply any class that implements the <apilink class="org.gradle.api.Plugin"/> interface. Gradle provides
            the core plugins as part of its distribution so simply applying the plugin as above is all you need to do.
            For 3rd party plugins however, you need to make the plugins available to the build classpath. For more information
            on how to do this, see <xref linkend="sec:external_dependencies" />.
        </para>
        <para>
            For more on writing your own plugins, see <xref linkend="custom_plugins" />.
        </para>
    </section>
    <section>
        <title>What plugins do</title>
        <para>
            Applying a plugin to the project allows the plugin to extend the project's capabilities. It can do things
            such as:
        </para>
        <itemizedlist>
            <listitem>Add tasks to the project (e.g. compile, test)</listitem>
            <listitem>Pre-configure added tasks with useful defaults.</listitem>
            <listitem>Add dependency configurations to the project (see <xref linkend="configurations"/>).</listitem>
            <listitem>Add new properties and methods to existing type via extensions.</listitem>
        </itemizedlist>
        <para>
            Let's check this out:
        </para>
        <sample id="pluginIntro" dir="userguide/tutorial/pluginIntro" title="Tasks added by a plugin">
            <sourcefile file="build.gradle" snippet="apply-by-id"/>
            <output args="-q show"/>
        </sample>
        <para>
            The Java plugin has added a <literal>compileJava</literal> task and a <literal>processResources</literal> task
            to the project and configured the <literal>destinationDir</literal> property of both of these tasks.
        </para>
    </section>
    <section id='sub:more_about_convention_objects'>
        <title>Conventions</title>
        <para>
            Plugins can pre-configure the project in smart ways to support convention-over-configuration. Gradle
            provides mechanisms and sophisticated support and it's a key ingredient in powerful-yet-concise build scripts.
        </para>
        <para>
            We saw in the example above that the Java plugins adds a task named <literal>compileJava</literal> that has
            a property named <literal>destinationDir</literal> (that configures where the compiled Java source should be placed).
            The Java plugin defaults this property to point to <literal>build/classes/main</literal> in the project directory.
            This is an example of convention-over-configuration via a <emphasis>reasonable default</emphasis>.
        </para>
        <para>
            We can change this property simply by giving it a new value.
        </para>
        <sample id="pluginConfig" dir="userguide/tutorial/pluginConfig" title="Changing plugin defaults">
            <sourcefile file="build.gradle"/>
            <output args="-q show"/>
        </sample>
        <para>
            However, the <literal>compileJava</literal> task is likely to not be the only task that needs to know where
            the class files are. 
        </para>
        <para>
            The Java plugin adds the concept of <emphasis>source sets</emphasis> (see <apilink class="org.gradle.api.tasks.SourceSet" />)
            to describe the aspects of a set of source, one aspect being where the class files should be written to when it is compiled.
            The Java plugin maps the <literal>destinationDir</literal> property of the <literal>compileJava</literal> task to this aspect of the source set.
        </para>
        <para>
            We can change where the class files are written via the source set.
        </para>
        <sample id="pluginConvention" dir="userguide/tutorial/pluginConvention" title="Plugin convention object">
            <sourcefile file="build.gradle"/>
            <output args="-q show"/>
        </sample>
        <para>
            In the example above, we applied the Java plugin which, among other things, did the following:
        </para>
        <itemizedlist>
            <listitem>Added a new domain object type: <apilink class="org.gradle.api.tasks.SourceSet" /></listitem>
            <listitem>Configured a <literal>main</literal> source set with default (i.e. conventional) values for properties</listitem>
            <listitem>Configured supporting tasks to use these properties to perform work</listitem>
        </itemizedlist>
        <para>
            All of this happened during the <literal>apply plugin: "java"</literal> step. In the example above, we <emphasis>changed</emphasis>
            the desired location of the class files after this conventional configuration had been performed. Notice by the output with the example
            that the value for <literal>compileJava.destinationDir</literal> also changed to reflect the configuration change.
        </para>
        <para>
            Consider the case where another task is to consume the classes files. If this task is configured to use the value from
            <literal>sourceSets.main.output.classesDir</literal>, then changing it in this location will update both the
            <literal>compileJava</literal> task and this other consumer task whenever it is changed.
        </para>
        <para>
            This ability to configure properties of objects to reflect the value of another object's task at all times (i.e. even when it changes) is
            known as “<emphasis>convention mapping</emphasis>”. It allows Gradle to provide conciseness through convention-over-configuration and
            sensible defaults yet not require complete reconfiguration if a conventional default needs to be changed. Without this, in the example above,
            we would have had to reconfigure every object that needs to work with the class files.
        </para>
    </section>
    <section>
        <title>More on plugins</title>
        <para>
            This chapter aims to serve as an introduction to plugins and Gradle and the role they play. For more information on the inner workings
            of plugins, see <xref linkend="custom_plugins" />.
        </para>
    </section>
</chapter>
