gpt4 book ai didi

scala - sbt 添加设置的惯用方式

转载 作者:行者123 更新时间:2023-12-04 12:18:55 25 4
gpt4 key购买 nike

我在开源 sbt 项目中经常看到这一点:

lazy val project = Project(
id = "root",
base = file("."),
settings = Project.defaultSettings ++ Seq(
...
)
)

我们也为我们的内部项目采用了这个惯例。但是今天我用这样的项目尝试了 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!

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

它建议我将我的项目更改为:
lazy val project = Project(
id = "root",
base = file(".")
).settings(Seq(
...
)

我的问题是:这种定义项目的建议方式是否符合 sbt 的习惯和偏好?使用它我会丢失任何东西吗(特别是,默认设置是否仍然添加到我的项目中)?

最佳答案

这两者之间的区别似乎是

.settings(Seq(...))

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

Project(settings = ...)

只写设置而不保存旧值。

因此看起来 .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)

关于scala - sbt 添加设置的惯用方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com