How to avoid activator executing compile task twice upon accessing Play page?
Asked Answered
A

2

9

I am trying to run a custom task before compilation of a Play 2.3 application. I have this in my build.sbt file:

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

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

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

When I run activator ~run and then open a page in the browser, I get the following output:

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)

It seems my custom task is running three times. Is there a way I can avoid this?

Alack answered 23/10, 2014 at 17:2 Comment(6)
Interesting. It appears that loading a page triggers compile twice. I'd report an issue.Pluto
BTW, use the shorter version taskKey to define a new task - lazy val hello = taskKey[Unit]("My custom hello task").Pluto
Eventually.... The. New release may fix thisTelespectroscope
Would this same issue cause assets tasks to be compiled twice as well?Chronicles
Any update? We can observe the same issue (play 2.3.8, sbt 13.7, scala 2.11.5). In our case, the task gets executed over and over again.Jobholder
I think it should be: lazy val helloTask = TaskKey[Unit]("helloTask", "hello")Pete
H
3

I had the same problem and I found solution.

In Sbt you have three Scopes by configuration axis :

  • Compile which defines the main build (src/main/scala).
  • Test which defines how to build tests (src/test/scala).
  • Runtime which defines the classpath for the run task.

You must use Runtime instead of Compile. It should looks like this:

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

helloTask := println("hello test")

(compile in Runtime) <<= (compile in Runtime) dependsOn helloTask
Highpitched answered 9/10, 2015 at 20:3 Comment(0)
T
0

This was the first result on google so I would like to post my current solution to the problem which actually works with play 2.8 and a multi project build. It is slightly modified. The proposed solution by @bartholomaios results in a compile loop for me.

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
Turley answered 13/8, 2020 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.