In this tutorial, we’ll see how to build a Scala project using sbt. sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file. We assume you’ve completed the first tutorial.
Creating the project
In this section, we’ll show you how to create the project in IntelliJ. However, if you’re comfortable with the command line, we recommend you try Getting Started with Scala and sbt on the Command Line and then come back here to the section “Writing Scala code”.
- If you didn’t create the project from the command line, open up IntelliJ and select “Create New Project”
- On the left panel, select Scala and on the right panel, select sbt
- Click Next
- Name the project “SbtExampleProject”
- If you already created the project on the command line, open up IntelliJ, select Import Project and open the
build.sbt
file for your project - Make sure the JDK version is 1.8 and the sbt version is at least 0.13.13
- Select Use auto-import so dependencies are automatically downloaded when available
- Select Finish
Understanding the directory structure
sbt creates many directories which can be useful once you start building more complex projects. You can ignore most of them for now but here’s a glance at what everything is for:
- .idea (IntelliJ files)
- project (plugins and additional settings for sbt)
- src (source files)
- main (application code)
- java (Java source files)
- scala (Scala source files) <-- This is all we need for now
- scala-2.12 (Scala 2.12 specific files)
- test (unit tests)
- target (generated files)
- build.sbt (build definition file for sbt)
Writing Scala code
- On the Project panel on the left, expand
SbtExampleProject
=>src
=>main
- Right-click
scala
and select New => Package - Name the package
example
and click OK (or just press the Enter or Return key). - Right-click the package
example
and select New => Scala class (if you don’t see this option, right-click theSbtExampleProject
, click Add Frameworks Support, select Scala and proceed) - Name the class
Main
and change the Kind toObject
. - Change the code in the class to the following:
@main def run() =
val ages = Seq(42, 75, 29, 64)
println(s"The oldest person is ${ages.max}")
Note: IntelliJ has its own implementation of the Scala compiler, and sometimes your code is correct even though IntelliJ indicates otherwise. You can always check to see if sbt can run your project on the command line.
Running the project
- From the Run menu, select Edit configurations
- Click the + button and select sbt Task.
- Name it
Run the program
. - In the Tasks field, type
~run
. The~
causes sbt to rebuild and rerun the project when you save changes to a file in the project. - Click OK.
- On the Run menu. Click Run ‘Run the program’.
- In the code, change
75
to61
and look at the updated output in the console.
Adding a dependency
Changing gears a bit, let’s look at how to use published libraries to add extra functionality to our apps.
- Open up
build.sbt
and add the following line:
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"
Here, libraryDependencies
is a set of dependencies, and by using +=
,
we’re adding the scala-parser-combinators dependency to the set of dependencies that sbt will go
and fetch when it starts up. Now, in any Scala file, you can import classes,
objects, etc, from scala-parser-combinators with a regular import.
You can find more published libraries on
Scaladex, the Scala library index, where you
can also copy the above dependency information for pasting into your build.sbt
file.
Next steps
Continue to the next tutorial in the getting started with IntelliJ series, and learn about testing Scala code in IntelliJ with ScalaTest.
or
- The Scala Book, which provides a set of short lessons introducing Scala’s main features.
- The Tour of Scala for bite-sized introductions to Scala’s features.
- Continue learning Scala interactively online on Scala Exercises.