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".
-
Possible duplicate for SBT 0.13 http://stackoverflow.com/q/8554992/1305344 – Jacek Laskowski Jan 01 '14 at 22:10
4 Answers
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

- 2,443
- 17
- 18
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
}

- 8,636
- 5
- 71
- 90
-
6There 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
-
-
2This answer is outdated. See [the updated version I just posted](https://stackoverflow.com/a/47654822/596329). – arussell84 Dec 05 '17 at 13:31
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)
}

- 62,329
- 13
- 183
- 228

- 5,437
- 6
- 35
- 43
-
4Please note that this suggestion only works for 'older' SBT 0.7.x. The current 0.11 does it differently. – Rick-777 Oct 06 '11 at 16:48
-
3
-
1This answer is outdated. See [the updated version I just posted](https://stackoverflow.com/a/47654822/596329). – arussell84 Dec 05 '17 at 13:31
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".

- 26,737
- 24
- 105
- 146

- 19
- 3