开发者

How to execute a Groovy Script from my Grails app?

开发者 https://www.devze.com 2022-12-14 13:48 出处:网络
Well, it seems a simple task but I didn\'t manage to make it run. I have a groovy script that runs fine under Windows Vista when calling from prompt:

Well, it seems a simple task but I didn't manage to make it run.

I have a groovy script that runs fine under Windows Vista when calling from prompt:

> cd MY_GAILS_PROJECT_DIR
> groovy cp src/groovy scripts/开发者_如何学Pythonmyscript.groovy

Now, I want to execute this script (and passing to it some input arguments) through my my Maintenance Service Class (called from a controller) as below,

class MaintenanceService {
  def executeMyScript() {
    "groovy cp src/groovy scripts/myscript.groovy".execute()
  }
}

It does not work at all! I don't even manage to have the execute() method recognizing any command (like "cd .".execute()) throwing exception:

Error 500: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified

1- How can I execute a groovy script from my grails application?

2- What are the best practices here? For instance, should I use the QuartzPlugin and then the triggerNow method for executing a script? should I use a Gant Task? If yes, how to do it?

Thank you.


If you don't mind your script running asynchronously (in a separate process to the service method), the following should work assuming groovy is on your PATH variable:

def cmd = ['groovy.bat', 'cp', 'src/groovy scripts/myscript.groovy']
cmd.execute()

If you want to view the output of the process in the application console, you should try something like this instead

// Helper class for redirecting output of process
class StreamPrinter extends Thread {
    InputStream inputStream

    StreamPrinter(InputStream is) {
        this.inputStream = is
    }

    public void run() {
        new BufferedReader(new InputStreamReader(inputStream)).withReader {reader ->
            String line
            while ((line = reader.readLine()) != null) {
                println(line)
            }
        }
    }
}

// Execute the script
def cmd = ['groovy', 'cp', 'src/groovy scripts/myscript.groovy']
Process executingProcess = cmd.execute()

// Read process output and print on console
def errorStreamPrinter = new StreamPrinter(executingProcess.err)
def outputStreamPrinter = new StreamPrinter(executingProcess.in)
[errorStreamPrinter, outputStreamPrinter]*.start()

Update: In response to your comment below, try the following (which assumes you're on Windows):

1: Create the file C:\tmp\foo.groovy. The content of this file should be simply:

println 'it works!'

2: In the groovy console, run the following:

cmd = ['groovy.bat', 'C:\\tmp\\foo.groovy']
cmd.execute().text

3: You should see the result of the script (the text 'it works!') shown in the Groovy console

If you can't get this simple example working, there's something wrong with your environment, e.g. 'groovy.bat' is not on your PATH. If you can get this example working, then you should be able to work forward from it to achieve your objective.


As of grails 1.3.6 the run-script command is built in to let you run

grails run-script myScript.groovy

For earlier versions of grails, check out my updated blog post from what Carlos posted above.


Easiest Way:

Generate an Groovy Class and place at in your /src/groovy Folder of your Grails Project. Import that Class in your Domain Class and use the Functions you defined.

My 2 Cents...


This might help as well:

http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/

Carlos


Another decision you can use GroovyScriptEngine for example:

file MyScript.groovy:

static String showMessage() {
    println("Message from showMessage")
}

file BootStrap.groovy:

class BootStrap {

    def init = { servletContext ->
        new GroovyScriptEngine("scripts")
                .loadScriptByName("MyScript.groovy")
                .showMessage()
    }
    def destroy = {

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消