Using the Scala plugin

We can also use Gradle to work with Scala source files. We can have a Scala-only project or both Java and Scala source files in our project. We must apply the Scala plugin to enable the 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 library in our build file. We must assign the correct dependencies from a Maven repository to the compile configuration so that Gradle can invoke the compiler to compile the source files.

Let's create a simple Scala source file in the src/main/scala/gradle/ directory 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, as follows:

apply plugin: 'scala' 
 
repositories { 
    jcenter() 
} 
 
dependencies { 
    // Define dependency on Scala library 
    // for compilation and Scala tools. 
    compile group: 'org.scala-lang', 
      name: 'scala-library', 
      version: '2.11.4' 
} 

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

$ gradle build
:compileJava UP-TO-DATE
:compileScala
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestScala UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 2.656 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 that we know from our Java projects. The Scala plugin will 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, as follows:

apply plugin: 'scala' 
 
repositories { 
    jcenter() 
} 
 
dependencies { 
    compile "org.scala-lang:scala-library:2.11.4" 
} 
 
sourceSets { 
    // Extra source set actors. 
    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 actors classes.
compileActorsJava - Compiles actors Java source.
compileActorsScala - Compiles the actors Scala source.
processActorsResources - Processes actors resources.
assemble - Assembles the outputs of this project. [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 main classes.
compileJava - Compiles main Java source.
compileScala - Compiles the main Scala source.
processResources - Processes main resources.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes. [classes]
testClasses - Assembles test classes. [classes]
compileTestJava - Compiles test Java source.
compileTestScala - Compiles the test Scala source.
processTestResources - Processes test resources.
...
BUILD SUCCESSFUL
Total time: 0.76 secs

The actorsClasses task 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 thejar 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.

When we execute the build task, our source files in the actors source set 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

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

scala.srcDirs

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

These are the directories with the Scala source files; they can also contain Java source files for joint compilation.

allScala

org.gradle.api.file.FileTree

These are only the Scala source files. All files with the .scala extension 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 { 
    jcenter() 
} 
 
dependencies { 
    compile "org.scala-lang:scala-library:2.11.4" 
} 
 
// New task to show properties on the 
// main source set added by the Scala plugin. 
task scalaSourceSetsProperties << { 
    sourceSets.main.with { sourceSet -> 
        println "scala.srcDirs = ${sourceSet.scala.srcDirs}" 
        println "scala.files = ${sourceSet.scala.files.name}" 
        println "allScala.files = ${sourceSet.allScala.files.name}" 
    } 
} 

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

$ gradle scalaSourceSetsProperties
:scalaSourceSetsProperties
scala.srcDirs = [/gradle-book/samples/Chapter8/Code_Files/scala/src/main/scala]
scala.files = [Sample.scala]
allScala.files = [Sample.scala]
BUILD SUCCESSFUL
Total time: 0.627 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 thejavadoc 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 { 
    jcenter() 
} 
 
dependencies { 
    compile "org.scala-lang:scala-library:2.11.4" 
} 
 
// Configure ScalaDoc task. 
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 index.html file 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.188.152.136