Pergunta

Good day all,

I'd like to be able to detect the mode that a Play application will use during build. Meaning I'd like to execute certain tasks within my Build.scala/build.sbt depending on whether the application is started in DEV or in PROD mode for instance.

The reason I need this is because we (the team) have implemented Grunt.js into the build process by adding it to the playRunHooks. Depending on whether the application is running in DEV mode or not we want to enable/disable some Grunt tasks.

I know I can check the application mode from within the actual application using Play.isDev and the like, is there a similar mechanism available for within the build files?

If not I would really only need to know the command that was issued by the developer (run, start, dist, stage etc.) but I can't seem to find a straight forward way of getting to know this either.

Can anyone point me in the right direction? Thank you in advance!

Foi útil?

Solução

Any build tasks that are added to playRunHooks are only executed on "play run". If you do "play stage" or "play dist" those tasks are never executed.

Outras dicas

The reason I need this is because we (the team) have implemented Grunt.js into the build process by adding it to the playRunHooks. Depending on whether the application is running in DEV mode or not we want to enable/disable some Grunt tasks.

Since you say build process and are looking to hook into the application when it's running in prod mode I think the place you're really looking to hook into is the dist command. In that case you'll want to create some tasks for your build file and get creative with .dependsOn to incorporate them.

SBT has a way of running external processes such that you could define a simple inputKey in your build.sbt file like so:

val doGulpRelease = inputKey[Unit]("Runs gulp --release")
doGulpRelease := {
    val s = streams.value
    s.log.info("Preparing to run my task")
   "gulp --release" ! s.log
    s.log.info("Done with my task")
}

Then, to hook into the dist process of play, you can set it to depend on a task created from the above:

dist in Universal <<= (dist in Universal).dependsOn(doGulpRelease.toTask(""))

This will cause your dist command to also run your custom build command, which in this case would do whatever the release task was defined to do.

In the same vein, if you need some build process to run for your tests, use test in Test and dependsOn to run whatever you need to. It seems to me you're asking an XY problem but if is the case that you're not and you really do need to run grunt subprocess's from within your running play app then you'll need to use the Global Object and hook into the onStart/onShutDown hooks and create some type of job runner for yourself. You could start here for some hints on running background tasks in play and besides that google is your friend.

Note: You may need to do some imports at the top of your build.sbt file to use the above code, and it will also depend on your sbt version, but with 0.13.5 I believe it is:

import sbt.complete._
import complete.DefaultParsers._
import com.typesafe.sbt.packager.Keys._
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top