1

How do I clean dependent projects in SBT from inside the code of a task?

I've checked before this related questions:

but I'm getting a little lost with strange syntax.

I've tried this:

projectDependencies.value.foreach { p =>
  System.out.println(s"Cleaning ${p.name}")
  (clean.all(ScopeFilter(inProjects(new LocalProject(p.name))))).value
}

but SBT complains about dynamic scope:

Illegal dynamic reference: p

Community
  • 1
  • 1
david.perez
  • 6,090
  • 4
  • 34
  • 57
  • What are you trying to achieve? Why don't you rely on sbt to know when to refresh the other dependent projects? What's the use case? – Jacek Laskowski Oct 08 '14 at 19:43
  • I'd like to automate creating a release artifact, and would like to clean also dependent projects, in order to guarantee everything is ok. In Android contexts, I've had problems sometimes when not fully clean (ProGuard caches not updated, ...) – david.perez Oct 09 '14 at 07:53
  • Why don't you do `clean` in the to-be-released project followed by the command you use to do the release? – Jacek Laskowski Oct 09 '14 at 08:06
  • I have a composite task that does clean and many other steps. For me, the point is if the `clean` command does clean also the dependency projects? – david.perez Oct 09 '14 at 09:07
  • Ah, right. It does not, only `aggregate`. Sorry for confusion. – Jacek Laskowski Oct 09 '14 at 14:00

2 Answers2

2

Use the following in build.sbt:

val selectDeps = ScopeFilter(inDependencies(ThisProject))

clean in Compile := clean.all(selectDeps).value
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
0

Based on the solution offered by Jacek Laskowski (thanks), here is a more complete snippet:

val cleanDependencies = taskKey[Seq[Unit]]("Clean dependencies of current project")

lazy val MyProject = project.settings(Seq(
   cleanDependencies <<= clean.all(ScopeFilter(inDependencies(ThisProject))),
   package <<= package.dependsOn(clean, cleanDependencies)
): _*)
david.perez
  • 6,090
  • 4
  • 34
  • 57