How to configure SBT to work with Eclipse

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.7, “How to configure SBT to work with Eclipse.”

Problem

You want to use Eclipse with a project you’re managing with SBT.

Solution

Use the Scala IDE for Eclipse project so you can work on Scala projects in Eclipse, and use the “sbteclipse” plug-in to enable SBT to generate files for Eclipse.

The Scala IDE for Eclipse project lets you edit Scala code in Eclipse. With syntax highlighting, code completion, debugging, and many other features, it makes Scala development in Eclipse a pleasure.

To use the sbteclipse plug-in, download it per the instructions on the website. Once installed, when you’re in the root directory of an SBT project, type sbt eclipse to generate the files Eclipse needs. You may see a lot of output the first time you run the command as SBT checks everything it needs, but at the end of the output you should see a “success” message, like this:

$ sbt eclipse
[info] Successfully created Eclipse project files for project(s):
[info] YourProjectNameHere

The plug-in generates the two files Eclipse needs, the .classpath and .project files.

Once these files are generated, go to Eclipse and follow the usual steps to import a project into the Eclipse workspace: File → Import → Existing Projects into Workspace. Your project will then appear in the Eclipse Navigator, Project Explorer, Package Explorer, and other views.

Discussion

The .classpath file is an XML file that contains a number of <classpathentry> tags, like this:

<classpath>
  <classpathentry output="target/scala-2.10/classes" path="src/main/scala" kind="src"></classpathentry>
  <classpathentry output="target/scala-2.10/classes" path="src/main/java" kind="src"></classpathentry>
  <classpathentry output="target/scala-2.10/test-classes" path="src/test/scala" kind="src"></classpathentry>
  <classpathentry output="target/scala-2.10/test-classes" path="src/test/java" kind="src"></classpathentry>
  <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"></classpathentry>
  <classpathentry
   path="/Users/Al/.ivy2/cache/com.typesafe/config/bundles/config-1.0.0.jar"
   kind="lib"></classpathentry>
  <classpathentry path="org.eclipse.jdt.launching.JRE_CONTAINER" kind="con"></classpathentry>
  <classpathentry path="bin" kind="output"></classpathentry>
</classpath>

The .project file is an XML file that describes your project and looks like this:

<projectDescription>
  <name>YourProjectName</name>
  <buildSpec>
    <buildCommand>
      <name>org.scala-ide.sdt.core.scalabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.scala-ide.sdt.core.scalanature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>
</projectDescription>

Any time you update your SBT build definition files (build.sbt, project/*.scala, project/*.sbt) you should rerun the sbt eclipse command to update the .classpath and .project files. Eclipse will also need to know that these files were regenerated, so this is really a two-step process:

  • Run sbt eclipse from the command line
  • In Eclipse, select your project and then refresh it (using the F5 function key, or refreshing it with the menu commands)

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.