Building a Scala Project with IntelliJ and sbt


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”.

  1. 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”
  2. 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
  3. Make sure the JDK version is 1.8 and the sbt version is at least 0.13.13
  4. Select Use auto-import so dependencies are automatically downloaded when available
  5. 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

  1. On the Project panel on the left, expand SbtExampleProject => src => main
  2. Right-click scala and select New => Package
  3. Name the package example and click OK (or just press the Enter or Return key).
  4. Right-click the package example and select New => Scala class (if you don’t see this option, right-click the SbtExampleProject, click Add Frameworks Support, select Scala and proceed)
  5. Name the class Main and change the Kind to Object.
  6. 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

  1. From the Run menu, select Edit configurations
  2. Click the + button and select sbt Task.
  3. Name it Run the program.
  4. 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.
  5. Click OK.
  6. On the Run menu. Click Run ‘Run the program’.
  7. In the code, change 75 to 61 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.

  1. 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.


Contributors to this page: