如何在 Play 框架中定义任意任务?(如红宝石耙) [英] How to define arbitrary tasks in the Play Framework? (like ruby rake)

查看:58
本文介绍了如何在 Play 框架中定义任意任务?(如红宝石耙)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Play 框架中定义任意任务?

How to define arbitrary tasks in the Play Framework?

我的意思是从命令行运行任务,类似于 ruby​​ rake.

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

我知道 ant 工具,但正在寻找更好的替代品.

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

推荐答案

[edit] 此答案适用于 Play 1.* 系列!

您应该编写一个自定义模块,然后您的命令进入commands.py 文件,参考:http://www.playframework.org/documentation/1.2.4/releasenotes-1.1#commands

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

您可以查看现有模块以获取灵感,例如:https://github.com/sim51/logisima-play-yml/blob/master/commands.py

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()

如果你想启动一些java - 通常是这样!-:

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

附言.

创建自定义模块就像:

play new-module mymodule

这是一个入门:http://playframework.wordpress.com/2011/02/27/play-modules/ ,考虑到官方 播放!模块文档在这方面非常有限

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

编辑

我想我会添加一些信息:

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 期望模块位于项目文件夹的根目录下,但如果你有很多模块就会变得一团糟.build-module module-srcs/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.

这篇关于如何在 Play 框架中定义任意任务?(如红宝石耙)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆