CoffeeScript is a seriously awesome client-side scripting language that fixes the many warts of Javascript.

See scalatra-coffeescript for a minimal and standalone project containing the example in this guide.

Full usage guide

Because CoffeeScript is an external project, we won’t attempt to reproduce its documentation here. Go to the official documentation; you’ll be glad you did!

Set up CoffeeScript in Scalatra


Install the plugin by adding the dependency to your project/plugins.sbt:

resolvers += Resolver.url("sbt-plugin-snapshots",
  new URL(""))(

addSbtPlugin("com.bowlingx" %% "xsbt-wro4j-plugin" % "0.1.0-SNAPSHOT")


Add these imports to the very top of your build.sbt:

import com.bowlingx.sbt.plugins.Wro4jPlugin._
import Wro4jKeys._

Now enable to plugin by adding these lines to your build.sbt, after all your imports. Don’t attempt to remove the blank line!

seq(wro4jSettings: _*)

(webappResources in Compile) <+= (targetFolder in generateResources in Compile)


Unfortunately, the plugin we’re using does require a bit of configuration.

Create the file src/main/webapp/WEB-INF/wro.xml, telling the compiler where our CoffeeScript files are:

<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation=" wro.xsd"

  <group name="scripts">

Also create the file src/main/webapp/WEB-INF/, requesting CoffeeScript compilation:

preProcessors = coffeeScript
postProcessors =


With installation out of the way, you’re free to put your CoffeeScript files in src/main/webapp/coffee. It doesn’t matter what the file/s are called, so long as they have the .coffee extension we said we’d use in the wro.xml configuration.

For example, create src/main/webapp/less/, which shows off the optionality of parentheses and semicolons in CoffeeScript:

alert "Hello CoffeeScript!"

That file will be compiled to one called compiled/scripts.js, so you can refer to it in your app like this:

class CoffeeScriptApp extends ScalatraServlet {

  get("/") {
        <h1>This is
          <a href="">resources/coffeescript</a>!

        <script type="text/javascript" src="compiled/scripts.js"></script>

See it in action

Now compile and run your app as usual. If all has gone well, and you’ve used the above example, an annoying popup should launch on page load.

$ cd scalatra-coffeescript
$ ./sbt
> jetty:start
> browse

If browse doesn’t launch your browser, manually open http://localhost:8080/ in your browser.

Further documentation

The resources processor we’ve been using, wro4j, is extensively configurable. See the wro4j documentation for more information.