2

How can I make sbt run a custom task whenever the code of Runtime configuration is compiled?

Given this task:

val myCustomTask = taskKey[Unit]("Prints a greeting")

myCustomTask := {
  println("Hello from myCustomTask!")
}

What I have tried so far:

compile := {(compile in Compile) dependsOn myCustomTask}.value

That does what I want when sbt compile is run.

However, my custom task is not run when I run sbt test, even though sbt test makes sbt also compile the runtime sources if needed, not only test sources:

sbt clean test
...
[info] Compiling 19 Scala sources to /myproject/target/scala-2.11/classes...
[info] Compiling 7 Scala sources to /myproject/target/scala-2.11/test-classes...
..

I read that test:compile implies a compile, but how? It doesn't seem to depend on compile itself.

juhoautio
  • 1,541
  • 1
  • 22
  • 23

1 Answers1

2

I found that triggeredBy does what I wanted (SBT run code in project after compile).

With that I get:

sbt clean test
...
[info] Compiling 19 Scala sources to /myproject/target/scala-2.11/classes...
Hello from myCustomTask!
[info] Compiling 7 Scala sources to /myproject/target/scala-2.11/test-classes...
..
juhoautio
  • 1,541
  • 1
  • 22
  • 23