Using the Scala plugin

We can also use Gradle to work with Scala source files. We can have a Scala-only project, or we can have both Java and Scala source files in our project. We must apply the Scala plugin to enable Scala support for our build. The plugin adds new tasks to compile the Scala source files. With the compileScala task, we compile our main Scala source files. The source files must be in the src/main/scala directory. The compileTestScala task compiles all Scala source code files that are in the src/test/scala directory. The plugin also adds a compile<SourceSet>Scala task for custom-defined source sets in our build.

The compile tasks support both Java and Scala source files with joint compilation. We can place our Java source files in say the src/main/java directory of our project and the Scala source files in the src/main/scala directory. The compiler will compile both types of files. To be able to compile the files, we must add dependencies to the Scala tools in our build file. The Scala plugin adds the scalaTools dependency configuration to our build. We must assign the correct dependencies from a Maven repository to this configuration, so that Gradle can invoke the compiler to compile the source files.

Let's create a simple Scala source file in the directory src/main/scala/gradle/sample and save it as Sample.scala:

package gradle.sample

class Sample(val name: String) {
    def getName() = name
}

In the following example build file, we apply the Scala plugin. Also, in the dependencies section we set the correct dependencies for the compiler:

apply plugin: 'scala'

repositories.mavenCentral()

ext {
    scala = [version: '2.9.2', group: 'org.scala-lang']
}

dependencies {
    scalaTools "${scala.group}:scala-compiler:${scala.version}"
    scalaTools "${scala.group}:scala-library:${scala.version}"
}

To build the project, we invoke the build task and get the following output:

$ gradle build
:compileJava UP-TO-DATE
:compileScala
Download http://repo1.maven.org/maven2/org/scala-lang/scala-library/2.9.2/scala-library-2.9.2.pom
Download http://repo1.maven.org/maven2/org/scala-lang/scala-library/2.9.2/scala-library-2.9.2.jar
Download http://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.9.2/scala-compiler-2.9.2.pom
Download http://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.9.2/scala-compiler-2.9.2.jar
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestScala UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
:check
:build

BUILD SUCCESSFUL

Total time: 17.167 secs

Note how the compileScala and compileTestScala tasks are dependency tasks for the classes and testClasses tasks respectively. So, the newly added tasks are automatically part of the normal build tasks we know from our Java projects. The Scala plugin will even automatically include the Java plugin, if we don't apply the Java plugin ourselves.

We can define a custom source set in our project. The Scala plugin adds a compile task for each source set to our project. In the following Gradle build file, we add a new source set with the name actors:

apply plugin: 'scala'

repositories.mavenCentral()

ext {
    scala = [version: '2.9.2', group: 'org.scala-lang']
}

dependencies {
    scalaTools "${scala.group}:scala-compiler:${scala.version}"
    scalaTools "${scala.group}:scala-library:${scala.version}"

    compile "${scala.group}:scala-library:${scala.version}"
}

sourceSets {
    actors
}

When we invoke the tasks command, we see that Gradle added compileActorsScala to the list of available tasks:

$ gradle tasks --all
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
actorsClasses - Assembles the actors classes.
    compileActorsJava - Compiles the actors Java source.
    compileActorsScala - Compiles the actors Scala source.
    processActorsResources - Processes the actors resources.
assemble - Assembles all Jar, War, Zip, and Tar archives. [jar]
build - Assembles and tests this project. [assemble, check]
buildDependents - Assembles and tests this project and all projects that depend on it. [build]
buildNeeded - Assembles and tests this project and all projects it depends on. [build]
classes - Assembles the main classes.
    compileJava - Compiles the main Java source.
    compileScala - Compiles the main Scala source.
    processResources - Processes the main resources.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes. [classes]
testClasses - Assembles the test classes. [classes]
    compileTestJava - Compiles the test Java source.
    compileTestScala - Compiles the test Scala source.
    processTestResources - Processes the test resources.
...

The task actorsClasses is added and has all the compile tasks for the actors source set. When we want the actorsClasses task to be part of the build task, we can assign it as a task dependency to the jar task. In the following example build file, we use the from() method of the jar task to assign the output of the actors source set as part of the JAR file contents:

apply plugin: 'scala'

repositories.mavenCentral()

ext {
    scala = [version: '2.9.2', group: 'org.scala-lang']
}

dependencies {
    scalaTools "${scala.group}:scala-compiler:${scala.version}"
    scalaTools "${scala.group}:scala-library:${scala.version}"

    compile "${scala.group}:scala-library:${scala.version}"
}

sourceSets {
    actors
}

jar {
    from sourceSets.actors.output
}

When we execute the build task, our source files in the source set actors are compiled and added to the JAR file.

The Scala plugin also adds several new properties to a source set. The following table shows the extra properties:

Property name

Type

Description

scala

org.gradle.api.file.SourceDirectorySet

The Scala source files for this project; contains both .java and .scala source files if they are in the scala directory.

scala.srcDirs

java.util.Set<java.io.File>

Directories with the Scala source files; can also contain Java source files for joint compilation.

allScala

org.gradle.api.file.FileTree

Only the Scala source files. All files with extension .scala are part of this collection.

Let's create a new task, scalaSourceSetsProperties, to see the contents of each of these properties:

apply plugin: 'scala'

repositories.mavenCentral()

ext {
    scala = [version: '2.9.2', group: 'org.scala-lang']
}

dependencies {
    scalaTools "${scala.group}:scala-compiler:${scala.version}"
    scalaTools "${scala.group}:scala-library:${scala.version}"

    compile "${scala.group}:scala-library:${scala.version}"
}

task scalaSourceSetsProperties << {
    sourceSets {
        main {
            println "scala.srcDirs = ${scala.srcDirs}"
            println "scala.files = ${scala.files.name}"
            println "allScala.files = ${allScala.files.name}"
        }
    }
}

When we invoke the scalaSourceSetsProperties task from the command-line, we get the following output:

$ gradle scalaSourceSetsProperties
:scalaSourceSetsProperties
scala.srcDirs = [/samples/chapter8/scala/src/main/scala]
scala.files = [Sample.scala]
allScala.files = [Sample.scala]

BUILD SUCCESSFUL

Total time: 3.036 secs

Creating documentation with the Scala plugin

The Scala plugin also adds a scaladoc task to our build. We can use this task to generate documentation from the source files. This is like the javadoc task from the Java plugin. We can configure the scaladoc task to provide extra options.

In the following example build file, we add a title to the generated documentation by configuring the scaladoc task:

import org.gradle.api.tasks.scala.*

apply plugin: 'scala'

version = 2.1

repositories.mavenCentral()

ext {
    scala = [version: '2.9.2', group: 'org.scala-lang']
}

dependencies {
    scalaTools "${scala.group}:scala-compiler:${scala.version}"
    scalaTools "${scala.group}:scala-library:${scala.version}"

    compile "${scala.group}:scala-library:${scala.version}"
}

scaladoc.title = 'Scala documentation'

When we invoke the scaladoc task, Gradle will generate the documentation, and the result is in build/docs/scaladoc. We can open the file index.html in our web browser to see the generated documentation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.12