用于添加设置的惯用方法

Koe*_*ers 7 scala sbt

我在开源sbt项目中看到了很多:

lazy val project = Project(
  id = "root",
  base = file("."),
  settings = Project.defaultSettings ++ Seq(
    ...
  )
)
Run Code Online (Sandbox Code Playgroud)

我们也为我们的内部项目采用了这个公约.但是今天我尝试使用这样的项目sbt-ensime并运行"gen-ensime"给了我一个错误:

[error] (*:update) java.lang.IllegalArgumentException: Cannot add dependency 'org.scala-lang#scala-compiler;2.11.7' to configuration 'ensime-internal' of module ... because this configuration doesn't exist!
Run Code Online (Sandbox Code Playgroud)

建议的修复程序在这里:https://github.com/ensime/ensime-sbt/issues/145

它建议我将我的项目更改为:

lazy val project = Project(
  id = "root",
  base = file(".")
).settings(Seq(
  ...
)
Run Code Online (Sandbox Code Playgroud)

我的问题是:这是建议的方式来定义项目惯用语和首选sbt?使用此功能是否会丢失任何内容(特别是,仍然将defaultSettings添加到我的项目中)?

str*_*obe 3

这两者之间似乎有区别

.settings(Seq(...))
Run Code Online (Sandbox Code Playgroud)

是将您的序列附加到项目中的“设置”,但是

Project(settings = ...)
Run Code Online (Sandbox Code Playgroud)

仅写入设置而不保存旧值。

因此看起来 .settings() 是更安全的方法。

一般来说 .settings() 现在更惯用了,因为一些 sbt 插件可能会尝试在项目构建时修改设置。

sbt 来源的一些片段:

/**
 * The explicitly defined sequence of settings that configure this project.
 * These do not include the automatically appended settings as configured by `auto`.
 */
def settings: Seq[Setting[_]]

/** Appends settings to the current settings sequence for this project. */
def settings(ss: Def.SettingsDefinition*): Project = copy(settings = (settings: Seq[Def.Setting[_]]) ++ Def.settings(ss: _*))

// TODO: Modify default settings to be the core settings, and automatically add the IvyModule + JvmPlugins.
def apply(id: String, base: File, aggregate: => Seq[ProjectReference] = Nil, dependencies: => Seq[ClasspathDep[ProjectReference]] = Nil,
    delegates: => Seq[ProjectReference] = Nil, settings: => Seq[Def.Setting[_]] = Nil, configurations: Seq[Configuration] = Nil,
    auto: AddSettings = AddSettings.allDefaults): Project =
    unresolved(id, base, aggregate, dependencies, delegates, settings, configurations, auto, Plugins.empty, Nil) // Note: JvmModule/IvyModule auto included...

def copy(id: String = id, base: File = base, aggregate: => Seq[ProjectReference] = aggregate, dependencies: => Seq[ClasspathDep[ProjectReference]] = dependencies,
    delegates: => Seq[ProjectReference] = delegates, settings: => Seq[Setting[_]] = settings, configurations: Seq[Configuration] = configurations,
    auto: AddSettings = auto): Project =
    unresolved(id, base, aggregate = aggregate, dependencies = dependencies, delegates = delegates, settings, configurations, auto, plugins, autoPlugins)
Run Code Online (Sandbox Code Playgroud)