Thursday, January 17, 2013

Scala + Gradle + emacs

It's been a while since I've been able to use emacs as a primary development environment. Since the eclipse development environment isn't all that great for scala, I thought I'd see how well emacs served in that role. First we'll set up emacs and scala. These directions are based on this blog post.
  1. Install scala-mode2. The remainder of the post assumes you have used the preferred installation method, the emacs package manager.
  2. Download ensime and install it in ~/.emacs.d
  3. Add the following to ~/.emacs.d/init.el (or ~/.emacs if you prefer):
    (add-to-list 'load-path "~/.emacs.d/ensime/elisp")
    (require 'scala-mode2)
    (require 'ensime)
    (add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
  4. Once you have a .ensime file for your scala project, you can run M-x ensime, and you're off!
Ideally, you'd be generating your .ensime from a build.gradle (or build.sbt or whichever tool you prefer) rather than writing it from scratch. From gradle, you would follow the directions for the gradle-ensime plugin. The directions in the  project aren't particularly good, and even though it has a configuration that doesn't match your general project specification, things still seem to work. Here's the step-by-step:
  1. Download gradle-ensime.
  2. In gradle-ensime, run ./gradlew install
  3. In your build.gradle, add the following towards the top of the file:
    buildscript {
      repositories { mavenLocal() }
      dependencies {
        classpath group: 'net.coacoas', name: 'gradle-ensime', version: '0.1.0-SNAPSHOT'
    apply plugin: 'ensime'
  4. In your project directory, now run <gradle-ensime-dir>/gradlew ensime
  5. You should now have a .ensime file that you can use when you M-x ensime, in emacs.

[Edit: ensime prefers scala-mode2 over scala-mode, so the instructions now reflect that.]