Introduction

SBT (Simple Build Tool) is a popular build tool for Scala projects. It is used to compile, run, and test Scala code, manage dependencies, and automate various tasks in the development lifecycle. SBT is highly customizable and integrates well with other tools and libraries in the Scala ecosystem.

Key Concepts

  1. Build Definition

  • Build.sbt: The main build definition file where you specify project settings, dependencies, and tasks.
  • Project Directory: Contains additional configuration files and project-specific settings.

  1. Dependencies

  • Library Dependencies: External libraries that your project depends on.
  • Resolvers: Repositories where SBT looks for dependencies.

  1. Tasks and Commands

  • Tasks: Actions that SBT can perform, such as compiling code or running tests.
  • Commands: Instructions you give to SBT to execute tasks.

Setting Up SBT

Installation

  1. Download and Install SBT:
    • Visit the SBT download page and follow the instructions for your operating system.
    • Verify the installation by running sbt sbtVersion in your terminal.

Creating a New Project

  1. Create a Project Directory:

    mkdir my-scala-project
    cd my-scala-project
    
  2. Initialize SBT:

    sbt new scala/scala-seed.g8
    
  3. Project Structure:

    my-scala-project/
    ├── build.sbt
    ├── project/
    ├── src/
    │   ├── main/
    │   │   └── scala/
    │   └── test/
    │       └── scala/
    └── target/
    

Writing the Build Definition

Basic build.sbt File

name := "MyScalaProject"

version := "0.1.0"

scalaVersion := "2.13.6"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % Test

Explanation

  • name: The name of your project.
  • version: The version of your project.
  • scalaVersion: The version of Scala to use.
  • libraryDependencies: External libraries your project depends on. In this case, ScalaTest for testing.

Common SBT Commands

Running SBT

  • Start SBT Shell:
    sbt
    

Compilation

  • Compile the Project:
    compile
    

Running the Project

  • Run the Main Class:
    run
    

Testing

  • Run Tests:
    test
    

Dependency Management

  • Update Dependencies:
    update
    

Practical Example

Example Project: Hello World

  1. Create a New Project:

    sbt new scala/scala-seed.g8
    
  2. Edit build.sbt:

    name := "HelloWorld"
    
    version := "0.1.0"
    
    scalaVersion := "2.13.6"
    
  3. Create Main Class:

    • Create a file src/main/scala/HelloWorld.scala:
      object HelloWorld extends App {
        println("Hello, World!")
      }
      
  4. Run the Project:

    sbt run
    

Output

[info] running HelloWorld 
Hello, World!

Exercises

Exercise 1: Adding a Dependency

  1. Task: Add the json4s library to your project.
  2. Solution:
    • Edit build.sbt:
      libraryDependencies += "org.json4s" %% "json4s-native" % "3.6.11"
      

Exercise 2: Creating a Test

  1. Task: Write a test for the HelloWorld object.
  2. Solution:
    • Add ScalaTest dependency in build.sbt:
      libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % Test
      
    • Create a test file src/test/scala/HelloWorldTest.scala:
      import org.scalatest.flatspec.AnyFlatSpec
      
      class HelloWorldTest extends AnyFlatSpec {
        "HelloWorld" should "print Hello, World!" in {
          assert(HelloWorld.main(Array()) == "Hello, World!")
        }
      }
      

Conclusion

In this section, you learned about SBT, its key concepts, and how to set up and manage a Scala project using SBT. You also practiced adding dependencies and writing tests. Understanding SBT is crucial for efficient Scala development, and mastering it will significantly enhance your productivity.

© Copyright 2024. All rights reserved