gpt4 book ai didi

sbt - 范围如何影响构建中的键?什么是范围轴?

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

我试图了解 SBT scope axes是以及它们如何影响构建映射中的作用域键。

共有三个范围轴:project , configurationtask .

  • 以下元组是构建映射的作用域键吗?

    (项目轴值、配置轴值、任务轴值、未作用域键值)
  • 或者一个作用域键(概念上)是一对具有以下三种形式之一的键?

    (项目轴的值,无范围键的值)

    (配置轴的值,无作用域键的值)

    (任务轴的值,无范围键的值)
  • 还是作用域键是其他东西,如果是的话,它看起来如何(概念上)?

  • 所以问题可能是什么是(概念上)作用域键?
  • 它是一个四元组吗?
  • 是一对吗?
  • 是别的什么吗,如果是的话,那么它看起来如何(概念上)?
  • 最佳答案

    来自 Custom settings and tasks :

    Keys have one of three types: SettingKey, TaskKey and InputKey.



    键的示例可以是:
    val scalaVersion = settingKey[String]("The version of Scala used for building.")

    它在 sbt 中默认可用并查询你执行的值 show :
    > show scalaVersion
    [info] 2.10.4

    执行 inspect了解键的详细信息(这不完全是键,而是您很快就会了解的设置):
    > inspect scalaVersion
    [info] Setting: java.lang.String = 2.10.4
    [info] Description:
    [info] The version of Scala used for building.
    [info] Provided by:
    [info] */*:scalaVersion
    [info] Defined at:
    [info] (sbt.Defaults) Defaults.scala:236
    [info] Reverse dependencies:
    [info] *:libraryDependencies
    [info] *:scalaInstance
    [info] *:evicted
    [info] *:dependencyUpdatesData
    [info] *:update
    [info] *:allDependencies
    [info] Delegates:
    [info] *:scalaVersion
    [info] {.}/*:scalaVersion
    [info] */*:scalaVersion
    [info] Related:
    [info] */*:scalaVersion

    您可以使用宏 settingKey 定义自己的 key :
    lazy val abc: sbt.SettingKey[String] = settingKey[String]("My own setting key")

    根据 sbt.SettingKey 的 scaladoc :

    Identifies a setting. It consists of three parts: the scope, the name, and the type of a value associated with this key. The scope is represented by a value of type Scope. The name and the type are represented by a value of type AttributeKey[T]. Instances are constructed using the companion object.



    正如您在 scaladoc 中看到的,任何 SettingKey有所需的 def scope: Scope描述设置的范围。很快就会被解释。

    在为它分配值(通过计算)之前,您无法看到构建中的键。您分配一个 Initialize[T]或只是 TSettingKey[T]通过 := , +=++= (更常用和推荐)或 <++= , <+=<<=运算符(即 sbt.SettingKey 的 scaladoc 中描述的 Scala 方法)。
    abc := "my value"

    上面的行等效于下面的行(它们是可互换的):
    abc.:=("my value")

    正如您在 := 的定义中看到的那样:
    final def :=(v: T): Def.Setting[T]

    结果是 Setting[T] .

    因此,这对( SettingKey[T]Initialize[T] )或简单地( SettingKey[T]T )是 Setting[T] .
    Setting[T] 类型的设置在哪里 T是作为设置评估结果的值的类型,描述了对构建中所有设置的映射的转换。这是构建加载后将在键下可用的值的配方。

    现在,最后一部分,范围。

    您可能已经注意到,设置总是在范围内。您可以使用 final def in(scope: Scope): SettingKey[T] 更改设置的范围。 .

    使用 inspect了解设置。
    > inspect abc
    [info] Setting: java.lang.String = my value
    [info] Description:
    [info] My new setting key
    [info] Provided by:
    [info] {file:/Users/jacek/sandbox/sbt-theory/}sbt-theory/*:abc
    [info] Defined at:
    [info] /Users/jacek/sandbox/sbt-theory/build.sbt:3
    [info] Delegates:
    [info] *:abc
    [info] {.}/*:abc
    [info] */*:abc

    您可以看到设置将( Delegates 部分)委托(delegate)给 *:abc , {.}/*:abc*/*:abc .我将描述为 A/B:K 的其他地方是 A - 项目轴, B - 配置轴和 K键或任务范围(因为 sbt 允许出现在 :: 之后的属性)。

    猜猜 *:abc 的值是多少是。 inspect它。
    > inspect *:abc
    [info] Setting: java.lang.String = my value
    [info] Description:
    [info] My new setting key
    [info] Provided by:
    [info] {file:/Users/jacek/sandbox/sbt-theory/}sbt-theory/*:abc
    [info] Defined at:
    [info] /Users/jacek/sandbox/sbt-theory/build.sbt:3
    [info] Delegates:
    [info] *:abc
    [info] {.}/*:abc
    [info] */*:abc

    猜猜 {.}/*:abc 的值是多少对应于表示为 {.} 的此构建在全局配置 * . inspect它。
    > inspect {.}/*:abc
    [info] No entry for key.
    [info] Description:
    [info] My new setting key
    [info] Delegates:
    [info] {.}/*:abc
    [info] */*:abc
    [info] Related:
    [info] *:abc
    abc 没有任何值(value)设置 ThisBuild (又名 thisBuild )范围。设置在 build.sbt使用:
    abc in ThisBuild := "abc in ThisBuild"
    inspect它(在 reload 之后重新加载构建的更改):
    > inspect {.}/*:abc
    [info] Setting: java.lang.String = abc in ThisBuild
    [info] Description:
    [info] My new setting key
    [info] Provided by:
    [info] {file:/Users/jacek/sandbox/sbt-theory/}/*:abc
    [info] Defined at:
    [info] /Users/jacek/sandbox/sbt-theory/build.sbt:5
    [info] Delegates:
    [info] {.}/*:abc
    [info] */*:abc
    [info] Related:
    [info] *:abc

    随着变化 build.sbt如下所示:
    lazy val abc: sbt.SettingKey[String] = settingKey[String]("My new setting key")

    abc := "my value"

    abc in ThisBuild := "abc in ThisBuild"

    说有 abc每个范围具有不同值的键 - ThisBuildGlobal .清除?问远点!

    关于sbt - 范围如何影响构建中的键?什么是范围轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25988630/

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