build.sbt - 迭代 monorepo 中通用设置的子项目

build.sbt - iteration over sub projects for common settings in monorepo

我正在使用 SBT 实现一个 monorepo。我想遍历我的子项目以初始化它们(因为它们具有相同的配置)并防止代码重复。

在我的 build.sbt:

lazy val root = (project in file("."))
    .aggregate(projects: _*)
    .settings(
        crossScalaVersions := Nil,
        publish / skip := true
    )

lazy val projects = Seq("projectA", "projectB", "projectC")
    .map((projectName: String) => (project in file(projectName))
        .settings(
            name := projectName,
            commonSettings,
            libraryDependencies ++= ModulesDependencies.get(projectName))
        .project
    )

我遇到错误:

 error: project must be directly assigned to a val, such as `val x = project`. Alternatively, you can use `sbt.Project.apply`

根据错误消息,我也尝试使用 sbt.Project.apply(projectName, file(projectName)).settings(...),但我也遇到了一些有趣的错误。

据我了解,SBT 似乎希望我声明为 lazy val projectA = (project in file("projectA")).settings(...),这工作正常,但我必须为我的所有子项目复制此代码。

我尝试实现的这个迭代是否可行?

实用方法可能有助于某些重复,例如

def createProject(projectName: String) = {
  Project(projectName, file(projectName))
    .settings(
      name := projectName,
      commonSettings,
      libraryDependencies ++= ModulesDependencies.get(projectName)
    )
}

lazy val projectA = createProject("projectA")
lazy val projectB = createProject("projectB")
lazy val projectC = createProject("projectC")

lazy val root = (project in file("."))
  .aggregate(projectA, projectB, projectB)
  .settings(
    crossScalaVersions := Nil,
    publish / skip := true
  )