How to configure Scala and SBT for Eclipse

This guide is about how to install Scala and SBT to be used together with Eclipse. I have used latest Eclipse 4.2 for this but in general it should not matter what version you use.

Step 1: Download and install Eclipse

Go to www.eclipse.org and download Eclipse. I generally go for Eclipse classic as I like to configure my environment myself. Link to download page is (732) 709-0950.
When download finishes install it by extracting and move the extracted folder to a place of your choice. Create a shortcut for easy access to Eclipse.

Step 2: Install Scala IDE for Eclipse

Scala IDE provides nice features for scala development on Eclipse. Installing is done from Eclipse’s “Help -> Install new software…”. Go to scala-ide.org to get the URL for the version you would like to install, since I use Eclipse Juno I will pick the current nightly build that is available supporting Juno. Paste the link in the “Install new software…” window of Eclipse and install the plugin.
If you have problems installing, try unchecking “Contact all update sites” and disable your firewall/anti-virus software during installation.

When asked to restart Eclipse, choose “Yes” and when restarted there will be a dialogue window popping up asking to run setup diagnostics, choose “Yes”. Mark “Use recommended default settings” and press “OK”. There should have been a text telling you about heap size, to change the heap size edit your eclipse.ini file.

If the installation went successfully then you should now be able to use Scala perspective from “Window -> Open perspective -> Other… ->Scala”. There should now be a Scala perspective next to your Java perspective in the top right corner of Eclipse window.

Step 3: Download and install Typesafe stack

Download the typesafe stack here and install it. Only SBT will be used in this guide, but install all components in the typesafe stack anyway.
Once installed, check that installation was successful by opening a terminal and typing “sbt”. If the installation went well this should start downloading Scala from a remote repository. If there are problems accessing the remote repository it can be due to you being behind a proxy or your firewall/anti-virus preventing access. To make sbt work through a proxy you need to add

-Dhttp.proxyHost=urlToProxy -Dhttp.proxyPort=portNumber

to your sbt.bat file right after “_JAVA_OPTS=”.

To leave the sbt prompt, type exit.

Step 4: Create directory structure for SBT

SBT uses same directory structure as maven for source code. Decide where you want your project root to be, I will use Eclipse’s default “workspace” as project root. I will call my project “myProject” and place it inside workspace. Then create the following directories:

myProject/src/main/scala
myProject/src/main/java
myProject/src/main/resources
myProject/src/test/scala
myProject/src/test/java
myProject/src/test/resources
myProject/lib
myProject/project

Step 5: Create .sbt files for your project

You will need to create two files. First one should be named build.sbt and be placed in “/myProject”. The file should contain the following:

name := “myProject”

version := “0.0.1″

scalaVersion := “2.9.2″

Note that the empty lines inbetween are required.

Second file should be called plugins.sbt and be placed under “/myProject/project”. In that file add

addSbtPlugin(“com.typesafe.sbteclipse” % “sbteclipse-plugin” % “2.0.0″)

This plugin will be used for converting your project into an Eclipse project by creating .project and .classpath files

Step 6: Generate and import Eclipse project

From a terminal enter “/myProject” directory. Type sbt, this should start downloading a bunch of things from the repository, including the sbteclipse tool that will be used for generating an Eclipse project. When download finishes, type “eclipse” in sbt prompt. This will generate the necessary files. Again if you have problems downloading this can be due to firewall/anti-virus software, try disabling those.

When the files have been generated go to Eclipse “File -> Import… -> General -> Existing Project into Eclipse” and pick “myProject”.  This imports your project into Eclipse with all the sbt goodies and scala nature!

Currently there seems not to be any good plugin for integration of SBT into Eclipse, but do not worry, SBT is a great tool and easy to use. For example lets add Akka as a dependency to SBT. Add the following lines to your build.sbt.

resolvers += “Typesafe Repository” at “/repo.typesafe.com/typesafe/releases/”

libraryDependencies += “com.typesafe.akka” % “akka-actor” % “2.0.2″

This will add the akka dependency for sbt to manage.

Now from sbt prompt hit the following commands: reload, update, eclipse.  Then go to Eclipse and do refresh on your project. This should cause the “referenced libraries” to show up, and akka-actor-2.0.2.jar should be found inside.

That was all, hope this is of any help to everyone that reads this!