【问题标题】:How to avoid activator executing compile task twice upon accessing Play page?如何避免激活器在访问播放页面时执行两次编译任务?
【发布时间】:2014-12-19 10:28:36
【问题描述】:

我正在尝试在编译 Play 2.3 应用程序之前运行自定义任务。我的build.sbt 文件中有这个:

lazy val helloTask = TaskKey[Unit]("hello", "hello")

helloTask := {
  println("hello test")
}

(compile in Compile) <<= (compile in Compile) dependsOn helloTask

当我运行activator ~run 然后在浏览器中打开一个页面时,我得到以下输出:

C:\Development\test>activator ~run
[info] Loading project definition from C:\Development\test\project
[info] Set current project to play (in build file:/C:/Development/test/)

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

hello test
[success] Compiled in 418ms
hello test
hello test
[info] play - Application started (Dev)

我的自定义任务似乎运行了 3 次。有没有办法避免这种情况?

【问题讨论】:

  • 有趣。加载页面似乎会触发compile 两次。 I'd report an issue.
  • 顺便说一句,使用较短的版本 taskKey 定义一个新任务 - lazy val hello = taskKey[Unit]("My custom hello task")。
  • 最终....新版本可能会解决此问题
  • 同样的问题是否会导致资产任务也被编译两次?
  • 有什么更新吗?我们可以观察到同样的问题(play 2.3.8, sbt 13.7, scala 2.11.5)。在我们的例子中,任务被一遍又一遍地执行。

标签: sbt playframework-2.3 typesafe-activator


【解决方案1】:

我遇到了同样的问题,我找到了解决方案。

在 Sbt 中,你有三个 Scopes by configuration axis :

  • 编译定义主构建 (src/main/scala)。
  • Test 定义了如何构建测试 (src/test/scala)。
  • 定义运行任务的类路径的运行时。

您必须使用 Runtime 而不是 Compile。它应该是这样的:

lazy val helloTask = taskKey[Unit]("hello")

helloTask := println("hello test")

(compile in Runtime) <<= (compile in Runtime) dependsOn helloTask

【讨论】:

    【解决方案2】:

    这是 google 上的第一个结果,所以我想发布我当前对实际适用于 play 2.8 和多项目构建的问题的解决方案。它稍作修改。 @bartholomaios 提出的 solution 对我来说是一个编译循环。

    lazy val helloTask = taskKey[Unit]("hello")
        
    helloTask := println("hello test")
    
    lazy val module1: Project = (project in file("modules/module1"))
    
    # Run a task before sbt module1/run
    ((module1 / run) in Compile) := (((module1 / run) in Compile) dependsOn Compile / helloTask).evaluated
    
    # Run a task before sbt module1/docker:stage
    ((module1 / stage) in Docker) := (((module1 / stage) in Docker) dependsOn Compile / helloTask).value
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 2023-04-02
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2020-02-28
      • 1970-01-01
      • 2013-02-16
      相关资源
      最近更新 更多