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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With