<!--
  ~ 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="scala_plugin" xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>The Scala Plugin</title>

    <para>The Scala plugin extends the Java plugin to add support for Scala projects. It can deal with
        Scala code, mixed Scala and Java code, and even pure Java code (although we don't necessarily recommend to use it for the latter).
        The plugin supports <emphasis>joint compilation</emphasis>, which allows you to freely mix and match Scala and Java code,
        with dependencies in both directions. For example, a Scala class can extend a Java class that in turn extends a Scala class.
        This makes it possible to use the best language for the job, and to rewrite any class in the other language if needed.
    </para>

    <section id="sec:scala_usage">
        <title>Usage</title>
        <para>To use the Scala plugin, include the following in your build script:</para>
        <sample id="useScalaPlugin" dir="scala/quickstart" title="Using the Scala plugin">
            <sourcefile file="build.gradle" snippet="use-plugin"/>
        </sample>
    </section>

    <section id="sec:scala_tasks">
        <title>Tasks</title>
        <para>The Scala plugin adds the following tasks to the project.</para>
        <table>
            <title>Scala plugin - tasks</title>
            <thead>
                <tr>
                    <td>Task name</td>
                    <td>Depends on</td>
                    <td>Type</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td><literal>compileScala</literal></td>
                <td><literal>compileJava</literal></td>
                <td><apilink class="org.gradle.api.tasks.scala.ScalaCompile"/></td>
                <td>Compiles production Scala source files.</td>
            </tr>
            <tr>
                <td><literal>compileTestScala</literal></td>
                <td><literal>compileTestJava</literal></td>
                <td><apilink class="org.gradle.api.tasks.scala.ScalaCompile"/></td>
                <td>Compiles test Scala source files.</td>
            </tr>
            <tr>
                <td><literal>compile<replaceable>SourceSet</replaceable>Scala</literal></td>
                <td><literal>compile<replaceable>SourceSet</replaceable>Java</literal></td>
                <td><apilink class="org.gradle.api.tasks.scala.ScalaCompile"/></td>
                <td>Compiles the given source set's Scala source files.</td>
            </tr>
            <tr>
                <td><literal>scaladoc</literal></td>
                <td>-</td>
                <td><apilink class="org.gradle.api.tasks.scala.ScalaDoc"/></td>
                <td>Generates API documentation for the production Scala source files.</td>
            </tr>
        </table>
        <para>The Scala plugin adds the following dependencies to tasks added by the Java plugin.</para>
        <table>
            <title>Scala plugin - additional task dependencies</title>
            <thead>
                <td>Task name</td>
                <td>Depends on</td>
            </thead>
            <tr>
                <td>
                    <literal>classes</literal>
                </td>
                <td>
                    <literal>compileScala</literal>
                </td>
            </tr>
            <tr>
                <td>
                    <literal>testClasses</literal>
                </td>
                <td>
                    <literal>compileTestScala</literal>
                </td>
            </tr>
            <tr>
                <td>
                    <literal><replaceable>sourceSet</replaceable>Classes</literal>
                </td>
                <td>
                    <literal>compile<replaceable>SourceSet</replaceable>Scala</literal>
                </td>
            </tr>
        </table>
        <figure>
            <title>Scala plugin - tasks</title>
            <imageobject>
                <imagedata fileref="img/scalaPluginTasks.png"/>
            </imageobject>
        </figure>
    </section>

    <section id="sec:scala_project_layout">
        <title>Project layout</title>
        <para>The Scala plugin assumes the project layout shown below.  All the Scala source directories can contain
            Scala <emphasis>and</emphasis> Java code. The Java source directories may only contain Java source code.
            None of these directories need to exist or have anything in them; the Scala plugin will simply compile
            whatever it finds.
        </para>
        <table id='scalalayout'>
            <title>Scala plugin - project layout</title>
            <thead>
                <tr>
                    <td>Directory</td>
                    <td></td>
                    <td>Meaning</td>
                </tr>
            </thead>
            <xi:include href="javaProjectMainLayout.xml"/>
            <tr>
                <td>
                    <filename>src/main/scala</filename>
                </td>
                <td></td>
                <td>Production Scala sources. May also contain Java sources for joint compilation.</td>
            </tr>
            <xi:include href="javaProjectTestLayout.xml"/>
            <tr>
                <td>
                    <filename>src/test/scala</filename>
                </td>
                <td></td>
                <td>Test Scala sources. May also contain Java sources for joint compilation.</td>
            </tr>
            <xi:include href="javaProjectGenericLayout.xml"/>
            <tr>
                <td>
                    <filename>src/<replaceable>sourceSet</replaceable>/scala</filename>
                </td>
                <td></td>
                <td>Scala sources for the given source set. May also contain Java sources for joint compilation.</td>
            </tr>
        </table>

        <section id="sec:changing_scala_project_layout">
            <title>Changing the project layout</title>
            <para>Just like the Java plugin, the Scala plugin allows you to configure custom locations for Scala production and test sources.</para>
            <sample id="customScalaSourceLayout" dir="scala/customizedLayout" title="Custom Scala source layout">
                <sourcefile file="build.gradle" snippet="custom-source-locations"/>
            </sample>
        </section>
    </section>

    <section id="sec:scala_dependency_management">
        <title>Dependency management</title>
        <para>
            Scala projects need to declare a <literal>scala-library</literal> dependency. This dependency will then be used on compile and
            runtime class paths. It will also be used to get hold of the Scala compiler and Scaladoc tool, respectively.
            <footnote>
                <para>See <xref linkend="sec:configure_scala_classpath"/>.</para>
            </footnote>
        </para>
        <para>
            If Scala is used for production code, the <literal>scala-library</literal> dependency should be added to the
            <literal>compile</literal> configuration:
        </para>
        <sample id="declareScalaDependency" dir="scala/quickstart" title="Declaring a Scala dependency for production code">
            <sourcefile file="build.gradle" snippet="scala-dependency"/>
        </sample>
        <para>
            If Scala is only used for test code, the <literal>scala-library</literal> dependency should be added to the <literal>testCompile</literal>
            configuration:
        </para>
        <sample id="declareScalaTestDependency" dir="userguide/scala/scalaDependency" title="Declaring a Scala dependency for test code">
            <sourcefile file="build.gradle" snippet="scala-test-dependency"/>
        </sample>
    </section>

    <section id="sec:configure_scala_classpath">
        <title>Automatic configuration of scalaClasspath</title>
        <para>
            The <literal>ScalaCompile</literal> and <literal>ScalaDoc</literal> tasks consume Scala code in two ways: on their <literal>classpath</literal>,
            and on their <literal>scalaClasspath</literal>. The former is used to locate classes referenced by the source code, and will typically
            contain <literal>scala-library</literal> along with other libraries. The latter is used to load and execute the Scala compiler
            and Scaladoc tool, respectively, and should only contain the <literal>scala-compiler</literal> library and its dependencies.
        </para>
        <para>
            Unless a task's <literal>scalaClasspath</literal> is configured explicitly, the Scala (base) plugin will try to infer it
            from the task's <literal>classpath</literal>. This is done as follows:
            <itemizedlist>
                <listitem>
                    If a <literal>scala-library</literal> jar is found on <literal>classpath</literal>, and the project has at least one repository declared,
                    a corresponding <literal>scala-compiler</literal> repository dependency will be added to <literal>scalaClasspath</literal>.
                </listitem>
                <listitem>
                    Otherwise, execution of the task will fail with a message saying that <literal>scalaClasspath</literal> could not be inferred.
                </listitem>
            </itemizedlist>
        </para>
    </section>

    <section id="sec:configure_zinc_compiler">
        <title>Configuring the Zinc compiler</title>
        <para>
            The Scala plugin uses a configuration named <literal>zinc</literal> to resolve the <link linkend="https://github.com/typesafehub/zinc">Zinc compiler</link> and its dependencies.
            Gradle will provide a default version of Zinc, but if you need to use a particular Zinc version, you can add an explicit dependency like <literal>“com.typesafe.zinc:zinc:0.3.6”</literal>
            to the <literal>zinc</literal> configuration. Gradle supports version 0.3.0 of Zinc and above; however, due to a regression in the Zinc compiler, versions 0.3.2 through 0.3.5.2 cannot be used.
        </para>
        <sample id="declareZincDependency" dir="userguide/scala/zincDependency" title="Declaring a version of the Zinc compiler to use">
            <sourcefile file="build.gradle" snippet="zinc-dependency"/>
        </sample>
        <note>
            <para>
                It is important to take care when declaring your <literal>scala-library</literal> dependency.  The Zinc compiler itself needs a compatible
                version of <literal>scala-library</literal> that may be different from the version required by your application.  Gradle takes care
                of adding a compatible version of <literal>scala-library</literal> for you, but over-broad dependency resolution rules could force an
                incompatible version to be used instead.
            </para>
            <para>
                For example, using <literal>configurations.all</literal> to force a particular version of <literal>scala-library</literal> would also override
                the version used by the Zinc compiler:
            </para>

            <sample id="allScalaLibraryDependency" dir="userguide/scala/zincOverride" title="Forcing a scala-library dependency for all configurations">
                <sourcefile file="build.gradle" snippet="all-dependency"/>
            </sample>

            <para>
                The best way to avoid this problem is to be more selective when configuring the <literal>scala-library</literal> dependency (such as not using a
                <literal>configuration.all</literal> rule or using a conditional to prevent the rule from being applied to the <literal>zinc</literal>
                configuration). Sometimes this rule may come from a plugin or other code that you do not have control over.  In such a case, you can
                force a correct version of the library on the <literal>zinc</literal> configuration only:
            </para>

            <sample id="zincScalaLibraryDependency" dir="userguide/scala/zincOverride" title="Forcing a scala-library dependency for the zinc configuration">
                <sourcefile file="build.gradle" snippet="zinc-dependency"/>
            </sample>
            <para>
                You can diagnose problems with the version of the Zinc compiler selected by running <link linkend="sec:dependency_insight">dependencyInsight</link> for the
                <literal>zinc</literal> configuration.
            </para>
        </note>
    </section>

    <section id="sec:scala_convention_properties">
        <title>Convention properties</title>
        <para>The Scala plugin does not add any convention properties to the project.</para>
    </section>

    <section id="sec:scala_source_set_properties">
        <title>Source set properties</title>
        <para>The Scala plugin adds the following convention properties to each source set in the project. You can
            use these properties in your build script as though they were properties of the source set object.</para>
        <table>
            <title>Scala plugin - source set properties</title>
            <thead>
                <tr>
                    <td>Property name</td>
                    <td>Type</td>
                    <td>Default value</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>scala</literal>
                </td>
                <td>
                    <apilink class="org.gradle.api.file.SourceDirectorySet"/> (read-only)
                </td>
                <td>
                    Not null
                </td>
                <td>
                    The Scala source files of this source set. Contains all <filename>.scala</filename> and
                    <filename>.java</filename> files found in the Scala source directories, and excludes all other
                    types of files.
                </td>
            </tr>
            <tr>
                <td>
                    <literal>scala.srcDirs</literal>
                </td>
                <td>
                    <classname>Set&lt;File></classname>. Can set using anything described in <xref linkend="sec:specifying_multiple_files"/>.
                </td>
                <td>
                    <literal>[<replaceable>projectDir</replaceable>/src/<replaceable>name</replaceable>/scala]</literal>
                </td>
                <td>
                    The source directories containing the Scala source files of this source set. May also contain
                    Java source files for joint compilation.
                </td>
            </tr>
            <tr>
                <td>
                    <literal>allScala</literal>
                </td>
                <td>
                    <apilink class="org.gradle.api.file.FileTree"/> (read-only)
                </td>
                <td>
                    Not null
                </td>
                <td>
                    All Scala source files of this source set. Contains only the <filename>.scala</filename> files
                    found in the Scala source directories.
                </td>
            </tr>
        </table>

        <para>These convention properties are provided by a convention object of type <apilink class="org.gradle.api.tasks.ScalaSourceSet"/>.</para>
        <para>The Scala plugin also modifies some source set properties:</para>
        <table>
            <title>Scala plugin - source set properties</title>
            <thead>
                <tr>
                    <td>Property name</td>
                    <td>Change</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>allJava</literal>
                </td>
                <td>Adds all <filename>.java</filename> files found in the Scala source directories.</td>
            </tr>
            <tr>
                <td>
                    <literal>allSource</literal>
                </td>
                <td>Adds all source files found in the Scala source directories.</td>
            </tr>
        </table>
    </section>

    <section id="sec:scala_compiling_in_external_process">
        <title>Compiling in external process</title>
        <para>
            Scala compilation takes place in an external process.
        </para>
        <para>Memory settings for the external process default to the defaults of the JVM. To adjust memory settings,
            configure the <literal>scalaCompileOptions.forkOptions</literal> property as needed:
            <sample id="zinc" dir="scala/zinc" title="Adjusting memory settings">
                <sourcefile file="build.gradle" snippet="adjust-memory"/>
            </sample>
        </para>
    </section>

    <section id="sec:scala_incremental_compilation">
        <title>Incremental compilation</title>
        <para>
            By compiling only classes whose source code has changed since the previous compilation, and classes affected by these changes,
            incremental compilation can significantly reduce Scala compilation time. It is particularly effective when frequently compiling
            small code increments, as is often done at development time.
        </para>

        <para>
            The Scala plugin defaults to incremental compilation by integrating with <ulink url="https://github.com/typesafehub/zinc">Zinc</ulink>,
            a standalone version of <ulink url="https://github.com/harrah/xsbt">sbt</ulink>'s incremental Scala compiler. If you want to disable the
            incremental compilation, set <literal>force = true</literal> in your build file:
            <sample id="zinc" dir="scala/force" title="Forcing all code to be compiled">
                <sourcefile file="build.gradle" snippet="force-compilation"/>
            </sample>
            <emphasis>Note:</emphasis> This will only cause all classes to be recompiled if at least one input source file has changed. If there are
            no changes to the source files, the <literal>compileScala</literal> task will still be considered <literal>UP-TO-DATE</literal> as usual.
        </para>

        <para>
            The Zinc-based Scala Compiler supports joint compilation of Java and Scala code. By default, all Java and Scala code
            under <literal>src/main/scala</literal> will participate in joint compilation. Even Java code will be compiled incrementally.
        </para>

        <para>
            Incremental compilation requires dependency analysis of the source code. The results of this analysis are stored in the file designated
            by <literal>scalaCompileOptions.incrementalOptions.analysisFile</literal> (which has a sensible default). In a multi-project build, analysis
            files are passed on to downstream <literal>ScalaCompile</literal> tasks to enable incremental compilation across project boundaries. For
            <literal>ScalaCompile</literal> tasks added by the Scala plugin, no configuration is necessary to make this work. For other
            <literal>ScalaCompile</literal> tasks that you might add, the property <literal>scalaCompileOptions.incrementalOptions.publishedCode</literal> needs to be configured to point
            to the classes folder or Jar archive by which the code is passed on to compile class paths of downstream <literal>ScalaCompile</literal> tasks.
            Note that if <literal>publishedCode</literal> is not set correctly, downstream tasks may not recompile code affected by upstream changes,
            leading to incorrect compilation results.
        </para>

        <para>
            Note that Zinc's Nailgun based daemon mode is not supported. Instead, we plan to enhance Gradle's own compiler daemon to stay alive across Gradle
            invocations, reusing the same Scala compiler. This is expected to yield another significant speedup for Scala compilation.
        </para>
    </section>

    <section id='sec:scala_cross_compilation'>
        <title>Compiling and testing for Java 6</title>
        <para>
            The Scala compiler ignores Gradle's <literal>targetCompatibility</literal> and <literal>sourceCompatibility</literal> settings.
            In Scala 2.11, the Scala compiler always compiles to Java 6 compatible bytecode.
            In Scala 2.12, the Scala compiler always compiles to Java 8 compatible bytecode.
            If you also have Java sources, you can follow the same steps as for the <link linkend="sec:java_cross_compilation">Java plugin</link> to ensure the correct Java compiler is used.
            <sample id="scalaCrossCompilation" dir="scala/crossCompilation" title="Configure Java 6 build for Scala">
                <sourcefile file="gradle.properties" />
                <sourcefile file="build.gradle" snippet="scala-cross-compilation" />
            </sample>
        </para>
    </section>

    <section id="sec:eclipse_integration">
        <title>Eclipse Integration</title>
        <para>
            When the Eclipse plugin encounters a Scala project, it adds additional configuration to make the project work with Scala IDE out of the box.
            Specifically, the plugin adds a Scala nature and dependency container.
        </para>
    </section>

    <section id="sec:intellij_idea_integration">
        <title>IntelliJ IDEA Integration</title>
        <para>
            When the IDEA plugin encounters a Scala project, it adds additional configuration to make the project work with IDEA out of the box. Specifically,
            the plugin adds a Scala SDK (IntelliJ IDEA 14+) and a Scala compiler library that matches the Scala version on the project's class path.
            The Scala plugin is backwards compatible with earlier versions of IntelliJ IDEA and it is possible to add a Scala facet instead of the default Scala SDK
            by configuring <literal>targetVersion</literal> on <apilink class="org.gradle.plugins.ide.idea.model.IdeaModel">IdeaModel</apilink>.
            <sample id="ideaTargetVersion" dir="userguide/scala/ideaTargetVersion" title="Explicitly specify a target IntelliJ IDEA version">
                <sourcefile file="build.gradle" snippet="scala-idea-target-version"/>
            </sample>
        </para>
    </section>
</chapter>
