如何避免在访问Play页面时激活器执行编译任务两次?

9

我想在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)

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


有趣。加载页面似乎会触发 compile 两次。 我要报告一个问题 - Jacek Laskowski
顺便提一下,使用更短的版本 taskKey 来定义一个新任务 - lazy val hello = taskKey[Unit]("My custom hello task") - Jacek Laskowski
最终……新版本可能会修复这个问题。 - jsuereth
这个问题会导致资产任务被编译两次吗? - Joe
1
有更新吗?我们发现了相同的问题(play 2.3.8, sbt 13.7, scala 2.11.5)。在我们的情况下,任务会一遍又一遍地执行。 - Patrick Hammer
我认为应该这样写:lazy val helloTask = TaskKey[Unit]("helloTask", "hello") - 0fnt
2个回答

3

我曾经遇到过同样的问题,并找到了解决方法。

在Sbt中,您有三个按配置轴划分的作用域

  • Compile定义主要构建(src/main/scala)。
  • Test定义如何构建测试(src/test/scala)。
  • Runtime定义run任务的类路径。

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

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

helloTask := println("hello test")

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

0
这是谷歌上的第一个结果,所以我想发布我的当前解决方案,它实际上可以在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接