<!--
  ~ Copyright 2010 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="eclipse_plugin">
    <title>The Eclipse Plugin</title>
    
    <para>The Eclipse plugin generates files that are used by <ulink url="http://eclipse.org">Eclipse IDE</ulink>, thus
        making it possible to import the project into Eclipse (<guimenuitem>File</guimenuitem> - <guimenuitem>Import...</guimenuitem> - <guimenuitem>Existing Projects into Workspace</guimenuitem>).
        Both external and project dependencies are considered.</para>

    <para>The Eclipse plugin will create different files depending on which other plugins used:</para>
    <table>
        <title>Eclipse support for other plugins</title>
        <thead>
            <tr><td>Plugin</td><td>Description</td></tr>
        </thead>
        <tr>
            <td>None</td><td>Generates minimal <filename>.project</filename> file.</td>
        </tr>
        <tr>
            <td><link linkend="java_plugin">Java</link></td><td>Adds Java configuration to <filename>.project</filename>.
            Generates <filename>.classpath</filename> and JDT settings file.</td>
        </tr>
        <tr>
            <td><link linkend="groovy_plugin">Groovy</link></td><td>Adds Groovy configuration to <filename>.project</filename> file.</td>
        </tr>
        <tr>
            <td><link linkend="scala_plugin">Scala</link></td><td>Adds Scala support to <filename>.project</filename> file.</td>
        </tr>
        <tr>
            <td><link linkend="war_plugin">War</link></td><td>Adds web application support to <filename>.project</filename>.
            Generates WTP settings files.</td>
        </tr>
    </table>

<section>
        <title>Usage</title>
    <para>To use the Eclipse plugin, include in your build script:</para>
    <sample id="useEclipsePlugin" dir="eclipse" title="Using the Eclipse plugin">
        <sourcefile file="build.gradle" snippet="use-plugin"/>
    </sample>
    <para>There are several tasks that the Eclipse plugin provides (presented in <xref linkend='eclipsetasks'/>). The
        main task that you'd use is the <literal>eclipse</literal> task.
    </para>
    </section>
    <section>
        <title>Tasks</title>

        <para>The Eclipse plugin adds the tasks shown below to a project.</para>

        <table id='eclipsetasks'>
            <title>Eclipse plugin - tasks</title>
            <thead>
                <tr>
                    <td>Task name</td>
                    <td>Depends on</td>
                    <td>Type</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>eclipse</literal>
                </td>
                <td><literal>eclipseProject</literal>, <literal>eclipseClasspath</literal>, <literal>eclipseJdt</literal>,
                    <literal>eclipseWtp</literal></td>
                <td><apilink class="org.gradle.api.Task"/></td>
                <td>Generates all the eclipse configuration files</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanEclipse</literal>
                </td>
                <td>
                    <literal>cleanEclipseProject</literal>, <literal>cleanEclipseClasspath</literal>, <literal>cleanEclipseJdt</literal>,
                    <literal>cleanEclipseWtp</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes all eclipse configuration files</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanEclipseProject</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the eclipse project file</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanEclipseClasspath</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the eclipse classpath file</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanEclipseJdt</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the JDT settings file.</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanEclipseWtp</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the WTP settings files.</td>
            </tr>
            <tr>
                <td>
                    <literal>eclipseProject</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.EclipseProject"/></td>
                <td>Generates the eclipse project file.</td>
            </tr>
            <tr>
                <td>
                    <literal>eclipseClasspath</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.EclipseClasspath"/></td>
                <td>Generates the Eclipse classpath file.</td>
            </tr>
            <tr>
                <td>
                    <literal>eclipseJdt</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.EclipseJdt"/></td>
                <td>Generates the JDT settings file.</td>
            </tr>
            <tr>
                <td>
                    <literal>eclipseWtp</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.EclipseWtp"/></td>
                <td>Generates the WTP settings files.</td>
            </tr>
        </table>

        <table id='eclipse-project'>
            <title>EclipseProject task</title>
            <thead>
                <tr>
                    <td>Property</td>
                    <td>Type</td>
                    <td>Default Value</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>projectName</literal>
                </td>
                <td>
                    <classname>String</classname>
                </td>
                <td><literal>project.name</literal></td>
                <td>The name of the Eclipse project. Must not be null.</td>
            </tr>
            <tr>
                <td>
                    <literal>comment</literal>
                </td>
                <td>
                    <classname>String</classname>
                </td>
                <td><literal>project.description</literal></td>
                <td>A comment for the eclipse project.</td>
            </tr>
            <tr>
                <td>
                    <literal>referencedProjects</literal>
                </td>
                <td>
                    <classname>Set&lt;String&gt;</classname>
                </td>
                <td>empty set</td>
                <td>The referenced projects of the Eclipse project.</td>
            </tr>
            <tr>
                <td>
                    <literal>natures</literal>
                </td>
                <td>
                    <classname>List&lt;String&gt;</classname>
                </td>
                <td>The default is an empty set. Applying Java, Groovy, Scala or War plugin
                will add additional natures.</td>
                <td>The natures of the Eclipse project.</td>
            </tr>
            <tr>
                <td>
                    <literal>buildCommands</literal>
                </td>
                <td>
                    <classname>List&lt;BuildCommand&gt;</classname>
                </td>
                <td>The default is an empty set. Applying Java, Groovy, Scala or War plugin
                will add additional build commands.</td>
                <td>The build commands of the Eclipse project.</td>
            </tr>
            <tr>
                <td>
                    <literal>links</literal>
                </td>
                <td>
                    <classname>Set&lt;Link&gt;</classname>
                </td>
                <td>empty set</td>
                <td>The links for this Eclipse project.</td>
            </tr>
        </table>

        <table id='eclipse-classpath'>
            <title>EclipseClasspath task</title>
            <thead>
                <tr>
                    <td>Property</td>
                    <td>Type</td>
                    <td>Default Value</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>sourceSets</literal>
                </td>
                <td><classname>Iterable&lt;SourceSet></classname></td>
                <td>
                    <literal>sourceSets</literal>
                </td>
                <td>The source sets which source directories should be added to the Eclipse classpath.</td>
            </tr>
            <tr>
                <td>
                    <literal>containers</literal>
                </td>
                <td>
                    <classname>Set&lt;String&gt;</classname>
                </td>
                <td>empty set</td>
                <td>The containers to be added to the Eclipse classpath.</td>
            </tr>
            <tr>
                <td>
                    <literal>plusConfigurations</literal>
                </td>
                <td>
                    <classname>Set&lt;Configuration&gt;</classname>
                </td>
                <td><literal>[configurations.testRuntime]</literal></td>
                <td>The configurations, which files are to be transformed into classpath entries.</td>
            </tr>
            <tr>
                <td>
                    <literal>minusConfigurations</literal>
                </td>
                <td>
                    <classname>Set&lt;Configuration&gt;</classname>
                </td>
                <td>empty set</td>
                <td>The configurations which files are to be excluded from the classpath entries.</td>
            </tr>
            <tr>
                <td>
                    <literal>downloadSources</literal>
                </td>
                <td>
                    <classname>boolean</classname>
                </td>
                <td>
                    <literal>true</literal>
                </td>
                <td>Whether to download sources for the external dependencies.</td>
            </tr>
            <tr>
                <td>
                    <literal>downloadJavadoc</literal>
                </td>
                <td>
                    <classname>boolean</classname>
                </td>
                <td>
                    <literal>false</literal>
                </td>
                <td>Whether to download javadoc for the external dependencies.</td>
            </tr>
            <tr>
                <td>
                    <literal>variables</literal>
                </td>
                <td>
                    <classname>Map&lt;String,File&gt;</classname>
                </td>
                <td><literal>[:]</literal></td>
                <td>If the beginning of the absolute path of a library matches a value of a variable,
                a variable entry is created. The matching part of the library path is replaced with the variable name.</td>
            </tr>
        </table>

        <table id='eclipse-wtp'>
            <title>EclipseWtp task</title>
            <thead>
                <tr>
                    <td>Property</td>
                    <td>Type</td>
                    <td>Default Value</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>sourceSets</literal>
                </td>
                <td><classname>Iterable&lt;SourceSet&gt;</classname></td>
                <td>
                    <literal>project.sourceSets</literal>
                </td>
                <td>The source sets which source directories should be added to the Eclipse classpath.</td>
            </tr>
            <tr>
                <td>
                    <literal>deployName</literal>
                </td>
                <td>
                    <classname>String</classname>
                </td>
                <td><literal>project.name</literal></td>
                <td>The deploy name to be used in the org.eclipse.wst.common.component file.</td>
            </tr>
            <tr>
                <td>
                    <literal>plusConfigurations</literal>
                </td>
                <td>
                    <classname>Set&lt;Configuration&gt;</classname>
                </td>
                <td><literal>[configurations.testRuntime]</literal></td>
                <td>The configurations, which files are to be transformed into classpath entries.</td>
            </tr>
            <tr>
                <td>
                    <literal>minusConfigurations</literal>
                </td>
                <td>
                    <classname>Set&lt;Configuration&gt;</classname>
                </td>
                <td><literal>[configurations.providedRuntime]</literal></td>
                <td>The configurations which files are to be excluded from the classpath entries.</td>
            </tr>
            <tr>
                <td>
                    <literal>variables</literal>
                </td>
                <td>
                    <classname>Map&lt;String,File&gt;</classname>
                </td>
                <td><literal>[:]</literal></td>
                <td>If the beginning of the absolute path of a library matches a value of a variable,
                a variable entry is created. The matching part of the library path is replaced with the variable name.</td>
            </tr>
            <tr>
                <td>
                    <literal>facets</literal>
                </td>
                <td>
                    <classname>List&lt;Facet&gt;</classname>
                </td>
                <td><literal>jst.java</literal> and <literal>jst.web</literal> facet</td>
                <td>The facets to be added as installed elements to the org.eclipse.wst.common.project.facet.core file.</td>
            </tr>
        </table>

        <table id='eclipse-task-hooks'>
            <title>Task Hooks</title>
            <thead>
                <tr>
                    <td>Method</td>
                    <td>EclipseProject Task Argument</td>
                    <td>EclipseClasspath Task Argument</td>
                    <td>EclipseWtp Task Argument</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal><code>beforeConfigured { arg -> }</code></literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Project"/></td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Classpath"/></td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Wtp"/></td>
                <td>Gets called directly after the domain objects have been populated with the content of the
                    existing XML file (if there is one).</td>
            </tr>
            <tr>
                <td>
                    <literal><code>whenConfigured { arg -> }</code></literal>
                </td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Project"/></td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Classpath"/></td>
                <td><apilink class="org.gradle.plugins.eclipse.model.Wtp"/></td>
                <td>Gets called after the domain objects have been populated with the content of the
                    existing XML file and the content from the build script.</td>
            </tr>
            <tr>
                <td>
                    <literal><code>withXml { arg -> }</code></literal>
                </td>
                <td><apilink class="org.gradle.api.artifacts.maven.XmlProvider"/></td>
                <td><apilink class="org.gradle.api.artifacts.maven.XmlProvider"/></td>
                <td><code>['org.eclipse.wst.common.component': groovy.util.Node, 'org.eclipse.wst.common.project.facet.core': groovy.util.Node]</code></td>
                <td>The root node of the XML just before the XML is written to disk.</td>
            </tr>
        </table>
    </section>
    <section>
        <title>Customizing the generated files</title>
        <para>All the tasks of the eclipse plugin provide the same hooks and behavior for customizing the generated content.</para>
        <para>The tasks recognize existing eclipse files. If an eclipse project, classpath or wtp file does not exists,
        default values are used. Otherwise the existing ones are merged.</para>
        <section>
            <title>Merging</title>
            <para>The first option to customize the Eclipse files is to have an existing Eclipse file before the tasks are run.
            Existing files will be merged together with the generated content.
            Any section of the existing Eclipse files that are not the target of generated content will neither be changed nor removed.</para>
            <section id="sec:complete-overwrite">
                <title>Disabling merging with a complete overwrite</title>
                <para>If you want Gradle to completely overwrite existing Eclipse files you can specify this on the command line by
                    executing something like <userinput>gradle cleanEclipse eclipse</userinput> or <userinput>gradle cleanEclipseClasspath eclipseClasspath</userinput>.
                    You can specify this also in the build script. Just make the generating tasks depending on the deleting tasks. You can tailor this
                    to your needs. You could make the <literal>eclipse</literal> task dependent on the <literal>cleanEclipse</literal> task. If you only want
                    to overwrite for example the classpath files you simply make the <literal>eclipseClasspath</literal> task dependent on the
                    <literal>cleanEclipseClasspath</literal> task.
                </para>
            </section>
        </section>
        <section>
            <title>Hooking into the generation lifecycle</title>
            <para>The Eclipse plugin provides a couple of domain classes that model the Eclipse files.
                But only the sections that are autogenerated by Gradle. The generation lifecycle is the following.
                If there is an existing Eclipse file, its whole XML is parsed and stored in memory. Then the domain
                objects are populated with the relevant content of the the existing XML. After that the build script information
                is used to further populate those objects (e.g. add additional dependencies).
                Then all sections modelled by the domain objects are removed from the XML in memory. After that the domain objects are used to inject
                their content into the XML. Finally the XML is written to disk. The lifecycle provides hooks to modify the result according to your needs.
            </para>
            <section id="sec:partial-overwrite">
                <title>Partial Overwrite</title>
                <para>Doing a <link linkend="sec:complete-overwrite">complete overwrite</link> removes all your manual customizations.
                    This might be not what you want.
                    Therefore Gradle provides the option for a partial overwrite. You can hook into the phase just after the existing
                    Eclipse files have populated the domain objects. You could then for example remove all the dependencies
                    from the <literal>classpath</literal> object.
                    <sample id="partialOverwrites" dir="eclipse"
                            title="Partial Overwrite for Module">
                        <sourcefile file="build.gradle" snippet="module-before-configured"/>
                    </sample>
                    The generated XML will have all sections of the existing Eclipse classpath file,
                    except for the dependencies, where only the information of the build script is used. You could do something
                    similar for the project file.
                    <sample id="partialOverwritesProject" dir="eclipse"
                            title="Partial Overwrite for Project">
                        <sourcefile file="build.gradle" snippet="project-before-configured"/>
                    </sample>
                </para>
            </section>
            <section>
                <title>Modifying the fully populated domain objects</title>
                <para>You can also hook into the phase after the existing Eclipse files and the build script metadata have
                    populated the domain objects. You could then for example disable export of all the dependencies
                    of the <literal>module</literal> object.
                    <sample id="exportDependencies" dir="eclipse"
                            title="Export Dependencies">
                        <sourcefile file="build.gradle" snippet="module-when-configured"/>
                    </sample>

                </para>
            </section>
            <section>
                <title>Modifying the XML</title>
                <para>You can also hook into the phase after the XML DOM is fully populated but not written to disk.
                    That hook provides total control over what is generated.
                    <sample id="wtpWithXml" dir="eclipse"
                            title="Customizing the XML">
                        <sourcefile file="build.gradle" snippet="wtp-with-xml"/>
                        <test args="eclipse"/>
                    </sample>
                </para>
            </section>
        </section>
    </section>
</chapter>