In software development there are no silver bullets, but I am always looking out for the next bronze one.

Wednesday, May 18, 2011

Enable a Scala compiler plugin using Gradle

Having recently switched a Scala project to Gradle for building, I was faced with the difficulty of figuring out how to enable the Scala continuation plugin during the compileScala task. Below is a general solution I came up with. Hopefully it will be helpful for others and save someone some time.

 

apply plugin: 'scala'
...
configurations {
  scalaCompilerPlugins { transitive = false }
}

dependencies {
...
    scalaCompilerPlugins 'org.scala-lang.plugins:continuations:2.9.0' 
}

def scalacXPlugins() {
     return ((configurations.scalaCompilerPlugins.files.collect { "\"-Xplugin:${it.path}\"" }))
}

compileScala.scalaCompileOptions.additionalParameters = scalacXPlugins() + ['-P:continuations:enable']

    Note:
  • The "transitive = false" in the configurations block prevents dependencies of the plugin as being treated as plugins themselves. Namely, the Scala compiler and library.