title: How can I run a Groovy class from the command-line within a Grails environment?
body: <p>I'm using Grails 2.1.0, and I have a Groovy class that I've written that's not dependent on services, controllers, or any of the other Grails goodness.  It uses some .jar libraries and other classes that are already in the Grails classpath.</p>
  
  <p>I want to:</p>
  
  <ul>
  <li>Run the Groovy class (or a Java class, it shouldn't mattter) use the other libraries/classes that Grails already has on its classpath (not services, not controllers, none of that).</li>
  <li>Be able to access the command line arguments <strong>[this is required]</strong></li>
  <li>Does not require bootstrapping the entire Grails environment (I need the classpath obviously, but nothing else)</li>
  </ul>
  
  <p>Ideally, I'd like to be able to do something like this:</p>
  
  <p><code>java -classpath (I_HAVE_NO_IDEA_HOW_TO_DETERMINE_THIS) com.something.MyClass param1 param2 param3</code></p>
  
  <p>Things I've already looked into:</p>
  
  <ul>
  <li>Using "grails create-script" which results in a Gant script. </li>
  <li>Using "grails run-script" </li>
  </ul>
  
  <p>The first one (using a Gant script) seems horribly wrong to me.  Using an Gant script as some sort of intermediary wrapper seems to require bootstrapping the whole Grails environment, plus I have to figure out how to get a reference to the actual class I want to call which seems to be difficult (but I Am Not A Gant Expert, so enlighten me). =) </p>
  
  <p>The second one (using run-script) sort of works... I've used this approach to call service methods before, but it has two problems: first, it bootstraps the entire Grails environment; second, there does not appear to be any way to pass the command-line arguments easily.</p>
  
  <p>Really, I just want the stuff in the classpath (and my command-line parameters) and to be able to call the main() method of my class with minimial frustration.  That being said, if you can come up with a working example of any sort that solves the issue (even if it involves some intermediary Gant or other class) I'll be happy to use that approach.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Update:  A solution that works with a Gant task, still open to better ideas if anyone has any...</p>
  
  <p>In <strong>scripts/FooBar.groovy</strong></p>
  
  <pre><code>includeTargets &lt;&lt; grailsScript("_GrailsInit")
  
  target(main: "Runs a generic script and passes parameters") {
     def myclass = classLoader.loadClass('com.whatever.scripting.GenericRunScript') 
     myclass.execute(args);
  }
  setDefaultTarget(main)
  </code></pre>
  
  <p>In <strong>src/groovy/com/whatever/scripting/GenericRunScript.groovy</strong></p>
  
  <pre><code>package com.whatever.scripting
  class GenericRunScript {
      public static execute(def args) {
          println "args=" + args.inspect()
      }
  }
  </code></pre>
  
  <p>Then from the command line, at while in the root directory of the Grails project:</p>
  
  <pre><code>$ grails compile 
  | Environment set to development.....
  | Compiling 2 source files.
  
  $ grails foo-bar test one two
  | Environment set to development....
  args='test\none\ntwo'
  </code></pre>
  
  <p>Note 1: When I first did this, I kept forgetting the compile statement, so I added that in. </p>
  
  <p>Note 2: Yes, the args are separated by carriage returns; fixing that is left as an exercise to the reader.</p>