IntelliJ IDEA 2021.3 Help

sbt

Open an existing sbt project

  1. If no project is currently opened in IntelliJ IDEA, click Open on the welcome screen. Otherwise, select File | Open from the main menu.

  2. In the dialog that opens, select a file that contains your sbt project description build.sbt. Click OK.

  3. In the dialog that opens, click Select as Project.

    IntelliJ IDEA opens and syncs the sbt project in the IDE. If you need to adjust importing options when you open the project, refer to the sbt settings.

Ensure 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 build.properties file.

  1. Create or open your sbt project.

  2. In the Project tool window, in the source root directory, locate the build.properties 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.

    sbt.version=xxx
  4. Reimport your project. (Click the Icons actions refresh in the sbt tool window.)

sbt project structure

When you create or import the 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.

    Project tool window
  • 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 tool window which contains sbt tasks, commands, and settings that you can execute.

    sbt tool window

Manage sbt projects

When you work with sbt projects you use the build.sbt file to make main changes to your project since IntelliJ IDEA considers the sbt configuration as a single source of truth.

Every time you manually change build.sbt in the editor, you need to load the changes. IntelliJ IDEA displays a nofication icon in the right part of the editor suggesting to Load sbt Changes made to the project (Ctrl+Shift+O).

build.sbt file: Load sbt Changes

If you want to control the importing process of your project, you can manually trigger the action or configure the auto-reloading process.

Reload an sbt project

  1. In the sbt tool window, right-click a linked project.

  2. From the context menu, select Reload project the Reload project icon.

    On invoking this action, IntelliJ IDEA parses the project structure in the sbt tool window.

    IntelliJ IDEA cannot reload just a part of your project, it reloads the whole project including subprojects and dependencies.

Configure the auto-reload

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), go to Build, Execution, Deployment | Build Tools.

    Alternatively, In the sbt tool window, click Build Tools Settings and select the Auto-Reload Settings option.

    the sbt tool window
  2. In the Build tools settings, specify the following options:

    the Build Tools settings
    • Reload changes in the build scripts: this option is selected by default. If you want to disable the auto-reload and manually control the reloading process, unselect this checkbox.

    • Any changes: select this option if you want to automatically reload the project after any changes you make to build.sbt or external changes.

      Every time you manually change the sbt build script in the editor, you need to load the changes. IntelliJ IDEA displays a nofication icon in the right part of the editor suggesting to Load sbt Changes made to the project (Ctrl+Shift+O).

      sbt build script

      With the Any changes option, IntelliJ IDEA reloads all the changes automatically.

    • External chages: when you select this option, IntelliJ IDEA automatically reloads the project only after the VCS changes and changes made to the build files outside the IDE.

  1. Open your build.sbt.

  2. Specify the following code:

    val localDep = RootProject(file("/path/to/project"))

    localDep in this case is a project that is located somewhere on the file system and will be imported as a module.

  3. Reload your project. (Click the Icons actions refresh in the sbt tool window.)

    IntelliJ IDEA displays the added project in the Project tool window as well as in the sbt tool window.

Add a sub-project or a module to the sbt project

  1. Open build.sbt in the editor.

  2. Specify, for example:

    lazy val sampleModule = (project in file("sampleModule"))

    "sampleModule" in this case is a sub-project that you want to add. You can specify more than one sub-project.

  3. Reimport your project. (Click the Reimport project in the sbt tool window.)

    IntelliJ IDEA generates a sub-project directory with the appropriate information and displays it in both Project and sbt tool windows.

Update sub-projects

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.6" ) 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 to the sbt tool window as a dependency in the sub-project.

Add 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 caret at the unresolved package and press Alt+Enter.

    add a dependency
  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.

    IntelliJ IDEA downloads the artifact, adds the dependency to the build.sbt file and to the sbt tool window.

Work 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 ⌘+⇧+S (for Mac OS X). Alternatively, click sbt shell on the toolbar located at the bottom of the screen.

  • To use the sbt shell for build and import procedures, select the Use sbt shell section located in the sbt settings and perform steps described in the Run a Scala application using the sbt shell section.

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

Run sbt tasks

  • You can run sbt tasks by selecting the one you need from the sbt Tasks directory in the sbt 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 tool window.

    1. Press Alt+Shift+F10 to open 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.

  • When you work in the distraction-free mode (without toolbars and tool windows), you can run sbt tasks or commands from the Run Anything window. Press Ctrl twice to open it, type your command and press Enter.

IntelliJ IDEA displays results in the sbt shell window.

Work with sbt settings

Use the sbt settings to configure the build and run actions the sbt project, an sbt version, importing of the project's changes, and so on.

Access the sbt settings

  1. Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment| sbt.

  2. Alternatively, click sbt Settings on the toolbar of the sbt tool window to access the sbt settings.

  3. On the sbt settings page, configure the following notable actions:

    • To delegate running builds to sbt, next to Use sbt shell, select the for imports and for builds options.

    • To debug your code via the sbt shell, select Enable debugging for sbt shell option that enables the debug button Icons actions start debugger in the sbt shell tool window.

      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.

  4. Click OK to save the changes.

To check the most common sbt issues and workarounds, see the sbt troubleshooting section.

Last modified: 01 August 2022