【问题标题】:Why sbt executes redundant task?为什么 sbt 执行冗余任务?
【发布时间】:2017-01-25 19:37:37
【问题描述】:

考虑一个简单的 sbt 文件:

lazy val task = taskKey[Unit]("task")

lazy val p = project.in(file(".")).settings(
  packageBin in Compile := {
    println("compile")
    (packageBin in Compile).value
  },
  task := {
    sys.props.get("conf") match {
      case Some(t) => println(t)
      case None => println((packageBin in Compile).value)
    }
  }
)

还有两次 sbt 运行:

sbt task
[info] Set current project to p (in build file:...)
compile
.../target/scala-2.10/p_2.10-0.1-SNAPSHOT.jar
[success] Total time: 1 s, completed Jan 20, 2017 1:11:48 PM

sbt -Dconf=xxx task
[info] Set current project to p (in build file:...)
compile
xxx
[success] Total time: 1 s, completed Jan 20, 2017 1:13:39 PM

你可以看到任务编译在两种情况下都被执行,认为它看起来应该只在第一种情况下执行。

我的问题是:

  1. 为什么会这样?我认为这是因为宏值的实现,但我对 scala 宏和 sbt 内部的理解太低,无法准确解释原因。
  2. 除了定义两个不同的任务之外,还有其他解决方法吗?
  3. 我是不是依赖道具使用了错误的模式?

【问题讨论】:

    标签: sbt


    【解决方案1】:

    这是因为task 键是根据packageBin in Compile 键定义的。所以即使它的值只在代码的一个分支中使用,它的效果(println)也会在两个分支中执行。

    解决方法是使用taskDyn:http://www.scala-sbt.org/0.13/docs/Tasks.html#Dynamic+Computations+with

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2015-07-08
      • 2012-10-07
      相关资源
      最近更新 更多