Scala Foundation Course - SBT Build Definition


SBT is designed to support a development approach which is most suitable for data exploration and analysis project. It's something like an experiment-driven development method. Most of the SQL developers must be familiar with that approach. I will give you a glimpse of this process.
Let's start sbt. You should see sbt command prompt. You can get the list of available commands by entering help. You will see long list, but I am more interested into just two commands.

  1. tasks
  2. settings

These two commands need special attention and rest of the commands are learned as we progress with the tutorial. Let's start with settings. Type help settings on the sbt command prompt and press enter key.
You will see all available settings which you can set to create your sbt build definition. For example, the name setting gives a name to your project. If you execute show name command, sbt will show you the current project name. The default value for your project name is the project directory name. Let's change it. Execute below commands.

                                
    set name:= "HelloScala."
    show name                                               
                            

It should show the new name.
Similarly, version defines the version number of your project. There are many such settings. For example, libraryDependencies is used to add additional managed dependencies. Suppose you are working on a Spark project and you want to use Spark 2.0.2. So, I will append Spark Core dependency to this setting. Try below commands.

                                
    set libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "2.0.2"
    show libraryDependencies                                            
                            

You can see that the new value is now part of the list. I can compile my project and sbt will download all the required jar files from maven central.
The idea is that you experiment with different settings and test your build on sbt command prompt. When you are happy with the settings, and you know that this is what you want, you can persist them into your build definition file.
To persist your settings into a build file, create a file named build.sbt and place all settings in that file. I have already tested and decided on following settings for my project. So, I place them into my project build file, and every time I start sbt, it will apply these settings to my project.

                                
    name := "HelloScala."
    version := "1.0"
    scalaVersion := "2.11"
    libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "2.0.2"                                     
                            

If you already have an open sbt session and you changed your build file. You can use reload command to reload settings from the build file.
If you want an exhaustive list of sbt settings, use -V option (for example settings -V), and it will show you all available options. You should spend some time to review available settings. However, if you are following all my videos, you will learn most of the frequently used settings.
One last thing. I used two types of operators to initialize the configuration in above examples.

  1. := is used for assignment. We used it for project name and version.
  2. += is used for appending. We used it for library dependency.

Some parameters take a list of values. Library dependency is one of them. If we use assignment operator for lib dependency, we will overwrite the current value. So, instead of using an assignment, we use append operator.
Now, let's look at the tasks command. The task is an action that you can perform using sbt. We have already used some of them like compile and run. Most of these tasks are obvious and self-explanatory. However, we will learn many of them as we progress with the tutorial.
Thank you for watching learning journal.
Keep learning and keep growing.


You will also like:


Scala Functions

Scala is a functional programming language. Functions are the building blocks in Scala.

Learning Journal

First Class Functions

Function is a first-class citizen in functional programming. What does it mean?

Learning Journal

Hadoop Security

Hadoop security implementation using Kerberos.

Learning Journal

Scala placeholder syntax

What is a scala placeholder syntax and why do we need it? Learn from experts.

Learning Journal

Spark in Google cloud

Learn How to Install Hadoop and Spark in Google Cloud in just 2 minuts.

Learning Journal