With SBT how do I sequence tasks from plugin before running a test

297 views Asked by At

How can I run sequential tasks that come from a plugin such as flyway before running a test, for example if I wanted to run: flywayClean then flywayMigrate then integrationTests, how would I do that?

The problem is that sbt doesn't really support anything with side-effects like a db.

I looked at examples of sequencing tasks here: http://eed3si9n.com/sequencing-tasks-with-sbt-sequential

I've tried working through these, and I can follow the basic example, but I can't figure out how to run a task from a plugin such as flyway.

It seems like part of the problem is that flywayClean and flywayMigrate are both of type TaskKey[Unit], where the sbt-sequential examples are using Def.Setting[Task[Unit]].

sbt has so much complexity that I can't quite figure out how to resolve the Task associated with a given TaskKey and then transform that into a Setting[Task[Unit]].

2

There are 2 answers

0
marios On

Here is how you can have a task that executes a lot of other tasks sequentially. Is this what you want to do? If the tasks are from a plugin you need to bring it into scope. If it's from an auto-plugin then pretty much all its task should be in scope without you explicitly importing them.

lazy val mtaskA = taskKey[Unit]("a")

lazy val mtaskB = taskKey[String]("b")

lazy val mtaskC = taskKey[Int]("c")

mtaskA := {
    println("hello")
}

mtaskB := {
    "hello-2"
}

mtaskC := {
    2
}


lazy val cmp = taskKey[Unit]("cmp")

cmp := {
    mtaskA.value
    Thread.sleep(1000)
    println( mtaskB.value )
    Thread.sleep(1000)
    println( mtaskC.value )
}
2
James Ward On

I setup my sbt build to do a flywayClean, then a flywayMigrate before running the tests:

executeTests in Test := {
  (executeTests in Test).dependsOn(flywayMigrate.dependsOn(flywayClean)).value
}