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

Tom*_*igh 9 sbt typesafe-activator playframework-2.3

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

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

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

(compile in Compile) <<= (compile in Compile) dependsOn helloTask
Run Code Online (Sandbox Code Playgroud)

当我运行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)
Run Code Online (Sandbox Code Playgroud)

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

小智 3

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

在 Sbt 中,按配置轴划分有三个范围:

  • 编译定义了主构建 (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
Run Code Online (Sandbox Code Playgroud)