33

Is it possible to override or modify built-in SBT tasks (like compile) to depend on custom tasks in my own Build.scala? Overriding e.g. "compile" directly is not possible since it has been defined with lazy val and thus referring to super.compile emits a compiler error "super may be not be used on lazy value".

Eemeli Kantola
  • 5,437
  • 6
  • 35
  • 43

4 Answers4

33

Since this question appears when Googling how to add a dependency in SBT, and the current answers are deprecated as of 0.13.x and removed in 1.0, here's the updated answer, assuming that printAction is the task that compile should depend on:

(Compile / compile) := ((Compile / compile) dependsOn printAction).value

arussell84
  • 2,443
  • 17
  • 18
17

Update: See arussell84's answer for a modern way to do this

You should be able to do it like this:

in a .sbt file:

compile <<= (compile in Compile) dependsOn jruby

Where jruby is a task key that you've defined in a project/something.scala file:

val jruby = TaskKey[Unit]("jruby", "run a jruby file")

Also, this isn't part of your question but you can just call regular Scala code:

compile <<= (compile in Compile) map { result =>
  println("in compile, something")
  result
}
James Moore
  • 8,636
  • 5
  • 71
  • 90
  • 6
    There are so many 'compile's in there I can't work out how this generalizes to adding dependencies to other tasks. What to I do to add a dependency to 'stage'? – Scott Morrison Aug 23 '12 at 03:16
  • @ScottMorrison I think you say: `stage << stage dependsOn jruby` – 0fnt Feb 15 '15 at 10:57
  • 2
    This answer is outdated. See [the updated version I just posted](https://stackoverflow.com/a/47654822/596329). – arussell84 Dec 05 '17 at 13:31
7

Reply to self: http://code.google.com/p/simple-build-tool/wiki/ProjectDefinitionExamples#Insert_Task_Dependency tells the answer:

If you are using older 0.7.x SBT versions you can do this:

import sbt._
class SampleProject(info: ProjectInfo) extends DefaultProject(info) {
  lazy val printAction = task { print("Testing...") }
  override def compileAction = super.compileAction dependsOn(printAction)
}
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Eemeli Kantola
  • 5,437
  • 6
  • 35
  • 43
-1

In the base_dir/project/ folder create a file build.sbt and put libraryDependencies += ... there.

That's the idiomatic SBT way to build your "build project", also known as "Meta Build".

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Kevin Cao
  • 19
  • 3