1

I have multiple projects in one directory, which are aggregated in sbt. I would like to write a task for sbt in build.sbt, which would make some commands in each project.

lazy val P1 = (project in file("P1")
lazy val P2 = (project in file("P2")
lazy val cleanEverywhere = taskKey[Unit]("Clean everywhere")

How am I supposed to write this cleanEverywhere task to clean each project?

Hubert Bratek
  • 894
  • 10
  • 30

2 Answers2

3

Another way is to restructure your build.sbt as following


    lazy val root = (project in file(".")).aggregate(p1, p2)

    lazy val p1 = project.in(file("p1"))
    lazy val p2 = project.in(file("p2"))

this way, whenever you run sbt clean, sbt test, sbt compile commands, every command will get executed against all the aggregated projects and you don't need to create cleanAll task

And if you want project specific command, you can run it like sbt p1/compile

Pritam Kadam
  • 2,388
  • 8
  • 16
2

Try

val cleanAll = taskKey[Unit]("Clean all projects")
cleanAll := clean.all(ScopeFilter(inAnyProject)).value

where all

Evaluates the task in all scopes selected by the filter

and inAnyProject selects all scopes along project axis.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98