Dynamically create subprojects in scala sbt

Viewed 102

I am trying to register Projects dynamically in sbt like this:

lazy val dirs = new File("path").listFiles().filter(_.isDirectory)

val subprojects = dirs.map { p =>
  Project(p.getName, p).settings(name := p.getName)
}

lazy val root = (project in file("."))
  .settings(
    name := "gen",
    libraryDependencies ++= dependencies
  )

val root2 = subprojects.foldLeft(root)((x, y) => x.aggregate(y))

But when executing, I am getting:

No project 'a-sub-project' in 'file:/path'.

Seems like projects are not being registered when creating them as Project on this line:

val subprojects = dirs.map { p =>
  Project(p.getName, p).settings(name := p.getName)
}

I also tried like this:

lazy val root = (project in file("."))
  .aggregate(subprojects:_*)
  .settings(
    name := "gen",
    libraryDependencies ++= dependencies
  )

I get:

no `: _*' annotation allowed here (such annotations are only allowed in arguments to -parameters) .aggregate(subprojects:_)

If I run projects on sbt none of the sub projects are listed.

1 Answers

If you replace your subprojects declaration with the one below it should work:

val subprojects = new CompositeProject {
  override def componentProjects: Seq[Project] = dirs.map { p =>
    Project(p.getName, p).settings(name := p.getName)
  }
}

as it should register all the sub-projects in the CompositeProject and allow you to aggregate them

Related