sbt: dynamic aggregation of subproject

给你一囗甜甜゛ 提交于 2019-12-19 05:54:20

问题


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

To do this, I'm planning to have either a "modules" folder or an optional configuration file containing paths to the modules.

In any case I'd need to loop through subfolders (or loop through a list of paths from a configuration file), and aggregate each subproject. I don't know how to do that.

Currently I'm building in the Play framework with the build.sbt file. I would need to add the loop like this:

name := "mysite"
version := "1.0"
scalaVersion := "2.11.1"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
//pseudocode:
foreach( folder in the 'modules' folder) { 
  lazy val module = (project in file(folder)).enablePlugins(PlayJava)
  root = root.dependsOn(module).aggregate(module)
}

Is there a way to do this?

EDIT 3: The code here is almost working:

object MyBuild extends Build {
  name := "mysite"
  version := "1.0"
  scalaVersion := "2.11.6"

  var m = new File("modules")
  var list = Seq[ProjectReference]()
  var deps = Seq[ClasspathDependency]()
  if (m.exists) {
    val subs = m.listFiles.filter ( _.isDirectory ).foreach { folder =>
      var modulePath = new File("modules", folder.getName)
      println("Found module " + modulePath)
      lazy val module:ProjectRef = ProjectRef(modulePath,folder.getName)
      lazy val dep:ClasspathDependency = ClasspathDependency(module, None)
      list = list :+ module
      deps = deps :+ dep
    }
  }

  lazy val root = Project(id = "mysite", base = file(".")).enablePlugins(PlayJava).aggregate(list:_*).dependsOn(deps:_*)
}

Edit 4:

See Dale Wijnand's solution below.

About the error: RuntimeException: No project 'myModule' in 'file:/Users/me/mysite/modules/myModule'. I fixed this using the solution from https://stackoverflow.com/a/28820578


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/30942220/sbt-dynamic-aggregation-of-subproject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!