SBT:覆盖多重构建中的设置dependsOn/aggregate 项目 [英] SBT: Override setting in multi-build dependsOn/aggregate project
问题描述
如何覆盖多构建 SBT 项目中的子项目设置/任务?例如,这里有两个非常简单的 SBT 项目:
How do I override a sub-project setting/task in a multi-build SBT project? For example, here are two very simple SBT projects:
~/projects/backend/build.sbt
name := "backend"
// old version of scala
scalaVersion := "2.9.1"
~/mycode/docker_builder/build.sbt
lazy val backend = RootProject(file("~/projects/backend"))
lazy val root = (project in file(".")).
settings(
// Doesn't work because sub-project already defines name
name in backend := "sub-overriden",
// Doesn't override {backend/}backend/*:scalaVersion since backend already defines scalaVersion in Global config
scalaVersion in backend := "2.10.1",
// Does define new setting in sub-project: {backend/}backend/test:scalaVersion (because backend did not define scalaVersion in test config)
scalaVersion in (backend, Test) := "2.10.2"
).
aggregate(sub1)
// dependsOn(sub1)
在上面的示例中,我试图覆盖 name 和 scalaVersion,但如评论中所述,根项目无法覆盖任何设置/task 在后端项目中明确定义.现在我假设这是预期的行为,因为 RootProject 和它的父 ProjectReference 指向一个完全不同的 SBT 构建,但如果是这种情况,为什么我们可以在构建中引入新设置,例如:scalaVersion in (backend, Test)?
In the sample above, I'm trying to override both name and scalaVersion, but as noted in the comments, the root project is not able to override any setting/task explicitly defined explicitly in the backend project. Now I'm assuming this is expected behavior, since RootProject and it's parent ProjectReference point to a completely different SBT build, but if this is the case, why are we allowed to introduce new settings into the build, e.g: scalaVersion in (backend, Test)?
任何解决方法或其他解决方案?
Any workarounds or other solutions?
上面的示例后端项目过于简单化——我们团队中的实际后端项目基于多项目格式,大约有十两个子项目和 sbt-plugins——但是,幸运的是我可以通过上面的列表重现该问题.
相关:
如何在多.sbt 项目构建?
为 SBT 依赖项设置 javac 选项
使用 SBT `RootProject` 中的常用设置
推荐答案
似乎没有任何方法可以直接从主构建覆盖子项目的设置.当您创建 Project
时,它的 settings
甚至不包含来自子项目的 build.sbt
的定义.
There doesn't seem to be any way to override settings from the subproject directly from the main build. When you create the Project
its settings
doesn't even contain the definitions from the subproject's build.sbt
yet.
你可以做的是调用 .addSbtFiles(file("overrides.sbt"))
(路径是相对于子项目的基本目录解析的),然后将覆盖放入这个单独的文件中在子项目的 build.sbt
之后加载.
What you can do though is call .addSbtFiles(file("overrides.sbt"))
(paths are resolved relative to the subproject's base dir) and then put the overrides into this separate file which is loaded after the subproject's build.sbt
.
这篇关于SBT:覆盖多重构建中的设置dependsOn/aggregate 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!