# The Scala Plugin

version 7.6.4

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 joint compilation, 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.

Note that if you want to benefit from the API / implementation separation, you can also apply the `java-library` plugin to your Scala project.

## Usage

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

Example 1. Using the Scala plugin
build.gradle

    plugins {
        id 'scala'
    }

build.gradle.kts

    plugins {
        scala
    }

## Tasks

The Scala plugin adds the following tasks to the project. Information about altering the dependencies to Java compile tasks are found here.

`compileScala` — ScalaCompile

  Depends on: `compileJava`

  Compiles production Scala source files.

`compileTestScala` — ScalaCompile

  Depends on: `compileTestJava`

  Compiles test Scala source files.

`compileSourceSetScala` — ScalaCompile

  Depends on: `compileSourceSetJava`

  Compiles the given source set’s Scala source files.

`scaladoc` — ScalaDoc

  Generates API documentation for the production Scala source files.

The `ScalaCompile` and `ScalaDoc` tasks can also leverage the Java toolchain support.

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

Table 1. Scala plugin - additional task dependencies
Task name | Depends on
--- | ---
`classes` | `compileScala`
`testClasses` | `compileTestScala`
`sourceSetClasses` | `compileSourceSetScala`

Figure 1. Scala plugin - tasks

## 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 to exist or have anything in them; the Scala plugin will simply compile whatever it finds.

`src/main/java`

  Production Java source.

`src/main/resources`

  Production resources, such as XML and properties files.

`src/main/scala`

  Production Scala source. May also contain Java source files 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 files for joint compilation.

`src/sourceSet/java`

  Java source for the source set named sourceSet.

`src/sourceSet/resources`

  Resources for the source set named sourceSet.

`src/sourceSet/scala`

  Scala source files for the given source set. May also contain Java source files for joint compilation.

### Changing the project layout

Just like the Java plugin, the Scala plugin allows you to configure custom locations for Scala production and test source files.

Example 2. Custom Scala source layout
build.gradle

    sourceSets {
        main {
            scala {
                srcDirs = ['src/scala']
            }
        }
        test {
            scala {
                srcDirs = ['test/scala']
            }
        }
    }

build.gradle.kts

    sourceSets {
        main {
            withConvention(ScalaSourceSet::class) {
                scala {
                    setSrcDirs(listOf("src/scala"))
                }
            }
        }
        test {
            withConvention(ScalaSourceSet::class) {
                scala {
                    setSrcDirs(listOf("test/scala"))
                }
            }
        }
    }

## Dependency management

Scala projects need to declare a `scala-library` 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.[1]

If Scala is used for production code, the `scala-library` dependency should be added to the `implementation` configuration:

Example 3. Declaring a Scala dependency for production code
build.gradle

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'org.scala-lang:scala-library:2.11.12'
        testImplementation 'org.scalatest:scalatest_2.11:3.0.0'
        testImplementation 'junit:junit:4.13'
    }

build.gradle.kts

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation("org.scala-lang:scala-library:2.11.12")
        testImplementation("org.scalatest:scalatest_2.11:3.0.0")
        testImplementation("junit:junit:4.13")
    }

If you want to use Scala 3 instead of the `scala-library` dependency you should add the `scala3-library_3` dependency:

Example 4. Declaring a Scala 3 dependency for production code
build.gradle

    plugins {
        id 'scala'
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'org.scala-lang:scala3-library_3:3.0.1'
        testImplementation 'org.scalatest:scalatest_3:3.2.9'
        testImplementation 'junit:junit:4.13'
    }

    dependencies {
        implementation 'commons-collections:commons-collections:3.2.2'
    }

build.gradle.kts

    plugins {
        scala
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation("org.scala-lang:scala3-library_3:3.0.1")
        testImplementation("org.scalatest:scalatest_3:3.2.9")
        testImplementation("junit:junit:4.13")
    }

    dependencies {
        implementation("commons-collections:commons-collections:3.2.2")
    }

If Scala is only used for test code, the `scala-library` dependency should be added to the `testImplementation` configuration:

Example 5. Declaring a Scala dependency for test code
build.gradle

    dependencies {
        testImplementation 'org.scala-lang:scala-library:2.11.1'
    }

build.gradle.kts

    dependencies {
        testImplementation("org.scala-lang:scala-library:2.11.1")
    }

## Automatic configuration of scalaClasspath

The `ScalaCompile` and `ScalaDoc` tasks consume Scala code in two ways: on their `classpath`, and on their `scalaClasspath`. The former is used to locate classes referenced by the source code, and will typically contain `scala-library` along with other libraries. The latter is used to load and execute the Scala compiler and Scaladoc tool, respectively, and should only contain the `scala-compiler` library and its dependencies.

Unless a task’s `scalaClasspath` is configured explicitly, the Scala (base) plugin will try to infer it from the task’s `classpath`. This is done as follows:

- If a `scala-library` jar is found on `classpath`, and the project has at least one repository declared, a corresponding `scala-compiler` repository dependency will be added to `scalaClasspath`.

- Otherwise, execution of the task will fail with a message saying that `scalaClasspath` could not be inferred.

## Configuring the Zinc compiler

The Scala plugin uses a configuration named `zinc` to resolve the Zinc compiler and its dependencies. Gradle will provide a default version of Zinc, but if you need to use a particular Zinc version, you can change it. Gradle supports version 1.6.0 of Zinc and above.

Example 6. Declaring a version of the Zinc compiler to use
build.gradle

    scala {
        zincVersion = "1.6.1"
    }

build.gradle.kts

    scala {
        zincVersion.set("1.6.1")
    }

The Zinc compiler itself needs a compatible version of `scala-library` that may be different from the version required by your application. Gradle takes care of specifying a compatible version of `scala-library` for you.

You can diagnose problems with the version of the Zinc compiler selected by running dependencyInsight for the `zinc` configuration.

Table 2. Zinc compatibility table
Gradle version | Supported Zinc versions | Zinc coordinates | Required Scala version | Supported Scala compilation version
--- | --- | --- | --- | ---
7.5 and newer | SBT Zinc. Versions 1.6.0 and above. | `org.scala-sbt:zinc_2.13` | Scala `2.13.x` is required for running Zinc. | Scala `2.10.x` through `3.x` can be compiled.
6.0 to 7.5 | SBT Zinc. Versions 1.2.0 and above. | `org.scala-sbt:zinc_2.12` | Scala `2.12.x` is required for running Zinc. | Scala `2.10.x` through `2.13.x` can be compiled.
1.x through 5.x | Deprecated Typesafe Zinc compiler. Versions 0.3.0 and above, except for 0.3.2 through 0.3.5.2. | `com.typesafe.zinc:zinc` | Scala `2.10.x` is required for running Zinc. | Scala `2.9.x` through `2.12.x` can be compiled.

## Adding plugins to the Scala compiler

The Scala plugin adds a configuration named `scalaCompilerPlugins` which is used to declare and resolve optional compiler plugins.

Example 7. Adding a dependency on a Scala compiler plugin
build.gradle

    dependencies {
        implementation "org.scala-lang:scala-library:2.13.1"
        scalaCompilerPlugins "org.typelevel:kind-projector_2.13.1:0.11.0"
    }

build.gradle.kts

    dependencies {
        implementation("org.scala-lang:scala-library:2.13.1")
        scalaCompilerPlugins("org.typelevel:kind-projector_2.13.1:0.11.0")
    }

## Convention properties

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

## Source set properties

The Scala plugin adds the following extensions to each source set in the project. You can use these in your build script as though they were properties of the source set object.

`scala` — SourceDirectorySet (read-only)

  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. Default value: non-null.

`scala.srcDirs` — `Set<File>`

  The source directories containing the Scala source files of this source set. May also contain Java source files for joint compilation. Can set using anything described in Understanding implicit conversion to file collections. Default value: `[projectDir/src/name/scala]`.

`allScala` — FileTree (read-only)

  All Scala source files of this source set. Contains only the `.scala` files found in the Scala source directories. Default value: non-null.

These extensions are backed by an object of type ScalaSourceSet.

The Scala plugin also modifies some source set properties:

Table 3. 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.

## Compiling in external process

Scala compilation takes place in an external process.

Memory settings for the external process default to the defaults of the JVM. To adjust memory settings, configure the `scalaCompileOptions.forkOptions` property as needed:

Example 8. Adjusting memory settings
build.gradle

    tasks.withType(ScalaCompile) {
        scalaCompileOptions.forkOptions.with {
            memoryMaximumSize = '1g'
            jvmArgs = ['-XX:MaxMetaspaceSize=512m']
        }
    }

build.gradle.kts

    tasks.withType<ScalaCompile>().configureEach {
        scalaCompileOptions.forkOptions.apply {
            memoryMaximumSize = "1g"
            jvmArgs = listOf("-XX:MaxMetaspaceSize=512m")
        }
    }

## Incremental compilation

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.

The Scala plugin defaults to incremental compilation by integrating with Zinc, a standalone version of sbt's incremental Scala compiler. If you want to disable the incremental compilation, set `force = true` in your build file:

Example 9. Forcing all code to be compiled
build.gradle

    tasks.withType(ScalaCompile) {
        scalaCompileOptions.with {
            force = true
        }
    }

build.gradle.kts

    tasks.withType<ScalaCompile>().configureEach {
        scalaCompileOptions.apply {
            isForce = true
        }
    }

Note: 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 `compileScala` task will still be considered `UP-TO-DATE` as usual.

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

Incremental compilation requires dependency analysis of the source code. The results of this analysis are stored in the file designated by `scalaCompileOptions.incrementalOptions.analysisFile` (which has a sensible default). In a multi-project build, analysis files are passed on to downstream `ScalaCompile` tasks to enable incremental compilation across project boundaries. For `ScalaCompile` tasks added by the Scala plugin, no configuration is necessary to make this work. For other `ScalaCompile` tasks that you might add, the property `scalaCompileOptions.incrementalOptions.publishedCode` 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 `ScalaCompile` tasks. Note that if `publishedCode` is not set correctly, downstream tasks may not recompile code affected by upstream changes, leading to incorrect compilation results.

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.

## Eclipse Integration

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.

## IntelliJ IDEA Integration

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 `targetVersion` on IdeaModel.

Example 10. Explicitly specify a target IntelliJ IDEA version
build.gradle

    idea {
        targetVersion = '13'
    }

build.gradle.kts

    idea {
        targetVersion = "13"
    }

1. See Automatic configuration of Scala classpath.