How to run Groovy class from command line in Grails environment?

I am using Grails 2.1.0, and I have a Groovy class that I wrote that is independent of Grails services, controllers, or any other merit. It uses some .jar libraries and other classes that are already in the Grails class path.

I want to:

  • Run the Groovy class (or the Java class, it should not be matte) to use other libraries / classes that Grails already has in its path to the classes (not services, not controllers, not one of them).
  • Be able to access command line arguments [required]
  • It does not require a reboot of the entire Grails environment (I need, of course, a classpath, but nothing else)

Ideally, I would like to do something like this:

java -classpath (I_HAVE_NO_IDEA_HOW_TO_DETERMINE_THIS) com.something.MyClass param1 param2 param3

Things I've already covered:

  • Using "grails create-script", which leads to the appearance of a gant script.
  • Using "grails run-script"

The first (using the Gant script) seems terribly wrong to me. Using a Gant script, since some kind of intermediate shell seems to require loading the whole Grails environment, plus I have to figure out how to get a link to the actual class that I want to name, which seems complicated (but I don't know Gant Expert, so enlighten me). =)

The second (using run-script) kind of work ... I used this approach to call service methods before, but it has two problems: first, it loads the entire Grails environment; secondly, there is no way to easily pass command line arguments.

In fact, I just want the material in the path of the classes (and the parameters of my command line) and be able to call the main () method of my class with minimal disruption. At the same time, if you can come up with a working example of any type that solves the problem (even if it is connected with some Gant intermediary or another class), I will be glad to use this approach.

Thanks.


Update: The solution that works with the Gant task still opens up better ideas if anyone has ...

In the scripts /FooBar.groovy

 includeTargets << grailsScript("_GrailsInit") target(main: "Runs a generic script and passes parameters") { def myclass = classLoader.loadClass('com.whatever.scripting.GenericRunScript') myclass.execute(args); } setDefaultTarget(main) 

In src / groovy / com / whatever / scripting / GenericRunScript.groovy

 package com.whatever.scripting class GenericRunScript { public static execute(def args) { println "args=" + args.inspect() } } 

Then from the command line, located in the root directory of the Grails project:

 $ grails compile | Environment set to development..... | Compiling 2 source files. $ grails foo-bar test one two | Environment set to development.... args='test\none\ntwo' 

Note 1: When I first did this, I continued to forget the compilation instruction, so I added this.

Note 2: Yes, arguments are separated by carriage returns; correction that remains as an exercise for the reader.

+4
source share
2 answers

As described in http://grails.org/doc/latest/guide/commandLine.html , you can include the _GrailsClasspath and _GrailsArgParsing and all that you need. For example, if you want to parse command line arguments without creating a second script:

 $ grails create-script ArgsScript | Created file scripts/ArgsScript.groovy 

Now edit the script scripts /ArgsScript.groovy as follows:

 includeTargets << grailsScript("_GrailsArgParsing") // grailsScript("_GrailsInit") target(main: "The description of the script goes here!") { println argsMap for (p in argsMap['params']) println p } setDefaultTarget(main) 

See the result:

 $ grails args-script one two three=four | Environment set to development.... [params:[one, two, three=four]] one two three=four 

Update: OK, it's not as easy as I thought. Basically, you can run the script as a Gant task, for example. doing grails myscript or as a script e.g. by running grails run-script src/groovy/MyScript.groovy . In the first case, you have access to the parameters, as I already explained, but you will still miss part of the Grails environment, which may be a mistake. For example, you cannot access scripts or classes defined in src/groovy/ from a Gant task. On the other hand, as already discussed, if you use run-script , you cannot receive arguments.

However, you can use System.getProperty to pass command line arguments with the syntax -Dproperty=value . Also see Java system properties and environment variables.

0
source

The method described above will work, but all grails features will not be included, including domains and dependencies.

If you need everything you define in a grails project, the run-script command will do the trick

grails run- script [path to your groovy file]

http://grails.org/doc/latest/ref/Command%20Line/run-script.html

0
source

All Articles