将 Seq[Setting[_]] 作为 Project.settings() 参数之一传递

passing Seq[Setting[_]] as one of the Project.settings() args

我在多项目build.sbt中初始化常用设置如下:

lazy val commonSettings = Seq( version := "0.1", ..)
..
lazy val libs = Seq( "org.some" % "somelib" % "1.2.3", ..)
..
lazy val myProject = Project(..).
  settings(commonSettings, version := "0.2", libraryDependencies ++= libs)

给定

的定义
sbt.Project.settings(ss: Setting[_]*)

我很困惑这实际上有效,因为我在这里混合了 Setting[_]Seq[Setting[_]] 参数。我错过了什么,扁平化发生在哪里?

自 sbt 0.13.8 起,定义更改为:

def settings(ss: SettingsDefinition*): Project =
  copy(settings = (settings: Seq[Setting[_]]) ++ ss.flatMap(_.settings))

这是扁平化发生的地方,SettingsDefinition 定义为:

sealed trait SettingsDefinition {
  def settings: Seq[Setting[_]]
}

sealed class Setting[T] private[Init] (
  val key: ScopedKey[T], val init: Initialize[T], val pos: SourcePosition
) extends SettingsDefinition {
  def settings = this :: Nil
  ...
}

final class SettingList(val settings: Seq[Setting[_]]) extends SettingsDefinition

object SettingsDefinition {
  ...
  implicit def wrapSettingsDefinition(ss: Seq[Setting[_]]): SettingsDefinition =
    new SettingList(ss)
}

注:

  • 每个单独的设置都是 SettingsDefinition
  • 任何 Seq[Setting[_]] 都会自动提升为 SettingsDefinition