Sunday, April 21, 2013

Scala with IntelliJ IDEA setup

So, I'm not a Scala IDE fan. Well, actually, I just can't get anything done in Eclipse, but love Scala. Unfortunately, IntelliJ IDE + ScalaPlugin + SBT has proven to be a bit tempermental, so I figured I'd document at least one successful setup here. The codebase I used for this example configuration is freely available at https://github.com/jasonnerothin/clockwork.git, though the code there probably isn't that interesting in and of itself.

The base state I'm working from:
1. A valid install of Scala version 2.9.2 (I have most of $SCALA_HOME/bin linked through .bash_profile, though I don't think that's strictly necessary.)
2. A valid install of sbt. I use version 0.12.0 and have it on my CLI as well.
3. An install of IntelliJ Idea + Scala plugin (I am currently at 12.1.1 Ultimate)
4. A JDK 7 or greater. (Since I'm on a MAC and Oracle's version of 1.7 is a little broken, I had to build an OpenJDK version to get to a decent state.)
5. Some scala codebase with a working build.sbt in place.

Here's mine:

name := "clockwork"
version := "0.0.1"
organization := "com.jasonnerothin"
scalaVersion := "2.9.2"
resolvers ++= Seq(
                    "snapshots"   at "http://oss.sonatype.org/content/repositories/snapshots",
                    "releases"    at "http://oss.sonatype.org/content/repositories/releases",
                    "central"     at "http://repo1.maven.org/maven2"
                )
scalacOptions ++= Seq("-deprecation", "-unchecked")
retrieveManaged := true
libraryDependencies ++= {
  val jodaConvertVersion = "1.2"
  val jodaTimeVersion   = "2.0"
  val junitVersion      = "4.8.2"
  val logbackVersion    = "1.0.6"
  val mockitoVersion    = "1.9.0"
  val scalaTestVersion  = "1.8"
  Seq(
    "ch.qos.logback"    % "logback-classic"         % logbackVersion            % "compile",
    "joda-time"         % "joda-time"               % jodaTimeVersion           % "test",
    "org.joda"          % "joda-convert"            % jodaConvertVersion        % "test",
    "junit"             % "junit"                   % junitVersion              % "test",
    "org.mockito"       % "mockito-all"             % mockitoVersion            % "test",
    "org.scalatest"     % "scalatest_2.9.2"         % scalaTestVersion          % "test"
  )
}

Once all of that is in place, typing 'sbt' from the command line in your project's base directory should get you something like the following:

So, all that would be well and good, but I'm not a SublimeText guy like all the cool kids and really enjoy my code completion and even admit to using a debugger crutch when particularly weak (only for debugging tests of course).

After starting IDEA and installing the scala plugin, you can create a new project using the new project dialog. (In my case after cloning the github clockwork project as clockwork2…) Just point the working directory at the newly cloned workspace.

Note that I'm choosing the same scala and scala compiler version (2.9.2) as is specified in my build.sbt file.

Opening a test file reveals that Idea's a little confused because it doesn't have it's classpath correctly configured:

But repeating the sbt test process (detailed above) creates a few new entries in the project view. In particular, one called "lib_managed":

Selecting each jar (Cmd-Select in Mac) and right click -> Add As Library…:

…adds the libraries we need to the classpath.

From this point, you can right click on a test and hit Run…

FTM!

No comments:

Post a Comment