sbt: dynamic aggregation of subproject

前端 未结 2 613
耶瑟儿~
耶瑟儿~ 2021-01-12 04:07

I would like to invent a system to dynamically discover subprojects and aggregate them into my project automatically. Or at least configure this somehow.

相关标签:
2条回答
  • 2021-01-12 04:45

    Here:

    project/Build.scala

    import sbt._
    import sbt.Keys._
    
    import play.sbt._
    import play.sbt.Play.autoImport._
    
    object Build extends Build {
      val commonSettings: Seq[Setting[_]] = Seq(
        scalaVersion := "2.11.7"
      )
    
      lazy val modules = (file("modules") * DirectoryFilter).get.map { dir =>
        Project(dir.getName, dir).enablePlugins(PlayJava).settings(commonSettings: _*)
      }
    
      lazy val root = (project in file("."))
        .enablePlugins(PlayJava)
        .settings(
          name := "mysite",
          version := "1.0"
        )
        .settings(commonSettings: _*)
        .dependsOn(modules map (m => m: ClasspathDependency): _*)
        .aggregate(modules map (m => m: ProjectReference): _*)
    
      override lazy val projects = root +: modules
    }
    

    Note, make sure that the module directories don't also contain build.sbt files defining them as projects as that will cause confusing RuntimeException: No project 'x' in 'file:/x' type exception, see Can't use sbt 0.13.7 with Play subprojects

    0 讨论(0)
  • 2021-01-12 04:57

    You can try something like this:

    import sbt._
    import sbt.Keys._
    
    import play.sbt._
    import play.sbt.Play.autoImport._
    
    object Build extends Build {
    
      lazy val modules = (file("modules") * DirectoryFilter).get.map { dir =>
        Project(dir.getName, dir).enablePlugins(PlayJava)
      }
    
      lazy val root = (project in file("."))
        .enablePlugins(PlayJava)
        .settings(
          name := "mysite",
          version := "1.0"
        )
        .dependsOn(modules map (m => m: ClasspathDependency): _*)
        .aggregate(modules map (m => m: ProjectReference): _*)
    
      override lazy val projects = root +: modules
    }
    

    Note: Inspired by Dale's answer, but I had to remove the "commonSettings", otherwise it would not work for me.

    0 讨论(0)
提交回复
热议问题