Over a million developers have joined DZone.

SBT Basics

DZone 's Guide to

SBT Basics

If you're using Scala at all, then you'll need to make sure you have the basics of SBT down. Here's a primer for the SBT build tool.

· Java Zone ·
Free Resource

SBT is the de facto build tool in the Scala community. If you're used to other build tools, you will be familiar with the commands:

  • clean: Deletes files produced by the build, such as generated sources, compiled classes, and task caches.
  • compile: Compiles sources.
  • test: Executes all tests.
  • package: Produces the main artifact, such as a binary JAR. This is typically an alias for the task that actually does the packaging.
  • help: Displays this help message or prints detailed help on requested commands (run 'help').
  • console: Starts the Scala interpreter with the project classes on the classpath.

Then we have extra commands, suchs as:

  • run: Runs a main class, passing along arguments provided on the command line.
  • tasks: Lists the tasks defined for the current project.
  • reload: (Re)loads the current project or changes to plugins project or returns from it.
  • console: Starts the Scala interpreter with the project classes on the classpath.

A key functionality is found with the new command.

For example, by using new, we can create a project from the template specified (for example scala-seed.g8) using giter8.

sbt new scala/scala-seed.g8

Minimum Scala build. 

name [My Something Project]: hello

Template applied in ./hello

The previous snippet creates a project called hello.

The file build.sbt holds a sequence of key-value pairs called setting expressions. The left-hand side is a key and the right hand side is the body.

There are three types of keys

  • SettingKey[T]: a key for a value computed once (the value is computed when loading the subproject, and kept around).
  • TaskKey[T]: a key for a value, called a task, that has to be recomputed each time, potentially with side effects.
  • InputKey[T]: a key for a task that has command line arguments as input. Check out Input Tasks for more details.

For example, if we want to add an extra task to our previous project, which prints hello, then we can add the following lines to the build.sbt file.

import Dependencies._

lazy val hello = taskKey[Unit]("An example task")

lazy val root = (project in file(".")).
    hello := { println("Hello!") },
            organization := "com.example",
            scalaVersion := "2.12.2",
            version := "0.1.0-SNAPSHOT"
    name := "Hello",
    libraryDependencies += scalaTest % Test

We can either run the task or ask for more info about the task.

> hello
[success] Total time: 0 s, completed May 1, 2017 6:08:36 PM
> help hello
An example task

Depending on the project and the plugin used, there would be extra tasks and settings defined.

sbt ,scala ,java ,build tools ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}