SBT creates subprojects using a collection

I searched if this is possible for a while with little success.

Using SBT, can you create a subproject programmatically without pointing each project to its own val?

My current project structure looks something like this:

root/
    common/ <--- This is another sub-project that others dependOn
    project/
        build.scala
    src/main/scala
    apps/ <--- sub-projects live here
        Sub1/
        Sub2/

Sub1and Sub2are their own SBT projects.

My first attempt to link these projects together looked like this:

// root/project/build.scala
import sbt._
import Keys._
object build extends Build {
  lazy val common = project /* Pseudo-code */
  val names = List("Sub1", "Sub2")
  lazy val deps = names map { name =>
    Project(id = name, base = file(s"apps/$name")).dependsOn(common)
  }

  lazy val finalDeps = common :: deps
  lazy val root = project.in(file(".")).aggregate(finalDeps.map(sbt.Project.projectToRef) :_*)
                 .dependsOn(finalDeps.map(ClassPathDependency(_, None)) :_*)
}

However, since SBT uses reflection to create projects and subprojects, this does not work.

It only works if each subproject is explicitly specified:

lazy val Sub1 = project.in(file("apps/Sub1"))

So the question is:

Is there a way to programmatically build dependencies between projects in SBT?

+4
source share
1

Sbt :

http://www.scala-sbt.org/release/docs/Getting-Started/Full-Def.html

project/project/build.scala, -, :

// project/project/build.scala
sourceGenerators in Compile <+= sourceManaged in Compile map { out =>
    Generator.generate(out / "generated")
}

EDIT: Generator.

, , , .

// project/subprojects.scala
// This is autogenerated from the source generator
object Subprojects{
  lazy val Sub1 = project.in(file("apps/Sub1"))
  lazy val Sub2 = project.in(file("apps/Sub2"))
  lazy val all = Seq(Sub1,Sub2)
}

build.scala :

// project/build.scala
lazy val root = project.in(file("."))
  .aggregate(Subprojects.all.map(sbt.Project.projectToRef) :_*)
  .dependsOn(Subprojects.all.map(ClassPathDependency(_, None)) :_*)

, , .

EDIT: Github, . , .

https://github.com/darkocerdic/sbt-auto-subprojects

+2

Source: https://habr.com/ru/post/1537113/


All Articles