20106 feb

Quick iterations with Scala, JRebel, and Maven

As a developer who’s followed a path from Java through Ruby to Scala, I miss the quick turnaround time an interpreted language like Ruby enables: change your code, hit the up arrow on your command-line and hit enter. Despite all the debugging time and headaches type safety saves, adding a compiling step still slows iterations.  (This slows me personally down even more because I always try to jam some other activity, like writing this blog entry, into my compile times.)

One thing I don’t miss about Ruby is the console, but only because Scala has one. For you Java/PHP developers, a console is nice to have because it lets you quickly test out little snippets of code, both to verify some syntax you forgot and to test your objects.

Using the maven-scala-plugin, the free JRebel plug-in, and the scala console, I’ve been able to get pretty close to script like iteration speed.

  1. mvn scala:cc – The scala continuous compilation command is part of the maven-scala-plugin.  It saves the maven startup, compiler startup, and human command-line time.  When you save a source file, the process detects the change, and nearly instantly starts compiling.

    Unfortunately, scala:cc exits when there is a compilation error, which is annoying in practice.  I’d prefer it either waited until a file changes to start compile again, or at waited for a keypress.   To work around this, I use the following command:

    • (while true; do mvn scala:cc; sleep 10; done)

    That will keep your CPU a bit overly busy, but will save you some thought.

  2. Use JRebel.  The JRebel plugin-in, a costly but probably well-worth-it library for Java developers, is available for free for Scala developers in what I assume is a random act of kindness.  JRebel detects changes to class files and reloads them without restarting the runtime, and it generally just works.
  3. Use the Scala console to run your code. The console is easily launched using maven-scala-plugin with the command: mvn scala:console

Now every time you save a .scala file, it will be compiled.  Wait the few seconds for compilation, then run your test command in the Scala console, for instance a command-line simulation: liivid.MyClass.main(Array(“–test”)). JRebel will reload any changed .class files. The console has a convenient command line history (try the up arrow), which it preserves even after restarts.

I still can’t iterate as fast as I can with Ruby or PHP, but this is huge improvement.

As a side-note, I am playing around with the Play framework, a Java and soon-to-be Scala framework which uses some clever magic to allow interpreted language behavior in a Java/Scala web framework. I’ve been playing with it in Java, and really like it. Just change your source file and reload the browser – no need to wait for compilation. Very convenient. The Play framework is also stateless, and contains a fully integrated stack with hibernate and a built-in, production ready (supposedly) webserver – no piecing together Spring, Maven, and whatever else configs. Check it out..

There are no responses so far

Leave a Reply