ThisBuild 和 Global 范围有什么区别? [英] What is the difference between ThisBuild and Global scopes?

查看:22
本文介绍了ThisBuild 和 Global 范围有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下写这两行的区别:

Can someone explain me the difference between writing these 2 lines:

resolvers in ThisBuild ++= appResolvers

resolvers in Global ++= appResolvers

推荐答案

阅读Scopes 以获得完整的解释.

Read Scopes for the full explanation.

我将引用相关部分:

共有三个范围轴:

  • 子项目轴
  • 依赖配置轴
  • 任务轴

如果您将多个项目放在一个构建中,则每个项目都需要自己的设置.也就是说,可以根据项目来确定键的范围.

If you put multiple projects in a single build, each project needs its own settings. That is, keys can be scoped according to the project.

项目轴也可以设置为ThisBuild,这意味着整个构建",因此设置适用于整个构建而不是单个项目.当项目没有定义项目特定的设置时,构建级设置通常用作后备.

The project axis can also be set to ThisBuild, which means the "entire build", so a setting applies to the entire build rather than a single project. Build-level settings are often used as a fallback when a project doesn’t define a project-specific setting.

每个作用域轴都可以用轴类型的实例填充(类似于Some(_)),也可以用特殊值Zero.所以我们可以把 Zero 想成 None.

Each scope axis can be filled in with an instance of the axis type (analogous to Some(_)), or the axis can be filled in with the special value Zero. So we can think of Zero as None.

Zero 是所有范围轴的通用后备,但在大多数情况下,它的直接使用应保留给 sbt 和插件作者.

Zero is a universal fallback for all scope axes, but its direct use should be reserved to sbt and plugin authors in most cases.

Global 是一个将 Zero 设置为所有轴的范围:Zero/Zero/Zero.换句话说,Global/someKeyZero/Zero/Zero/someKey 的简写.

Global is a scope that sets Zero to all axes: Zero / Zero / Zero. In other words, Global / someKey is a shorthand for Zero / Zero / Zero / someKey.

Global / concurrentRestrictions := Seq(
  Tags.limitAll(1)
)

(Global/concurrentRestrictions 隐式转换为 Zero/Zero/Zero/concurrentRestrictions,将所有轴设置为 Zero 范围组件;任务和默认配置已经是Zero了,所以这里的效果就是让项目Zero,也就是定义Zero/Zero/Zero/concurrentRestrictions而不是 ProjectRef(uri("file:/tmp/hello/"), "root")/Zero/Zero/concurrentRestrictions)

(Global / concurrentRestrictions implicitly converts to Zero / Zero / Zero / concurrentRestrictions, setting all axes to Zero scope component; the task and configuration are already Zero by default, so here the effect is to make the project Zero, that is, define Zero / Zero / Zero / concurrentRestrictions rather than ProjectRef(uri("file:/tmp/hello/"), "root") / Zero / Zero / concurrentRestrictions)

如上所述,Global 将所有三个轴设置为 ZeroThisBuild 仅将子项目轴设置为 ThisBuild.如果您将 ThisBuild 与其他轴(如配置)结合使用,这可能是有意义的:

So as written above, Global sets all three axes to Zero whereas ThisBuild sets only the subproject axis to ThisBuild. This might make sense if you combine ThisBuild with other axis like configuration:

> set ThisBuild / Test / name := "test-name"
[info] Defining ThisBuild / Test / name

2020 年 2 月更新:正如 Stefan K 在评论中指出的那样 范围委托规则是我在上述解释中没有包含的一个关键事实.

Update February 2020: As Stefan K noted in the comment scope delegation rule is a key fact that I didn't include in the above explanation.

规则 4:给定一个范围,通过按以下顺序替换子项目轴来搜索委托范围:给定的子项目,ThisBuild,然后是 Zero.

Rule 4: Given a scope, delegate scopes are searched by substituting the subproject axis in the following order: the given subproject, ThisBuild, and then Zero.

例如,如果发布配置引用projFoo/version,它将按以下顺序查找:

For example, if publishing configuration refers to projFoo / version, it would look in the order of:

  1. projFoo/版本
  2. ThisBuild/版本
  3. 全球/版本

如果默认设置的范围为 Global,例如 version 设置(请参阅 inspect version),则使用 >ThisBuild/versionGlobal/version 将能够为构建中的所有项目设置版本号.ThisBuild 这里的选择几乎是约定俗成的.

If the default setting is scoped to Global, like in the case of version setting (see inspect version), using either ThisBuild / version or Global / version would be able to set a version number for all projects in the build. The choice of ThisBuild here is almost by convention.

有时可能需要区分这两个范围.首先是源依赖.sbt 有一个内置的支持来依赖跨多个构建的子项目.在这些情况下,使用 ThisBuild 可以防止一个设置溢出到其他构建.

There are times where one might want to distinguish the two scoping. First is for source dependencies. sbt has a built-in support to depend on subprojects across multiple builds. In these cases, using ThisBuild would prevent one setting from spilling over to other builds.

在某些情况下,某些功能专门引用全局范围的设置,通常用于配置命令的行为和/或 sbt 本身的行为.Global/concurrentRestrictions 就是一个例子.在这种情况下,必须使用 Global/concurrentRestrictions.

There are also cases where some feature specifically refers to a globally scoped setting, often to configure the behavior of a command and/or the behavior of sbt itself. Global / concurrentRestrictions is an example of that. In this case, one must use Global / concurrentRestrictions.

这篇关于ThisBuild 和 Global 范围有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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