# Chapter 22. The Scala Plugin

The Scala plugin extends the Java plugin to add support for Scala projects. It can deal with Scala-only projects and with mixed Java/Scala projects. It can even deal with Java-only projects. The Scala plugin supports joint compilation of Java and Scala source. This means your project can contain Scala classes which use Java classes, and vice versa.

## 22.1. Usage

To use the Scala plugin, include in your build script:

Example 22.1. Using the Scala plugin

`build.gradle`

    apply plugin: 'scala'

## 22.2. Tasks

The Scala plugin adds the following tasks to the project.

Table 22.1. Scala plugin - tasks

Task name | Depends on | Type | Description
`compileScala` | `compileJava` | `ScalaCompile` | Compiles production Scala source files using scalac.
`compileTestScala` | `compileTestJava` | `ScalaCompile` | Compiles test Scala source files using scalac.
`compile`SourceSet`Scala` | `compile`SourceSet`Java` | `ScalaCompile` | Compiles the given source set's Scala source files using scalac.
`scaladoc` | - | `ScalaDoc` | Generates API documentation for the production Scala source files using scaladoc.

The Scala plugin adds the following dependencies to tasks added by the Java plugin.

Table 22.2. Scala plugin - additional task dependencies

Task name | Depends on
`classes` | `compileScala`
`testClasses` | `compileTestScala`
``sourceSet`Classes` | `compile`SourceSet`Scala`

Figure 22.1. Scala plugin - tasks

## 22.3. Project layout

The Scala plugin assumes the project layout shown below. All the Scala source directories can contain Scala and Java code. The Java source directories may only contain Java source code. None of these directories need exist or have anything in them. The Scala plugin will compile whatever it finds, and handles anything which is missing.

Table 22.3. Scala plugin - project layout

Directory | Meaning
`src/main/java` | Production Java source
`src/main/resources` | Production resources
`src/main/scala` | Production Scala source. May also contain Java source for joint compilation.
`src/test/java` | Test Java source
`src/test/resources` | Test resources
`src/test/scala` | Test Scala source. May also contain Java source for joint compilation.
`src/`sourceSet`/java` | Java source for the given source set
`src/`sourceSet`/resources` | Resources for the given source set
`src/`sourceSet`/scala` | Scala source for the given source set. May also contain Java source for joint compilation.

### 22.3.1. Changing the project layout

TBD

Example 22.2. Custom Scala source layout

`build.gradle`

    sourceSets {
        main {
            scala {
                srcDir 'src/scala'
            }
        }
    }

## 22.4. Dependency Management

The Scala plugin adds a `scalaTools` configuration, which it uses to locate the Scala tools, such as scalac, to use. You must specify the version of Scala to use. Below is an example.

Example 22.3. Declaring the Scala version to use

`build.gradle`

    repositories {
        mavenCentral()
    }

    dependencies {
        // Libraries needed to run the scala tools
        scalaTools 'org.scala-lang:scala-compiler:2.7.7'
        scalaTools 'org.scala-lang:scala-library:2.7.7'

        // Libraries needed for scala api
        compile 'org.scala-lang:scala-library:2.7.7'
    }

## 22.5. Convention Properties

The Scala plugin does not add any convention properties to the project.

## 22.6. Source set properties

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 (see Section 18.2, “Using the convention object”).

Table 22.4. Scala plugin - source set properties

Property name | Type | Default value | Description
`scala` | `SourceDirectorySet` (read-only) | Not null | The Scala source files of this source set. Contains all `.scala` and `.java` files found in the Scala source directories, and excludes all other types of files.
`scala.srcDirs` | `Set<File>`. Can set using anything described in Section 15.5, “Specifying a set of input files”. | `[`projectDir`/src/`name`/scala]` | The source directories containing the Scala source files of this source set. May also contain Java source files for joint compilation.
`allScala` | `FileTree` (read-only) | Not null | All Scala source files of this source set. Contains only the `.scala` files found in the Scala source directories.

These convention properties are provided by a convention object of type `ScalaSourceSet` .

The Scala plugin also modifies some source set properties:

Table 22.5. Scala plugin - source set properties

Property name | Change
`allJava` | Adds all `.java` files found in the Scala source directories.
`allSource` | Adds all source files found in the Scala source directories.