Ugly Stool Rotating Header Image

Scala, SBT, and IntelliJ

I started a personal project to learn Scala, and I had zero idea where to start – how do I build Hello World and then continue to scale it.  I know how to do it with make/gcc, ant/Eclipse, and VisualStudio.  From reading various Scala projects on github and it was clear that people where using some combination of sbt, maven, and IntelliJ.

I know nothing about Maven, but at first blush it is intimidating – I am sure it is my lack of knowledge.  I punted on maven.  sbt appears (stress on appears) simple, and it is written in and developers configure it by writing Scala.

The IntelliJ pick is more of an experiment to see how it compares to Eclipse.  I know not everyone thinks you should change editors.

There may be better ways to combine sbt and IntelliJ, but this one is fairly easy.  I assume sbt is already installed.

Step 1 – SBT

Create a directory to hold your project, and then create an empty project by invoking sbt.

$ mkdir test
$ cd test
$ sbt
Project does not exist, create new project? (y/N/s) y
Name: example
Organizatoin: org.example
Version [1.0]:
Scala version [2.8.1]:
sbt version [0.7.5]:
> exit

Note: SBT uses Scala version 2.7.7, but builds your project with Scala 2.8.1.

Step 2 – Build

Create a .scala file to control the building of your project, and to specify your project’s dependencies.

$ mkdir project\build
$ notepad project\build\project.scala

This file includes example dependencies for Squeryl.

import sbt._
class Project(info: ProjectInfo) extends DefaultProject(info)
      with IdeaProject {
  val mysql = "mysql" % "mysql-connector-java" % "5.1.15"
  val squeryl = "org.squeryl" % "squeryl_2.8.1" % "0.9.4-RC6"
  override def libraryDependencies = Set(
  ) ++ super.libraryDependencies

Step 3 – Plugin

Add the IntelliJ plugin to SBT.

$ mkdir project\plugins
$ notepad project\plugins\plugins.scala

Create the plugins.scala file, and add the appropriate reference.

import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
    val sbtIdeaRepo  = "sbt-idea-repo" at ""
    val sbtIdea = "com.github.mpeltonen" % "sbt-idea-plugin" % "0.4.0"

Step 4 – Build

Create a Hello World application to build, and put it in src/main/scala/.

object Program {
    def main(args: Array[String]): Unit = {
        println("Hello World")

Launch sbt, and fetch the dependencies. Execute the idea command to create an IntelliJ project.

$ sbt update
$ sbt idea
$ sbt run

Once the files have been created simply open the project using IntelliJ.  sbt can be executed from within IntelliJ using the idea-sbt-plugin

Comments are closed.

Page optimized by WP Minify WordPress Plugin