Search code examples
playframeworkrake

How to define arbitrary tasks in the Play Framework? (like ruby rake)


How to define arbitrary tasks in the Play Framework?

I mean tasks run from the command line, something similar to ruby rake.

I'm aware of the ant tool but looking for a better alternative.


Solution

  • [edit] This answer is for the Play 1.* series!

    You should write a custom module, then your commands go into the commands.py file, ref: http://www.playframework.org/documentation/1.2.4/releasenotes-1.1#commands

    You can look at existing modules to get inspired, eg: https://github.com/sim51/logisima-play-yml/blob/master/commands.py

    Basically you define the commands you want and launch them from the "execute" method, eg:

    COMMANDS = ['namespace:command']
    
    def execute(**kargs):
        command = kargs.get("command")
        app = kargs.get("app")
        args = kargs.get("args")
        env = kargs.get("env")
    
        if command == "namespace:command":
            do_something()
    

    if you want to launch something java - often the case! -:

    def do_something():
        java_cmd = app.java_cmd([], None, "play.modules.mymodule.MyClass", args)
            try:
                subprocess.call(java_cmd, env=os.environ)
            except OSError:
                print "Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly (the java executable should reside at JAVA_HOME/bin/java). "
                sys.exit(-1)
            print
    

    Ps.

    creating a custom module is as easy as:

    play new-module mymodule
    

    This is a primer: http://playframework.wordpress.com/2011/02/27/play-modules/ , considering that official Play! module documentation is quite limited in that respect

    edit

    I thought I'd add a little piece of information:

    before being able to execute your commands, you must BUILD your module. It does not run like the rest of play with a dynamic compilation.

    play build-module mymodule
    

    new-module/build-module expect the module to be at the root of the project folder, but if you have many that becomes a mess. build-module module-srcs/mymodule works perfectly fine.