【问题标题】:How to print/log val in sbt build?如何在 sbt build 中打印/记录 val?
【发布时间】:2019-08-06 22:06:10
【问题描述】:

如果我们想打印/记录一个简单的变量,例如,

lazy val config = ConfigFactory.load()

并在 sbt 中检查它的值,那我们该怎么做呢?

当我在 sbt shell 中输入它时,我得到:

> config

[error] Not a valid command: config

[error] Not a valid project ID: config

[error] Expected ':' (if selecting a configuration)

[error] Not a valid key: config (similar: configuration, deploy-configs, conflictManager)

[error] config

【问题讨论】:

    标签: sbt


    【解决方案1】:

    如果你想在 sbt 控制台输入字符串时打印一些东西,你可以添加

    lazy val configTest = settingKey[String]("example")
    
    configTest := "config test"
    

    然后你会得到

    > configTest
    [info] config test
    

    【讨论】:

    • 您还可以使用println 打印来自sbt 定义代码本身的值。就其本身而言,这不会以任何方式打印特定于 sbt 的对象,例如 settingKey 对象(它们必须需要自己的方法来获取它们的实际值),但是您可以使用它从构建中打印常规 Scala 类型定义代码。
    【解决方案2】:

    要在 sbt shell 中打印在 build.sbt 中定义的变量,请使用 sbt show

    来自 sbt 外壳

    > show {{ variable name }}
    

    来自 bash 或 zsh

    sbt 'show {{ variable name }}'
    

    示例

    sbt 'show scalaVersion'
    
    [info] Loading project definition from [...]/project
    [info] Set current project to [...] (in build file:[...])
    [info] 2.12.4
    

    来源

    sbt Reference Manual

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      相关资源
      最近更新 更多