<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Using Gant to execute a groovy script within the Grails context (updated)</title>
	<atom:link href="http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<pubDate>Tue, 06 Jan 2009 01:37:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-857</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Wed, 10 Dec 2008 02:56:53 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-857</guid>
		<description>I've never seen that kind of behavior before.  You could try putting "flush:true" in your save method to see if that fixes things.

Also, if you're not letting the script exit normally (but doing some sort of System.exit(0)) that could potentially interrupt things too.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve never seen that kind of behavior before.  You could try putting &#8220;flush:true&#8221; in your save method to see if that fixes things.</p>
<p>Also, if you&#8217;re not letting the script exit normally (but doing some sort of System.exit(0)) that could potentially interrupt things too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter-Frank Spierenburg</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-856</link>
		<dc:creator>Peter-Frank Spierenburg</dc:creator>
		<pubDate>Tue, 09 Dec 2008 23:46:58 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-856</guid>
		<description>Upon further examination, there appears to be some kind of time-delay between statements in the frails shell and SQL winding up in prodDB.log. If I kill the shell too early, no SQL gets logged, and the user is effectively not created. I suspect that what is going on is that the GANT script ends before the database layer has an opportunity to persist the data. I hope I don't have to add a time delay to my GANT script to account for this. Is there some kind of synchronization method that I can call to ensure that save()d data are actually persisted?

Peter.</description>
		<content:encoded><![CDATA[<p>Upon further examination, there appears to be some kind of time-delay between statements in the frails shell and SQL winding up in prodDB.log. If I kill the shell too early, no SQL gets logged, and the user is effectively not created. I suspect that what is going on is that the GANT script ends before the database layer has an opportunity to persist the data. I hope I don&#8217;t have to add a time delay to my GANT script to account for this. Is there some kind of synchronization method that I can call to ensure that save()d data are actually persisted?</p>
<p>Peter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-852</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Tue, 09 Dec 2008 06:47:42 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-852</guid>
		<description>Peter, that sounds more like a problem with your member save code than the script.  Are you sure it saved?

If you open up a "grails console" and paste your script in there and run it, does it get saved in the DB?

Make sure you try to "validate()" before saving to ensure that you have all of the required fields on your domain object.  See the grails docs on &lt;a href="http://grails.org/Validation" rel="nofollow"&gt;validation&lt;/a&gt; for more details.

Also, make sure that you're running the script against the environment that you think you're running it against.  You can specify your environment as the first grails parameter:

&lt;pre lang="bash"&gt;
grails prod run-script userScripts/env.groovy
&lt;/pre&gt;

You can see what environment is currently running by putting this in your script:
&lt;pre lang="groovy"&gt;
println grails.util.GrailsUtil.environment
&lt;/pre&gt;

If I save that to userScripts/env.groovy and run the above command it prints out "production".</description>
		<content:encoded><![CDATA[<p>Peter, that sounds more like a problem with your member save code than the script.  Are you sure it saved?</p>
<p>If you open up a &#8220;grails console&#8221; and paste your script in there and run it, does it get saved in the DB?</p>
<p>Make sure you try to &#8220;validate()&#8221; before saving to ensure that you have all of the required fields on your domain object.  See the grails docs on <a href="http://grails.org/Validation" rel="nofollow" onclick="">validation</a> for more details.</p>
<p>Also, make sure that you&#8217;re running the script against the environment that you think you&#8217;re running it against.  You can specify your environment as the first grails parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">grails prod run-script userScripts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">env</span>.groovy</pre></div></div>

<p>You can see what environment is currently running by putting this in your script:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #993399;">println</span> grails.<span style="color: #006600;">util</span>.<span style="color: #006600;">GrailsUtil</span>.<span style="color: #006600;">environment</span></pre></div></div>

<p>If I save that to userScripts/env.groovy and run the above command it prints out &#8220;production&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter-Frank Spierenburg</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-848</link>
		<dc:creator>Peter-Frank Spierenburg</dc:creator>
		<pubDate>Mon, 08 Dec 2008 20:29:29 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-848</guid>
		<description>I'm trying to create a script to add new members to my webapp. I'm trying to invoke the MemberController's save() method to save the new member. The script claims to have created the new member, and I can find the new member using Member.findAll(), but the member's record doesn't show up in the database (when I choose production). And when I run the script a second time, Member.findAll() only finds the newly created member, not any of the members I've already created.

Peter.</description>
		<content:encoded><![CDATA[<p>I&#8217;m trying to create a script to add new members to my webapp. I&#8217;m trying to invoke the MemberController&#8217;s save() method to save the new member. The script claims to have created the new member, and I can find the new member using Member.findAll(), but the member&#8217;s record doesn&#8217;t show up in the database (when I choose production). And when I run the script a second time, Member.findAll() only finds the newly created member, not any of the members I&#8217;ve already created.</p>
<p>Peter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-629</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 11 Jun 2008 23:31:48 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-629</guid>
		<description>Sorry for the delay.  I ended up just running my script in the grails console.  I'm not familiar enough with grails to try what was suggested by Armin.</description>
		<content:encoded><![CDATA[<p>Sorry for the delay.  I ended up just running my script in the grails console.  I&#8217;m not familiar enough with grails to try what was suggested by Armin.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-494</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Mon, 02 Jun 2008 05:05:29 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-494</guid>
		<description>@Steve  Did it give you some sort of error, or were the items just not found?  If they weren't found, I'd be sure that you're calling the script with the right environment (noted in the comments above).  If you're getting an error, does the stuff mentioned above by Armin Heinzer fix things for you?</description>
		<content:encoded><![CDATA[<p>@Steve  Did it give you some sort of error, or were the items just not found?  If they weren&#8217;t found, I&#8217;d be sure that you&#8217;re calling the script with the right environment (noted in the comments above).  If you&#8217;re getting an error, does the stuff mentioned above by Armin Heinzer fix things for you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-481</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 01 Jun 2008 20:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-481</guid>
		<description>I tried your code to load my script, but my script wasn't able to read existing entries in the database (which works when loaded into grails console).</description>
		<content:encoded><![CDATA[<p>I tried your code to load my script, but my script wasn&#8217;t able to read existing entries in the database (which works when loaded into grails console).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-469</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Sat, 31 May 2008 18:10:13 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-469</guid>
		<description>@Oliver, yes, the current script doesn't pass any arguments to the script and simply treats all parameters as separate script files.

It wouldn't be that hard to change it to have it instead only parse a single file and pass the parameters along to the calling script.

The call to parseArguments would simply grab the first parameter as the file name and create a variable that contains the remaining arguments.

Then, executeScript would add the remaning parameters as a new item in the Binding object.

Here's an updated script with these changes (I called it RunScriptWithParms.groovy):

&lt;pre lang="groovy"&gt;
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
import org.springframework.orm.hibernate3.SessionFactoryUtils
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.support.TransactionSynchronizationManager

grailsHome = Ant.project.properties."environment.GRAILS_HOME"

includeTargets &lt;&lt; new File ( "${grailsHome}/scripts/Package.groovy" )
includeTargets &lt;&lt; new File ( "${grailsHome}/scripts/Bootstrap.groovy" )

target('default': "Execute the specified script after starting up the application environment") {
    depends(checkVersion, configureProxy, packageApp, classpath)
    runScript()
}

target(runScript: "Main implementation that executes the specified script after starting up the application environment") {
    parseArguments()
    if (argsMap["params"].size() == 0) {
        event("StatusError", ["Required script name parameter is missing"])
        System.exit 1
    }
    compile()
    classLoader = new URLClassLoader([classesDir.toURL()] as URL[], rootLoader)
    Thread.currentThread().setContextClassLoader(classLoader)
    loadApp()
    configureApp()
	configureHibernateSession()
	scriptFile = argsMap["params"].getAt(0)
	// remove our script name from the params so when we call the script it's not in there
	argsMap["params"].remove(scriptFile)
    executeScript(scriptFile, classLoader, argsMap)
}

def configureHibernateSession() {
	// without this you'll get a lazy initialization exception when using a many-to-many relationship
	def sessionFactory = appCtx.getBean("sessionFactory")
	def session = SessionFactoryUtils.getSession(sessionFactory, true)
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session))
}

def executeScript(scriptFile, classLoader, argsMap) {
    File script = new File(scriptFile)
    if (script.exists()) {
        def shell = new GroovyShell(classLoader, new Binding(ctx: appCtx, grailsApplication: grailsApp, argsMap: argsMap, args: args))
        shell.evaluate(script.text)
    } else {
        event("StatusError", ["Designated script doesn't exist: $scriptFile"])
    }
}

// this argument parsing target was submitted as a patch to grails and is availble in Init.groovy as of 1.0.3
// see http://jira.codehaus.org/browse/GRAILS-2663

argsMap = [params: []]

target(parseArguments: "Parse the arguments passed on the command line") {
    args?.tokenize().each {  token -&gt;
        def nameValueSwitch = token =~ "--?(.*)=(.*)"
        if (nameValueSwitch.matches()) { // this token is a name/value pair (ex: --foo=bar or -z=qux)
            argsMap[nameValueSwitch[0][1]] = nameValueSwitch[0][2]
        } else {
            def nameOnlySwitch = token =~ "--?(.*)"
            if (nameOnlySwitch.matches()) {  // this token is just a switch (ex: -force or --help)
                argsMap[nameOnlySwitch[0][1]] = true
            } else { // single item tokens, append in order to an array of params
                argsMap["params"] &lt;&lt; token
            }
        }
    }
    event("StatusUpdate", ["Done parsing arguments: $argsMap"])
}
&lt;/pre&gt;

If you have this groovy script (userScripts/showParams.groovy):

&lt;pre lang="groovy"&gt;
println "in showParams script, argsMap is: $argsMap"
println "in showParams script, raw args are: $args"
&lt;/pre&gt;

And call it with this command line:

&lt;pre lang="bash"&gt;
grails run-script-with-parms userScripts/showParams.groovy --foo=zazz -b=quux
&lt;/pre&gt;

You'll get this as a result with the args passed in:

&lt;pre lang="bash"&gt;
in showParams script, argsMap is: ["params":[], "foo":"zazz", "b":"quux"]
in showParams script, raw args are: userScripts/showParams.groovy
--foo=bar
--foo=zazz
-b=quux
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@Oliver, yes, the current script doesn&#8217;t pass any arguments to the script and simply treats all parameters as separate script files.</p>
<p>It wouldn&#8217;t be that hard to change it to have it instead only parse a single file and pass the parameters along to the calling script.</p>
<p>The call to parseArguments would simply grab the first parameter as the file name and create a variable that contains the remaining arguments.</p>
<p>Then, executeScript would add the remaning parameters as a new item in the Binding object.</p>
<p>Here&#8217;s an updated script with these changes (I called it RunScriptWithParms.groovy):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #a1a100;">import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU</span>
<span style="color: #a1a100;">import org.springframework.orm.hibernate3.SessionFactoryUtils</span>
<span style="color: #a1a100;">import org.springframework.orm.hibernate3.SessionHolder</span>
<span style="color: #a1a100;">import org.springframework.transaction.support.TransactionSynchronizationManager</span>
&nbsp;
grailsHome <span style="color: #66cc66;">=</span> Ant.<span style="color: #006600;">project</span>.<span style="color: #006600;">properties</span>.<span style="color: #ff0000;">&quot;environment.GRAILS_HOME&quot;</span>
&nbsp;
includeTargets <span style="color: #b1b100;">&lt;&lt;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;${grailsHome}/scripts/Package.groovy&quot;</span> <span style="color: #66cc66;">&#41;</span>
includeTargets <span style="color: #b1b100;">&lt;&lt;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;${grailsHome}/scripts/Bootstrap.groovy&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
target<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'default'</span>: <span style="color: #ff0000;">&quot;Execute the specified script after starting up the application environment&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    depends<span style="color: #66cc66;">&#40;</span>checkVersion, configureProxy, packageApp, classpath<span style="color: #66cc66;">&#41;</span>
    runScript<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
target<span style="color: #66cc66;">&#40;</span>runScript: <span style="color: #ff0000;">&quot;Main implementation that executes the specified script after starting up the application environment&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    parseArguments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>argsMap<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;StatusError&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Required script name parameter is missing&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">exit</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #66cc66;">&#125;</span>
    compile<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    classLoader <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">URLClassLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>classesDir.<span style="color: #CC0099;">toURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">URL</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, rootLoader<span style="color: #66cc66;">&#41;</span>
    <span style="color: #aaaadd; font-weight: bold;">Thread</span>.<span style="color: #006600;">currentThread</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setContextClassLoader</span><span style="color: #66cc66;">&#40;</span>classLoader<span style="color: #66cc66;">&#41;</span>
    loadApp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    configureApp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	configureHibernateSession<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	scriptFile <span style="color: #66cc66;">=</span> argsMap<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">getAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #808080; font-style: italic;">// remove our script name from the params so when we call the script it's not in there</span>
	argsMap<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>scriptFile<span style="color: #66cc66;">&#41;</span>
    executeScript<span style="color: #66cc66;">&#40;</span>scriptFile, classLoader, argsMap<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> configureHibernateSession<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// without this you'll get a lazy initialization exception when using a many-to-many relationship</span>
	<span style="color: #000000; font-weight: bold;">def</span> sessionFactory <span style="color: #66cc66;">=</span> appCtx.<span style="color: #006600;">getBean</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">def</span> session <span style="color: #66cc66;">=</span> SessionFactoryUtils.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span>sessionFactory, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
	TransactionSynchronizationManager.<span style="color: #006600;">bindResource</span><span style="color: #66cc66;">&#40;</span>sessionFactory, <span style="color: #000000; font-weight: bold;">new</span> SessionHolder<span style="color: #66cc66;">&#40;</span>session<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> executeScript<span style="color: #66cc66;">&#40;</span>scriptFile, classLoader, argsMap<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">File</span> script <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #66cc66;">&#40;</span>scriptFile<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>script.<span style="color: #006600;">exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> shell <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GroovyShell<span style="color: #66cc66;">&#40;</span>classLoader, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Binding</span><span style="color: #66cc66;">&#40;</span>ctx: appCtx, grailsApplication: grailsApp, argsMap: argsMap, args: args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        shell.<span style="color: #006600;">evaluate</span><span style="color: #66cc66;">&#40;</span>script.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
        event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;StatusError&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Designated script doesn't exist: $scriptFile&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// this argument parsing target was submitted as a patch to grails and is availble in Init.groovy as of 1.0.3</span>
<span style="color: #808080; font-style: italic;">// see http://jira.codehaus.org/browse/GRAILS-2663</span>
&nbsp;
argsMap <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>params: <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
target<span style="color: #66cc66;">&#40;</span>parseArguments: <span style="color: #ff0000;">&quot;Parse the arguments passed on the command line&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    args<span style="color: #66cc66;">?</span>.<span style="color: #CC0099;">tokenize</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>  token -<span style="color: #66cc66;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">def</span> nameValueSwitch <span style="color: #66cc66;">=</span> token <span style="color: #66cc66;">=</span>~ <span style="color: #ff0000;">&quot;--?(.*)=(.*)&quot;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nameValueSwitch.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// this token is a name/value pair (ex: --foo=bar or -z=qux)</span>
            argsMap<span style="color: #66cc66;">&#91;</span>nameValueSwitch<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> nameValueSwitch<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">def</span> nameOnlySwitch <span style="color: #66cc66;">=</span> token <span style="color: #66cc66;">=</span>~ <span style="color: #ff0000;">&quot;--?(.*)&quot;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>nameOnlySwitch.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>  <span style="color: #808080; font-style: italic;">// this token is just a switch (ex: -force or --help)</span>
                argsMap<span style="color: #66cc66;">&#91;</span>nameOnlySwitch<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// single item tokens, append in order to an array of params</span>
                argsMap<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">&lt;&lt;</span> token
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;StatusUpdate&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Done parsing arguments: $argsMap&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you have this groovy script (userScripts/showParams.groovy):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;in showParams script, argsMap is: $argsMap&quot;</span>
<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;in showParams script, raw args are: $args&quot;</span></pre></div></div>

<p>And call it with this command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">grails run-script-with-parms userScripts<span style="color: #000000; font-weight: bold;">/</span>showParams.groovy --<span style="color: #007800;">foo=</span>zazz -<span style="color: #007800;">b=</span>quux</pre></div></div>

<p>You&#8217;ll get this as a result with the args passed in:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">in</span> showParams script, argsMap is: <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>, <span style="color: #ff0000;">&quot;foo&quot;</span>:<span style="color: #ff0000;">&quot;zazz&quot;</span>, <span style="color: #ff0000;">&quot;b&quot;</span>:<span style="color: #ff0000;">&quot;quux&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">in</span> showParams script, raw args are: userScripts<span style="color: #000000; font-weight: bold;">/</span>showParams.groovy
--<span style="color: #007800;">foo=</span>bar
--<span style="color: #007800;">foo=</span>zazz
-<span style="color: #007800;">b=</span>quux</pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-463</link>
		<dc:creator>Oliver</dc:creator>
		<pubDate>Sat, 31 May 2008 09:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-463</guid>
		<description>Thanks. Another question: It is currently not supported to pass arguments to the script because you basically treat all arguments as seperate script files right?</description>
		<content:encoded><![CDATA[<p>Thanks. Another question: It is currently not supported to pass arguments to the script because you basically treat all arguments as seperate script files right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/#comment-452</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Fri, 30 May 2008 14:23:56 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=11#comment-452</guid>
		<description>@Oliver

By default, scripts run in the development environment.  You can run any script in another environment by using the &lt;code&gt;-Dgrails.env &#60;env_name&#62;&lt;/code&gt;parameter.

Here's an example running in the "test" environment (where environments are defined in the grails-app/conf/Config.groovy file)
&lt;pre lang="bash"&gt;
grails -Dgrails.env=test run-script userScripts/createBook.groovy
&lt;/pre&gt;

You can take a look at the &lt;a href="http://grails.org/doc/1.0.x/guide/3.%20Configuration.html#3.2%20Environments" rel="nofollow"&gt;environments section of the Grails documentation&lt;/a&gt; for more information.</description>
		<content:encoded><![CDATA[<p>@Oliver</p>
<p>By default, scripts run in the development environment.  You can run any script in another environment by using the <code>-Dgrails.env &lt;env_name&gt;</code>parameter.</p>
<p>Here&#8217;s an example running in the &#8220;test&#8221; environment (where environments are defined in the grails-app/conf/Config.groovy file)</p>

<div class="wp_syntax"><div class="code"><pre class="bash">grails -Dgrails.<span style="color: #007800;">env=</span>test run-script userScripts<span style="color: #000000; font-weight: bold;">/</span>createBook.groovy</pre></div></div>

<p>You can take a look at the <a href="http://grails.org/doc/1.0.x/guide/3.%20Configuration.html#3.2%20Environments" rel="nofollow" onclick="">environments section of the Grails documentation</a> for more information.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
