sbt在行动中书在配置中引入了密钥的概念。
然后列出默认配置:
Q1()是否可以从sbt会话中打印出所有配置的列表?如果没有,我能在sbt文档中找到有关配置的信息吗?
(Q2)用于特定配置的,例如“‘Compile”,是否可以从sbt会话中打印出配置的键列表?如果没有,我能在sbt文档中找到有关配置密钥的信息吗?
发布于 2017-08-29 22:15:24
所有配置的列表
为此,您可以使用如下所示的setting:
val allConfs = settingKey[List[String]]("Returns all configurations for the current project")
val root = (project in file("."))
  .settings(
     name := "scala-tests",
     allConfs := {
       configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList
         .map(_.name)
     }这显示了所有配置的名称。您可以访问map中每个配置的更多细节。
来自交互式sbt控制台的输出:
> allConfs
[info] * provided
[info] * test
[info] * compile
[info] * runtime
[info] * optional如果您只想打印它们,则可以在设置定义中使用settingKey[Unit]和println。
配置中所有密钥的列表
为此,我们需要一个task (可能还有其他的方法,但我还没有探索,在sbt中,我很满意如果有什么东西能起作用.)以及解析用户输入的解析器。
在这个片段中,所有用户都加入了上面的设置:
import sbt._
import sbt.Keys._
import complete.DefaultParsers._
val allConfs = settingKey[List[String]]("Returns all configurations for the current project")
val allKeys = inputKey[List[String]]("Prints all keys of a given configuration")
val root = (project in file("."))
  .settings(
     name := "scala-tests",
     allConfs := {
       configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList
         .map(_.name)
     },
     allKeys := {
       val configHints = s"One of: ${
         configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList.mkString(" ")
       }"
       val configs = spaceDelimited(configHints).parsed.map(_.toLowerCase).toSet
       val extracted: Extracted = Project.extract(state.value)
       val l = extracted.session.original.toList
         .filter(set => set.key.scope.config.toOption.map(_.name.toLowerCase)
           .exists(configs.contains))
         .map(_.key.key.label)
       l
     }
   )现在您可以像这样使用它:
$ sbt "allKeys compile"
如果您处于交互式模式,可以在allKeys之后按tab键查看提示符:
> allKeys
One of: provided test compile runtime optional因为allKeys是一个task,所以它的输出不会出现在sbt控制台上,如果您只是“返回”它,但是可以打印它。
https://stackoverflow.com/questions/45894022
复制相似问题