DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > SBT Basics

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.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
May. 03, 17 · Java Zone · Tutorial
Like (4)
Save
Tweet
13.71K Views

Join the DZone community and get the full member experience.

Join For Free

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(".")).
settings(
    hello := { println("Hello!") },
    inThisBuild(
        List(
            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.

>sbt
> hello
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.

Task (computing)

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Best JavaScript Web Development Frameworks
  • Is NoOps the End of DevOps?
  • Artificial Intelligence (AI) And Its Assistance in Medical Diagnosis
  • Major PostgreSQL Features You Should Know About

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo