title: DRY Gradle Tasks
body: <p>Is there any neat, straightforward way to abstract away elements of a task in gradle?</p>
  
  <p>I'm creating a mixed project, which includes some Scala to be compiled to Javascript - which doesn't appear to be possible with Gradle alone. However I end up with several tasks that look like this:</p>
  
  <pre><code>task buildScalaJS(type: Exec) {
      workingDir './scalajs'
      commandLine '../sbt', 'fastOptJS'
  }
  
  task buildScalaJSLauncher(type: Exec, dependsOn: buildScalaJS) {
      workingDir './scalajs'
      commandLine '../sbt', 'scalaJSLauncher'
  }
  </code></pre>
  
  <p>My first thought was to abstract this into a function like:</p>
  
  <pre><code>def sbt(String... params) {
      workingDir './scalajs'
      commandLine('../sbt', params)
  }
  </code></pre>
  
  <p>However, these properties don't exist outside the scope of the task definition, so this (understandably) results in an error.</p>
  
  <hr>
  
  <p>The only other way that I can conceive is to have a function that generates tasks:</p>
  
  <pre><code>def sbtTask(String taskName, String sbtTask = taskName, Map options) {
      Map defaultOptions = [type: Exec]
      task ("${taskName}", (defaultOptions &lt;&lt; options)) {
          workingDir './scalajs'
          commandLine '../sbt', sbtTask
      }
  }
  
  sbtTask('buildScalaJS', 'fastOptJS')
  sbtTask('buildScalaJSLauncher', 'scalaJSLauncher', dependsOn: 'buildScalaJS')
  </code></pre>
  
  <p>However, gradle complains that the method 'sbtTask()' couldn't be found.</p>
  
  <hr>
  
  <p>Can anyone suggest a way to do this?</p>