在 SBT `RootProject` 中使用通用设置 [英] Use common settings in SBT `RootProject`

查看:20
本文介绍了在 SBT `RootProject` 中使用通用设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://stackoverflow.com/a/21605652/1737727 上的答案显示了如何覆盖使用主项目中的 RootProject 定义的子项目.我想知道是否有一种很好的方法可以针对多个设置执行此操作,然后也可能针对多个子项目执行此操作,因此您不必单独列出每个组合.这是为了防止扩散并减少忘记组合和意外设置不匹配的机会.

The answer at https://stackoverflow.com/a/21605652/1737727 shows how to override one setting for a subproject defined with RootProject from the main project. I was wondering if there is a nice way to do this for multiple settings, and then possibly for multiple subprojects, too, so you don't have to list every combination separately. This would be to prevent proliferation and reduce the chance of forgetting a combination and accidentally having a mismatch of settings.

不使用 RootProject 时,SBT 文档 展示了如何使用通用设置序列执行此操作:

When not using RootProject, the SBT docs show how to do this with a common sequence of settings:

lazy val commonSettings = Seq(
  organization := "com.example",
  version := "0.1.0",
  scalaVersion := "2.11.8"
)

lazy val core = (project in file("core")).
  settings(commonSettings: _*).
  settings(
    // other settings
  )

lazy val util = (project in file("util")).
  settings(commonSettings: _*).
  settings(
    // other settings
  )

但是 RootProject 无法设置其设置.根据上面提到的答案,我尝试了以下操作:

But a RootProject has no method to set its settings. I tried something like the following, according to the answer mentioned above:

lazy val util = RootProject(file("../util"))
commonSettings.map(_.key).foreach(key => key in util := key.value)

但这似乎不是正确的方法.

but this doesn't seem to be the right approach.

我已经考虑使用 GlobalThisBuild 范围,但每个子项目都在自己的 build.sbt 文件中设置设置,这需要如果我理解正确,则优先于这些更广泛的范围.

I have looked into using Global or ThisBuild scope, but each subproject sets the settings in its own build.sbt file, which takes precedence over these wider scopes if I understand correctly.

有什么好的方法可以做到这一点,还是应该手动为每个子项目设置每个设置?我应该使用不同的范围,例如子项目在 Global 中定义它们的设置,在 ThisBuild?

Is there a nice way to do this, or should I just set each setting for each subproject manually? Should I use different scopes, e.g. the subprojects define their settings in Global and the main project in ThisBuild?

推荐答案

可以这样做:

commonSettings.map { s => s.mapKey(Def.mapScope(_.in(util))) }

这会创建新的 Seq[Setting[_]],其中每个设置的范围都更改为在 util 项目中.这是 sbt 文件中的有效条目.

This creates new Seq[Setting[_]] where each of the setting's scopes is changed to be in the util project. This is a valid entry in an sbt file.

另一种选择是定义一个 sbt 插件,您可以将其添加到主项目和 RootProject 自己的 build.sbt

Another option is defining an sbt plugin that you add to both your main projects and in RootProject's own build.sbt

但您可能需要考虑是否确实需要将 util 项目作为 RootProject 而不是常规子项目导入.

But you may want to consider if you actually need to import the util project as a RootProject instead of a regular subproject.

这篇关于在 SBT `RootProject` 中使用通用设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆