Creating a Scala Project with SBT and Working in Eclipse


Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

There are numerous view points on the use of Maven or sbt for creating Scala projects. If you have been working on Java projects for a long time then your favorite has to be Maven unless it is Ant. Still Ant 😉 But there are several advantages of using sbt, including the original one that it is created for Scala and has a lot of features to support the language.

Lets quickly get started with installing sbt

  1. Download sbt-launch.jar
  2. Copy the launcher to your ~/bin folder. For example for me, I copied it to
    • vikas@vikas-laptop:~$ sudo mv sbt-launch-0.7.7.jar /usr/local/bin/sbt-launcher.jar
  3. Create a script called sbt
    • echo “java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar “$@”” | sudo tee /usr/local/bin/sbt
  4. Make the script executable
    • sudo chmod +x /usr/local/bin/sbt

Now once you execute sbt, you should see the following

vikas@vikas-laptop:~$ sbt
Project does not exist, create new project? (y/N/s)</pre>

To create a new project, create a new directory and execute the sbt command there

vikas@vikas-laptop:~/w/inphina/scala$ mkdir HelloInphina
vikas@vikas-laptop:~/w/inphina/scala$ cd HelloInphina/
vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ sbt
Project does not exist, create new project? (y/N/s)

Now keep answering the questions one by one

vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ sbt
Project does not exist, create new project? (y/N/s) y
Name: HelloInphina
Organization: Inphina
Version [1.0]:
Scala version [2.9.0]:
sbt version [0.7.7]:
Getting net.java.dev.jna jna 3.2.3 ...
:: retrieving :: org.scala-tools.sbt#boot-app
	confs: [default]
	1 artifacts copied, 0 already retrieved (838kB/15ms)
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
	confs: [default]
	2 artifacts copied, 0 already retrieved (9911kB/25ms)
Getting org.scala-tools.sbt sbt_2.7.7 0.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-app
	confs: [default]
	17 artifacts copied, 0 already retrieved (4379kB/41ms)
[success] Successfully initialized directory structure.
Getting Scala 2.9.0 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
	confs: [default]
	4 artifacts copied, 0 already retrieved (20442kB/45ms)
[info] Building project HelloInphina 1.0 against Scala 2.9.0
[info]    using sbt.DefaultProject with sbt 0.7.7 and Scala 2.7.7
>

and finally your project is ready. Note, that it may take some time to get the project dependencies as they are downloaded. At this time, you are still in the sbt prompt and can issue sbt commands. For example, you would write > run and it would do the following

> run
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == compile ==
[info]   Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info]   Post-analysis: 0 classes.
[info] == compile ==
[info]
[info] == run ==
[info] == run ==
[error] Error running run: No main class specified.
[info]
[info] Total time: 0 s, completed 19 May, 2011 3:39:02 PM
>

Let us come out of sbt with cntrl+c, on Ubuntu at least.

Let us now create a simple scala program for HelloInphina.

vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ cd src/main/scala/
vikas@vikas-laptop:~/w/inphina/scala/HelloInphina/src/main/scala$ gedit HelloInphina.scala

Let the object have the following code

object HelloInphina {
  def main(args: Array[String]) {
    println("Hello, Inphina!")
  }
}

Now once you do sbt run at the root of your project, you should get the following

vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ sbt run
[info] Building project HelloInphina 1.0 against Scala 2.9.0
[info]    using sbt.DefaultProject with sbt 0.7.7 and Scala 2.7.7
[info]
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == run ==
[info] Running HelloInphina
<strong>Hello, Inphina</strong>!
[info] == run ==
[success] Successful.
[info]
[info] Total time: 6 s, completed 19 May, 2011 3:45:06 PM
[info]
[info] Total session time: 7 s, completed 19 May, 2011 3:45:06 PM
[success] Build completed successfully.
vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$

So far so good. Now you have 2 options, either to continue with an editor and continue like this or move to an IDE. I chose the latter and moved to the Eclipse IDE 3.6.2 with Scala IDE 2.0.0-beta4

Option 1:

One quick way to move to eclipse is to execute

vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ sbt make-pom

This would create a pom file for you in the target folder. Next move the pom from the target to the root of your project
vikas@vikas-laptop:~/w/inphina/scala/HelloInphina$ mv target/scala_2.9.0/helloinphina_2.9.0-1.0.pom pom.xml

and now do the standard mvn eclipse:eclipse and import into eclipse

This is easy but the caveat that I found with this approach are that, my project does not have the Scala nature. I had to give that to the project. Also the src/main/scala and src/test/scala were not added as source folders. Also the name of the project had a weird scala version extension to it. But, nevertheless, the project was in eclipse.

Option 2:

The second approach is a bit involved and makes use of the plugin SbtEclipsify. For this, we would have to do the following

a) Create a plugins directory
then create the plugins directory mkdir MySbtProject/project/plugins
vikas@vikas-laptop:~/w/inphina/scala/HelloInphina/project$ mkdir plugins

b) next create a file name MySbtProjectPlugins.scala and add the following text to it:

 import sbt._

 class MySbtProjectPlugins(info: ProjectInfo) extends PluginDefinition(info) {
       lazy val eclipse = "de.element34" % "sbt-eclipsify" % "0.7.0"
 }

This will enable your project to get the plugin in order to use it you need to add it to your project defintion.

c) Create another folder called build in project
Create your project definition file, something like “MySbtProject.scala” in “project/build/” folder, and add the Eclipsify trait.

import sbt._
import de.element34.sbteclipsify._

class MySbtProject(info: ProjectInfo) extends DefaultProject(info) with Eclipsify {
  // the project definition here
}

d) Now at the project root do sbt reload

After reloading the project you should have a new action named “eclipse” which will generate a .project and a .classpath file in the MySbtProject folder.

e) finally execute sbt eclipse

Now import in eclipse. With this approach my Scala nature was preserved. The only problem that I saw was that this time /src/test/resources and /src/main/resources were not added to the source folders by default. But overall a lesser problem.

The advantage of this approach is also that other members of the team who are rooting for Idea do not get the .project and .classpath files checked into the repository. So this approach sounds more maven’ish.

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. Knoldus does niche Reactive and Big Data product development on Scala, Spark and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged , , . Bookmark the permalink.

2 Responses to Creating a Scala Project with SBT and Working in Eclipse

  1. qooyend says:

    Worked really nice. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s