Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new task dependencies to built-in SBT tasks?

Tags:

scala

sbt

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".

like image 822
Eemeli Kantola Avatar asked Sep 08 '11 07:09

Eemeli Kantola


People also ask

Which is the correct way to add dependencies in sbt file?

If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.

How do you add a dependency to a task?

Creating a dependency between two tasks To do this, hover over the task that you want to be completed first. You will see a grey dot to the right of the task. Click and drag this to the task you want to create the dependency with. You will see an arrow created, linking the two tasks.

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.


2 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

like image 180
arussell84 Avatar answered Sep 27 '22 22:09

arussell84


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 } 
like image 43
James Moore Avatar answered Sep 27 '22 23:09

James Moore