title: How do you compose tasks in sbt?
body: <p>I'm trying to build a custom task for building our project in our continuous integration environment. It is a set of steps along the lines of</p>
  
  <ol>
  <li>Send build started message to a chat room</li>
  <li>Compile templates</li>
  <li>Run npm tests</li>
  <li>Run jshint</li>
  <li>Compile </li>
  <li>Build application artifact</li>
  <li>Upload artifact to our deployment server</li>
  <li>Run tests</li>
  <li>Send test results to our deployment server</li>
  <li>Send build results message to chat room</li>
  </ol>
  
  <p>Note that step 10 should be done if any steps fail, and the message should be customized depending on which step failed, e.g. if step 5 fails it should say "Compilation failed", if step 8 fails it should say how many tests were run and how many failed.</p>
  
  <p>To make things extra interesting this is a multi project build, so when running tests and publishing results it should run all the tests and publish aggregated results.</p>
  
  <p>To make things even more interesting, the npm tests, jshint and artifact only really make sense in the <code>webapp</code> subproject, where the Javascript lives and the web server resides.</p>
  
  <p>I've been looking at sbt-release for inspiration, but I'm stymied on how to take the value produced by one task and use it in the next one, how to run tasks in aggregate and get the produced values (I see a method in <code>Extracted</code> to run aggregated tasks, but it doesn't give produced values), how to run tasks in a subproject and get the produced value, and how to do the error handling.</p>
  
  <p>So far I've tried two approaches</p>
  
  <pre><code>npmTest.result.value match {                                                                    
    case Inc(inc) =&gt;                                                                              
      println(inc)                                                                                
    case Value(res) =&gt; Def.taskDyn {                                                              
      (executeTests in Test).result.value match {                                                 
        case Inc(inc) =&gt;                                                                          
          println(inc)                                                                            
        case Value(res) =&gt;                                                                        
          println(res)                                                                            
      }                                                                                           
    }
  </code></pre>
  
  <p>The problem with the above is that <code>executeTests</code> is always run, even if <code>npmTest</code> fails. And none of the <code>println</code>s are executed.</p>
  
  <pre><code>npmTest.result.                                                                                    
   flatMap {-                                                                                       
     case Inc(inc) =&gt;                                                                               
       task { println(inc) }                                                                        
     case Value(res) =&gt;-                                                                            
       (executeTests in Test).result.                                                               
         flatMap {                                                                                  
           case Inc(inc) =&gt;                                                                         
             task { println(inc) }                                                                  
           case Value(res) =&gt;                                                                       
             task { println(res) }                                                                  
         }                                                                                          
   }                
  </code></pre>
  
  <p>This one doesn't compile because <code>(executeTasks in Test)...</code> produces an <code>Initialize[Task[Unit]]</code> value and a <code>Task[Unit]</code> is required.</p>
  
  <p>Is there a way to accomplish this with sbt?</p>