Getting CoffeeScript compilation working in Gradle

Gradle 1.2 includes support for compiling CoffeeScript but it’s not well documented, there’s nothing on the gradle website and all I was able to find after a bunch of googling was a gradle-dev thread where Luke Daley announces the functionality.

Based on that thread, I was able to come up with this sample gradle file that let me compile my .coffee source files into javascript as part of a build:

import org.gradle.plugins.javascript.coffeescript.CoffeeScriptCompile

apply plugin: 'coffeescript-base'

repositories {
  mavenCentral()
  maven {
    url: "http://repo.gradle.org/gradle/repo"
  }
}

task compileCoffee(type: CoffeeScriptCompile) {
  source fileTree('src/main/coffee')
  destinationDir file('build/js')
}

To integrate this into a war file, you’d need to extend it a little further to make the war task depend on the compileCoffee task, and then tell it to include the output in build/js in the root of the war.

I wasn’t able to use this approach on my current project for a couple of reasons related to restricted maven repos, and ended up using require-cs plugin for require.js AMD module framework as I was already using it.

One Response to “Getting CoffeeScript compilation working in Gradle”

  1. techie.Brandon

    Awesome, I had to change the maven repo to that which Luke referenced (hardcoded url is probably not a good idea for future maintainers) and worked like a charm!

    repositories {
    mavenCentral()
    maven {
    url ‘http://repo.gradle.org/gradle/javascript-public/’
    }
    }

Leave a Reply