IntelliJ IDEA 2017.3 Help


Importing an sbt project

  1. Click Import Project or Open on the welcome screen.
  2. In the dialog that opens, select a directory that contains your sbt project or simply build.sbt. Click OK.
  3. Follow the steps suggested in the Import Project wizard.
    You can use the suggested default settings since they are enough to successfully import your project.
    We recommend that you enable the Use sbt shell for build and import (requires sbt 0.13.5+) option when you use code generation or other features that modify the build process in sbt. If your sbt project is not in the IntelliJ IDEA project root directory, we suggest you skip this option.
    You can also select the appropriate option for grouping modules in your project.

Ensuring sbt and Scala versions compatibility

Often you share your project across a team and need to use a specific version of sbt.

You can override the sbt version in your project's file.

  1. Create or import your sbt project.
  2. In the Project tool window, in the source root directory locate the file and open it in the editor.
  3. In the editor explicitly specify the version of sbt that you want to use in the project.
  4. Refresh your project. (Click the refresh in the sbt projects tool window.)

Managing sbt projects

sbt project structure

When you create or import an sbt project, IntelliJ IDEA generates the following sbt structure:

  • sbt project (proper build) which defines a project and contains build.sbt file, src, and target directories, modules; anything related to a regular project.
    sbt main
  • sbt build project which is defined in the project subdirectory. It contains additional code that is part of the build definition.
    sbt source root
  • The sbt projects tool window which contains sbt tasks, commands, and settings that you can execute.
    sbt tool window new

When you work with sbt projects you use the build.sbt file to make main changes to your project since IntelliJ IDEA considers an sbt configuration as a single source of truth.
To see what common tasks you can perform via build.sbt, refer to the Commonly performed sbt tasks section.

Adding a library to the sbt project

You can add sbt dependencies via the build.sbt file or you can use the import statement in your .scala file.

  1. Open a .scala file in the editor.
  2. Specify a library you want to import.
  3. Put the cursor on the unresolved package and press Alt+Enter.
    sbt add dependency via import
  4. From the list of available intention actions, select Add sbt dependency.
  5. Follow the steps suggested in the wizard that opens and click Finish.
  6. IntelliJ IDEA downloads the artifact, adds the dependency to the build.sbt file and to the sbt projects tool window.

Working with sbt shell

An sbt shell is embedded in the sbt project and is available on your project start. You can use the sbt shell for executing sbt commands and tasks, for running, and debugging your projects.

  • To start the sbt shell, press Ctrl+Shift+S(for Windows) or Cmd+Shift+S (for Mac OS X). Alternatively, click sbt shell icon on the toolbar located on the bottom of the screen.
  • To use the sbt shell for build and import procedures, select the Use sbt shell for build and import (requires sbt 0.13.5+) option located in the sbt settings and perform steps described in the Run a Scala application using the sbt shell section. Note that sbt versions 0.13.16.+ / 1.0.3.+ are recommended.
  • To use the sbt shell for debugging, refer to the debugging with sbt shell section.
  • To run your tests from the sbt shell:
    1. Open a run/debug configuration (Run | Edit Configurations).
    2. Create a test configuration and select the use sbt option from the available settings.

Running sbt tasks

  • You can run sbt tasks by selecting the one you need from the the sbt Tasks directory in the sbt projects tool window.
  • You can manually enter your task (code completion is supported) in the sbt shell and run it directly from there.
  • You can create a run configuration for a task. For example, you can create a custom task which is not part of the list of tasks located in the sbt projects tool window.
    1. Open (Shift+Alt+F10) a run configuration.
    2. Specify the run configuration settings and click OK. If you need, you can add another configuration or a task to execute before running your configuration. Click the add icon in the Before Launch section and from the list that opens select what you need to execute.

IntelliJ IDEA displays results in the sbt shell window.

Working with sbt settings

To access sbt settings, click settings in the sbt projects tool window.
You can use sbt settings for the following notable actions:

  • If you want sbt automatically refresh your project every time you make changes to build.sbt, select Use auto-import.
  • To delegate running builds to sbt, select Use sbt shell for build and import.
  • To debug your code via the sbt shell, select Enable debugging for sbt shell option that enables a debug button (debug) in the sbt shell tool window. To start the debugging session, simply click this button. For more information on debugging, see debugging with sbt.
  • To change the .ivy cache location in your project or set other sbt properties, use the VM parameters field.

Commonly performed sbt tasks

You can link an external sbt project to the sbt project that you already have.

  1. Open your build.sbt.
  2. Specify the following code:
    val localDep = RootProject(file("/path/to/project"))
    where localDep in this case is a project that is located somewhere on the file system and will be imported as a module.
  3. Refresh your project. (Click the refresh in the sbt projects tool window.)
    IntelliJ IDEA displays the added project in the Project tool window as well as in the sbt projects tool window.

Working with an sbt multi-module project

You can add a sub project or a module that would depend on your main sbt project using the build.sbt file.

  1. Open build.sbt in the editor.
  2. Specify, for example:
    lazy val sampleModule = (project in file("sampleModule"))
    where "sampleModule" is a sub project that you want to add. You can specify more than one sub project.
  3. Refresh your project. (Click the refresh in the sbt projects tool window.)
    IntelliJ IDEA generates a sub project directory with the appropriate information and displays it in both Project and sbt projects tool windows.

If you want the sub projects to automatically update when you change the version of Scala, specify commonSettings and settings method call for each sub project.

  1. Open build.sbt.
  2. Specify, for example, the following code:
    lazy val commonSettings = Seq( organization := "com.example", version := "0.1.0-SNAPSHOT", scalaVersion := "2.12.3" ) lazy val moduleSample = (project in file("moduleSample")) .settings( commonSettings )

    The appropriate Scala version is added to the sub project's directory in the Project tool window and also to the sbt projects tool window as a dependency in the sub project.

Working with dependencies in sbt projects

IntelliJ IDEA lets you use external dependencies in your sbt project.

You can use both managed dependencies and unmanaged dependencies:

  • Managed dependencies are the ones that are added through build.sbt and managed by sbt.
  • The unmanaged dependencies are the ones that you add to the lib directory in your project and manage manually.

We recommend that you use build.sbt for configuring all of the dependencies to ensure portability of the project.

  1. Open build.sbt in the editor.
  2. Specify your dependency which have the following syntax:
    libraryDependencies += groupID % artifactID % revision
    You can also declare several dependencies:
    libraryDependencies ++= Seq( groupID % artifactID % revision, groupID % otherID % otherRevision )
    You can also configure dependencies externally in the Maven POM file or Ivy configuration file and let sbt use those files. For more information, please refer to the sbt Library Management page.
Last modified: 6 March 2018