Up and Running using SBT with Eclipse or IntelliJ

If you’re new to programming with Play, Akka, Lagom, or other Scala projects, you will probably need to get familiar with SBT (Scala Build Tool). For a developer coming from Maven, using SBT may be a bit confusing at first, so this blog post aims to get you over the initial hurdle of using SBT with your IDE.

Why SBT?

First, let’s address some points on why SBT exists and remains popular in the Scala community and also how it differs from Maven.

  1. SBT is task-oriented and is more similar to ANT or Gradle rather than Maven.
  2. SBT has a lot of flexibility for the developer to build custom tasks.
  3. SBT executes tasks in parallel by default and can speed up builds.
  4. SBT build configs and tasks are written in Scala.
  5. SBT has a lot of flexibility with multi-module projects as compared to Maven which is strictly hierarchal in nature.
  6. SBT can build both Scala and Java code out of the box, but with Maven you need to add plugins to build Scala.
  7. SBT integrates with the Scala REPL making it easy to run commands against your project code.
  8. SBT handles dependencies (under the covers) with Ivy and uses the same repositories as Maven does to download artifacts.

For Maven die-hards, this list may not be convincing enough for you to want to use SBT, but my advice is to learn the basics of SBT even if you desire to stick with Maven for your Scala projects. It may give you better insights on how to use Maven later on after you understand the SBT way of doing things.

Why use Eclipse with SBT?

Eclipse is “tried and true” and remains the most popular Java IDE (in part because it’s completely free to use). Additionally, there is the Scala IDE, which is based on Eclipse, has good support from the Scala community, and can help a developer get running with Scala projects quickly. Also, the Scala IDE can be installed onto an existing Eclipse installation, if you prefer to go that route.

sbteclipse

Now, where many developers get tripped up is how to setup their SBT project for use in Eclipse. There is no “Import Project from SBT” feature present like there is for Maven. This may be surprising, but I am going to explain how to use the sbteclipse SBT plugin to setup your SBT project.

First, let’s review how importing a project works with Maven. Eclipse project files are generated for you by Eclipse’s Maven integration upon import. Also, Eclipse will add a library for Maven Managed Dependencies to your project, so when you update your pom.xml, the dependencies will refresh in Eclipse. It all works quite well and has for many years.

With sbteclipse, it works very differently. Instead, you need to add the sbteclipse plugin to your plugins.sbt. The code to add to plugins.sbt is:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")

Additionally, to download library sources and Javadoc JARs, you will need to add to your build.sbt:

EclipseKeys.withSource := true
EclipseKeys.withJavadoc := true

You must install SBT separately from Eclipse in order to use sbteclipse from the command line. On a Mac, you can use Homebrew and do brew install sbt.

Next, to generate the Eclipse project files, execute, from the command line, sbt eclipse in your project directory.

Finally, you can import Existing Projects Into Workspace (under General) to setup your project with Eclipse. The new Eclipse project will have all of the dependent JARs referenced from build.sbt included into the project, and the Scala compiler setup for you.

When you update your build.sbt dependencies, you will need to run sbt eclipse again and Refresh your project in Eclipse.

With SBT and Eclipse, you will need to go back to the command line on occasion, so working 100% in Eclipse isn’t realistic. Yet, this should be a viable solution for any developer to be productive with SBT in Eclipse.

Why IntelliJ with SBT?

Ok, so what about IntelliJ in the context of SBT? As a Maven developer, using SBT in IntelliJ works much more like you would expect with the “Import Project from SBT” capability. IntelliJ is also much more feature-packed IDE than Eclipse, and that appeals to many developers.

If you don’t need the IntelliJ Ultimate Edition, you can use the free Community Edition of IntelliJ with the IntelliJ Scala plugin.

When you are running through the IntelliJ CE installer, you can select Scala as a featured plugin:

Install Scala Plugin

Then, you can import your SBT project:

Import SBT Project
Settings for SBT Project Import

Note: Download Javadocs is missing from my Project Import even though it’s shown in the official documentation. This may only be available with the Ultimate version of IntelliJ and the Scala plugin.

What about updating the build.sbt dependencies? When you make changes to the build.sbt, IntelliJ will prompt you to reload the project and will download the new dependencies. This works similar to how Maven does from an IDE when you save changes to a pom.xml.

You should definitely read through the IntelliJ Scala Blog to find out more about features such as the integrated SBT shell, Java to Scala code converter, and Akka support (Ultimate Edition only).

Conclusion

Both Eclipse and IntelliJ can offer good experience working with SBT. For most developers, using IntelliJ is easier to get up and running, but you must be willing to learn a new IDE if you’re used to Eclipse. If you want to stick with Eclipse, that’s OK, you will just need to get a bit more familiar with using SBT from the command line to setup the project. Either way, you should get familiar with using SBT from the command line for more advanced usage.

I hope you enjoyed reading, and please follow me for more postings in the future!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.