Home » Scala » The SBT Tutorial

The SBT Tutorial

Welcome to my first blog. As you known, while working with progamming langues, one of the tools that you have to get used to is a build tool. SBT is stand for Simple Build Tool that is a general purpose build tool written in Scala for JVM developers.

Features of SBT:
* Little or no configuration required for simple projects
* Scala-based build defination that can use the full flexibility of Scala code
* Accurate incremental recompilation using information extracted from the compiler
* Continuous compilation and testing with triggered execution
* Packages and publishes jars
* Generates documentation with scaladoc
* Supports mixed ScaONla/Java projects
* Supports testing with ScalaCheck, specs, and ScalaTest. JUnit is supported by a plugin.
* Starts the Scala REPL with project classes and dependencies on the classpath
* Modularization supported with sub-projects
* External project support (list a git repository as a dependency!)
* Parallel task execution, including parallel test execution
* Library management support: inline declarations, external Ivy or Maven configuration files, or manual management

Wow, as you can see, sbt has a lot of features. In this tutorial, I will show you some very common features that every Scala project uses.

Firstly


Open your terminal and please check that sbt is installed by command


If you see the version of sbt (in this example, it is 0.13.6), it means you have installed. Otherwise, you have to install sbt by commands


Once install success, you can go to next step.

Secondly


We create a simple sbt project by commands


Inside the hellosbt directory, we create a new file build.sbt to house the build script. The content of build.sbt is shown below:


You must to notice that each line above is separated with a blank line, otherwise, you will get some errors when you use next commands.

Now, you run the sbt command:


You are now inside the sbt shell, so you can run many sbt tasks. To view all the tasks available, you can use help task:


If you already use Maven, you will see that by default, sbt follows Maven project layout. Scala source files are placed inside src/main/scala and test source files are placed inside src/test/scala.
Let’s make two this directory by commands after you run a new terminal inside the hellosbt directory.


OK, we will write some code to hello sbt 😀
Now, let’s create a new Scala file HelloSbt.scala inside src/main/scala and place the following contents in it


You can run the code from inside the sbt shell by first compiling the code using “compile” command task, and then running it using the run task as shown below:


Thirdly


When you write some codes, it is necessary to write test code. So, let’s figure out how sbt run test code.
You must add dependencies for scalatest by adding the following line to build.sbt


After that, in sbt shell, you call reload task to update dependencies


We create a new file CheckValidNumber.scala inside src/main/scala that contains the following contents:


We also create a new file TestCheckValidNumber.scala inside src/test/scala and place the following contents in it:


To run the test you can execute the test task


As you can see, we have passed 1 test.

One of the coolest features of sbt is that it can rerun your tasks without manual intervention whenever any project source file changes. This is enabled using the ~ operator. If you prefix any sbt task with ~ then sbt will wait for changes in the source files. As soon as any file changes, it will rerun that task.

Type the ~test command inside sbt shell


Let’s write new test inside TestCheckValidNumber.scala file


Save file and open to sbt shell you will see some information as shown below


We have 1 successful test and 1 unsuccessful test.

To pass this fail test, let’s add the actual implementation to positiveNumber method inside CheckValidNumber.scala


Save file and go back to sbt shell, you will see the result:


All tests passed

Finally


Now you know that how sbt project run and test. So you can make some projects with sbt by yourself. To get more acknowledge about sbt, please visite SBT Document

References


  1. SBT Document
  2. Tutorial on github

Tagged width:,