<?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</title>
	<atom:link href="http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<pubDate>Tue, 06 Jan 2009 03:39:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Michael Tkachev</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-786</link>
		<dc:creator>Michael Tkachev</dc:creator>
		<pubDate>Mon, 21 Jul 2008 13:29:30 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-786</guid>
		<description>Hi

I've found problem with run-script and many-to-many relations (lazy initialisation exception). And there is fix - change shell execution routine to following:

        appCtx.getBeansOfType(PersistenceContextInterceptor).each { k,v -&#62;
            v.init()
        }
        def shell = new GroovyShell(classLoader, new Binding(ctx: appCtx, grailsApplication: grailsApp))
        shell.evaluate(script.text)
        appCtx.getBeansOfType(PersistenceContextInterceptor).each { k,v -&#62;
            v.flush()
            v.destroy()
        }
And add
import org.codehaus.groovy.grails.support.*
to resolve PersistenceContextInterceptor.

Thanks, Michael.</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I&#8217;ve found problem with run-script and many-to-many relations (lazy initialisation exception). And there is fix - change shell execution routine to following:</p>
<p>        appCtx.getBeansOfType(PersistenceContextInterceptor).each { k,v -&gt;<br />
            v.init()<br />
        }<br />
        def shell = new GroovyShell(classLoader, new Binding(ctx: appCtx, grailsApplication: grailsApp))<br />
        shell.evaluate(script.text)<br />
        appCtx.getBeansOfType(PersistenceContextInterceptor).each { k,v -&gt;<br />
            v.flush()<br />
            v.destroy()<br />
        }<br />
And add<br />
import org.codehaus.groovy.grails.support.*<br />
to resolve PersistenceContextInterceptor.</p>
<p>Thanks, Michael.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-785</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Tue, 08 Jul 2008 19:14:28 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-785</guid>
		<description>@Tim 

Thanks for the follow up message, that makes sense.  I used to do a fair amount of programming on windows, but I always used the cygwin shell, which I think would treat this in the same way that it does on my Mac.</description>
		<content:encoded><![CDATA[<p>@Tim </p>
<p>Thanks for the follow up message, that makes sense.  I used to do a fair amount of programming on windows, but I always used the cygwin shell, which I think would treat this in the same way that it does on my Mac.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Russell</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-784</link>
		<dc:creator>Tim Russell</dc:creator>
		<pubDate>Mon, 07 Jul 2008 20:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-784</guid>
		<description>Looks like I figured it out...

I now feel it necessary to mention that I'm running Windows XP... and unfortunately this plays a role in what's occurring. After digging through the Init.groovy code, tracing back to where the system property grails.cli.args is set and doing some more playing in that, I decided to go even further back -- to the batch file that actually launches grails on Windows machines (startGrails.bat). Even that batch file was seeing the equals sign as a space. I then created a dummy batch file:

echo %1 %2 %3

And ran it:

test.bat --id=15 b c

I'd expect the output to be the same parameters I passed in: "--id=15 b c". But Windows has a better idea:

--id 15 b

To wrap things up, everything works fine if I wrap my equals-containing parameter in double-quotes, like so:

grails test-run argumentOne argumentTwo "--id=15" argumentFour

Finally I get the desired result:

arguments are:
 argumentOne
argumentTwo
--id=15
argumentFour
argsMap = ["params":["argumentOne", "argumentTwo", "argumentFour"], "id":"15"]</description>
		<content:encoded><![CDATA[<p>Looks like I figured it out&#8230;</p>
<p>I now feel it necessary to mention that I&#8217;m running Windows XP&#8230; and unfortunately this plays a role in what&#8217;s occurring. After digging through the Init.groovy code, tracing back to where the system property grails.cli.args is set and doing some more playing in that, I decided to go even further back &#8212; to the batch file that actually launches grails on Windows machines (startGrails.bat). Even that batch file was seeing the equals sign as a space. I then created a dummy batch file:</p>
<p>echo %1 %2 %3</p>
<p>And ran it:</p>
<p>test.bat &#8211;id=15 b c</p>
<p>I&#8217;d expect the output to be the same parameters I passed in: &#8220;&#8211;id=15 b c&#8221;. But Windows has a better idea:</p>
<p>&#8211;id 15 b</p>
<p>To wrap things up, everything works fine if I wrap my equals-containing parameter in double-quotes, like so:</p>
<p>grails test-run argumentOne argumentTwo &#8220;&#8211;id=15&#8243; argumentFour</p>
<p>Finally I get the desired result:</p>
<p>arguments are:<br />
 argumentOne<br />
argumentTwo<br />
&#8211;id=15<br />
argumentFour<br />
argsMap = [&#8221;params&#8221;:[&#8221;argumentOne&#8221;, &#8220;argumentTwo&#8221;, &#8220;argumentFour&#8221;], &#8220;id&#8221;:&#8221;15&#8243;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-783</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Sat, 05 Jul 2008 18:54:22 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-783</guid>
		<description>@Tim

I'm glad you found it useful!

Regarding the issue you're asking about, I'm actually not seeing the same behavior that you're seeing with the "=" getting stripped out.  I get the normal java behavior of splitting on a space (which leaves the "-id=15" intact.  In the original script above, I actually have a "parseArguments" Gant target that takes advantage of the = being there.  This method is part of grails 1.0.3, so you should have it available if you call "parseArguments()" in your script.

Here's a slightly modified version of your script that demonstrates it:

&lt;pre lang="groovy"&gt;
grailsHome = Ant.project.properties."environment.GRAILS_HOME"

includeTargets &lt;&lt; new File ( "${grailsHome}/scripts/Init.groovy" )
target('default': "The description of the script goes here!") {
	parseArguments() // populates the argsMap variable
	println "arguments are:\n $args"
	println "argsMap = $argsMap"
}
&lt;/pre&gt;

when I run this command (slightly modified to have 2 dashes in front of the "id" for posix compliance):
&lt;pre lang="bash"&gt;
grails test-run argumentOne argumentTwo --id=15 argumentFour
&lt;/pre&gt;

Here is the output:

&lt;pre lang="bash"&gt;
Welcome to Grails 1.0.3 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /usr/local/grails		
		
Base Directory: /Users/ted/Documents/workspace/testargs
Note: No plugin scripts found
Running script /Users/ted/Documents/workspace/testargs/scripts/TestRun.groovy
Environment set to development
Done parsing arguments: ["params":["argumentOne", "argumentTwo", "argumentFour"], "id":"15"] ...
arguments are:
 argumentOne
argumentTwo
--id=15
argumentFour
argsMap = ["params":["argumentOne", "argumentTwo", "argumentFour"], "id":"15"]
&lt;/pre&gt;

I'm currently running the same versions of grails/groovy that you are:

&lt;pre lang="bash"&gt;
% groovy -v
Groovy Version: 1.5.6 JVM: 1.5.0_13-119
% grails version
Welcome to Grails 1.0.3 - http://grails.org/
...
&lt;/pre&gt;

Do you have the release version of 1.0.3 running?  I'd also try just creating a clean app ("grails create-app testargs") and pasting in the test script to see if you get the same behavior.</description>
		<content:encoded><![CDATA[<p>@Tim</p>
<p>I&#8217;m glad you found it useful!</p>
<p>Regarding the issue you&#8217;re asking about, I&#8217;m actually not seeing the same behavior that you&#8217;re seeing with the &#8220;=&#8221; getting stripped out.  I get the normal java behavior of splitting on a space (which leaves the &#8220;-id=15&#8243; intact.  In the original script above, I actually have a &#8220;parseArguments&#8221; Gant target that takes advantage of the = being there.  This method is part of grails 1.0.3, so you should have it available if you call &#8220;parseArguments()&#8221; in your script.</p>
<p>Here&#8217;s a slightly modified version of your script that demonstrates it:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">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/Init.groovy&quot;</span> <span style="color: #66cc66;">&#41;</span>
target<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'default'</span>: <span style="color: #ff0000;">&quot;The description of the script goes here!&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: #808080; font-style: italic;">// populates the argsMap variable</span>
	<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;arguments are:<span style="color: #000099; font-weight: bold;">\n</span> $args&quot;</span>
	<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;argsMap = $argsMap&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>when I run this command (slightly modified to have 2 dashes in front of the &#8220;id&#8221; for posix compliance):</p>

<div class="wp_syntax"><div class="code"><pre class="bash">grails test-run argumentOne argumentTwo --<span style="color: #007800;">id=</span><span style="color: #000000;">15</span> argumentFour</pre></div></div>

<p>Here is the output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">Welcome to Grails <span style="color: #000000;">1.0</span><span style="color: #000000;">.3</span> - http:<span style="color: #000000; font-weight: bold;">//</span>grails.org<span style="color: #000000; font-weight: bold;">/</span>
Licensed under Apache Standard License <span style="color: #000000;">2.0</span>
Grails home is <span style="color: #000000; font-weight: bold;">set</span> to: <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>grails		
&nbsp;
Base Directory: <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>ted<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testargs
Note: No plugin scripts found
Running script <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>ted<span style="color: #000000; font-weight: bold;">/</span>Documents<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>testargs<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>TestRun.groovy
Environment <span style="color: #000000; font-weight: bold;">set</span> to development
Done parsing arguments: <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: #ff0000;">&quot;argumentOne&quot;</span>, <span style="color: #ff0000;">&quot;argumentTwo&quot;</span>, <span style="color: #ff0000;">&quot;argumentFour&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>, <span style="color: #ff0000;">&quot;id&quot;</span>:<span style="color: #ff0000;">&quot;15&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ...
arguments are:
 argumentOne
argumentTwo
--<span style="color: #007800;">id=</span><span style="color: #000000;">15</span>
argumentFour
argsMap = <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: #ff0000;">&quot;argumentOne&quot;</span>, <span style="color: #ff0000;">&quot;argumentTwo&quot;</span>, <span style="color: #ff0000;">&quot;argumentFour&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>, <span style="color: #ff0000;">&quot;id&quot;</span>:<span style="color: #ff0000;">&quot;15&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>I&#8217;m currently running the same versions of grails/groovy that you are:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">%</span> groovy -v
Groovy Version: <span style="color: #000000;">1.5</span><span style="color: #000000;">.6</span> JVM: <span style="color: #000000;">1.5</span>.0_13<span style="color: #000000;">-119</span>
<span style="color: #000000; font-weight: bold;">%</span> grails version
Welcome to Grails <span style="color: #000000;">1.0</span><span style="color: #000000;">.3</span> - http:<span style="color: #000000; font-weight: bold;">//</span>grails.org<span style="color: #000000; font-weight: bold;">/</span>
...</pre></div></div>

<p>Do you have the release version of 1.0.3 running?  I&#8217;d also try just creating a clean app (&#8221;grails create-app testargs&#8221;) and pasting in the test script to see if you get the same behavior.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Russell</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-782</link>
		<dc:creator>Tim Russell</dc:creator>
		<pubDate>Sat, 05 Jul 2008 18:27:08 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-782</guid>
		<description>Hi Ted,

I've found this extremely helpful. I'm very surprised that something like this isn't already part of the grails core. I wonder if anyone else has run into some issues dealing with command line arguments: specifically, the removal of equals signs (=) from within parameters.

Here's my test script. Versions are:
-- Welcome to Grails 1.0.3 - http://grails.org/
-- Groovy Version: 1.5.6 JVM: 1.6.0_02-b06


import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
grailsHome = Ant.project.properties."environment.GRAILS_HOME"
includeTargets &#60;&#60; new File ( "${grailsHome}/scripts/Init.groovy" )  
target('default': "The description of the script goes here!") {
	println "arguments are: \n" + args + "\n"    
}

When I run "grails test-run argumentOne argumentTwo --id=15 argumentFour", I get:

arguments are:
argumentOne
argumentTwo
--id
15
argumentFour

It seems that some piece of the initialization, in turning the args variable from an array to a newline-delimited string, is treating that equals sign in "--id=15" as a separator rather than part of the argument.

Has anyone seen a way to either a) get it to stop doing that or b) get at the original command line parameters?

Thanks!</description>
		<content:encoded><![CDATA[<p>Hi Ted,</p>
<p>I&#8217;ve found this extremely helpful. I&#8217;m very surprised that something like this isn&#8217;t already part of the grails core. I wonder if anyone else has run into some issues dealing with command line arguments: specifically, the removal of equals signs (=) from within parameters.</p>
<p>Here&#8217;s my test script. Versions are:<br />
&#8211; Welcome to Grails 1.0.3 - <a href="http://grails.org/" rel="nofollow" onclick="">http://grails.org/</a><br />
&#8211; Groovy Version: 1.5.6 JVM: 1.6.0_02-b06</p>
<p>import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU<br />
grailsHome = Ant.project.properties.&#8221;environment.GRAILS_HOME&#8221;<br />
includeTargets &lt;&lt; new File ( &#8220;${grailsHome}/scripts/Init.groovy&#8221; )<br />
target(&#8217;default&#8217;: &#8220;The description of the script goes here!&#8221;) {<br />
	println &#8220;arguments are: \n&#8221; + args + &#8220;\n&#8221;<br />
}</p>
<p>When I run &#8220;grails test-run argumentOne argumentTwo &#8211;id=15 argumentFour&#8221;, I get:</p>
<p>arguments are:<br />
argumentOne<br />
argumentTwo<br />
&#8211;id<br />
15<br />
argumentFour</p>
<p>It seems that some piece of the initialization, in turning the args variable from an array to a newline-delimited string, is treating that equals sign in &#8220;&#8211;id=15&#8243; as a separator rather than part of the argument.</p>
<p>Has anyone seen a way to either a) get it to stop doing that or b) get at the original command line parameters?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rollo</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-664</link>
		<dc:creator>rollo</dc:creator>
		<pubDate>Mon, 16 Jun 2008 09:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-664</guid>
		<description>Runs perfectly, thanks a lot
But next time, pls don't forget to espcape HTML characters like gt and lt when putting code in HTML page ;-)
Rollo</description>
		<content:encoded><![CDATA[<p>Runs perfectly, thanks a lot<br />
But next time, pls don&#8217;t forget to espcape HTML characters like gt and lt when putting code in HTML page ;-)<br />
Rollo</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BarryMac</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-236</link>
		<dc:creator>BarryMac</dc:creator>
		<pubDate>Tue, 20 May 2008 21:55:26 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-236</guid>
		<description>I was just working away in the grails console and wanted to be able to run the script in the grails context from just the command line to process some csv files. 

Perfect, thanks a million! 

I agree with the comment it should be built into grails!</description>
		<content:encoded><![CDATA[<p>I was just working away in the grails console and wanted to be able to run the script in the grails context from just the command line to process some csv files. </p>
<p>Perfect, thanks a million! </p>
<p>I agree with the comment it should be built into grails!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Hurst</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-8</link>
		<dc:creator>John Hurst</dc:creator>
		<pubDate>Mon, 31 Mar 2008 21:47:43 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-8</guid>
		<description>Thank you, thank you, thank you!

I am fairly new to Grails, and for some strange reason I expected to be able to do this "out of the box" with scripts.  I see I have a long way to go to understand the Grails infrastructure.

I think "run-script" needs to become part of the standard infrastructure!

Thanks again,

John Hurst
Wellington, New Zealand</description>
		<content:encoded><![CDATA[<p>Thank you, thank you, thank you!</p>
<p>I am fairly new to Grails, and for some strange reason I expected to be able to do this &#8220;out of the box&#8221; with scripts.  I see I have a long way to go to understand the Grails infrastructure.</p>
<p>I think &#8220;run-script&#8221; needs to become part of the standard infrastructure!</p>
<p>Thanks again,</p>
<p>John Hurst<br />
Wellington, New Zealand</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-7</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Fri, 21 Mar 2008 04:33:02 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-7</guid>
		<description>Thanks guys, I'm glad you found it useful!

@Mike: Yep, using it in a batch program is exactly what drove me to make this in the first place :)

@Armin: Do you have a script that you can share that's exhibiting a problem?   My understanding of the bug that you linked to shouldn't affect the way that this run-script gant task works.

I've run into the issue that you speak of when I try to save an object, but then forget to check .hasErrors() on it after the save to see if it was successful.

The linked issue seems to be due to the normal grails shell executing one statement at a time and associating a different hibernate context with each.  Since context info isn't shared across multiple statements in the grails shell, they fail.  The way run-script is compiling the grails script with a single "evaluate", I'd think it wouldn't suffer from this.  The entire script should be run with the same hibernate session in scope.

I recreated the Customer-&#62;Sites example given in the post and was able to run the example script without hitting an issue.

I created and ran 2 test scripts with this command:

grails run-script testScript.groovy testScript2.groovy

And got this output:

testScript all done! Customer : 1, Cust sites: [Site : 1] and Site : 1
In script 2: [Customer : 1]


script1.groovy:
def s = new Site(name: "foo")
s.save()
if (s.hasErrors()) {
	println "Site Errors: ${s.errors}"
}
def cust = new Customer(name: "bar", sites: [s])
cust.save()
if (cust.hasErrors()) {
	println "Cust errors: ${cust.errors}"
}
println "testScript all done! $cust, Cust sites: ${cust.sites} and $s"


script2.groovy:
println "In script 2: ${Customer.list()}"


Again, if you have an example that demonstrates the problem, I'd love to see it.</description>
		<content:encoded><![CDATA[<p>Thanks guys, I&#8217;m glad you found it useful!</p>
<p>@Mike: Yep, using it in a batch program is exactly what drove me to make this in the first place :)</p>
<p>@Armin: Do you have a script that you can share that&#8217;s exhibiting a problem?   My understanding of the bug that you linked to shouldn&#8217;t affect the way that this run-script gant task works.</p>
<p>I&#8217;ve run into the issue that you speak of when I try to save an object, but then forget to check .hasErrors() on it after the save to see if it was successful.</p>
<p>The linked issue seems to be due to the normal grails shell executing one statement at a time and associating a different hibernate context with each.  Since context info isn&#8217;t shared across multiple statements in the grails shell, they fail.  The way run-script is compiling the grails script with a single &#8220;evaluate&#8221;, I&#8217;d think it wouldn&#8217;t suffer from this.  The entire script should be run with the same hibernate session in scope.</p>
<p>I recreated the Customer-&gt;Sites example given in the post and was able to run the example script without hitting an issue.</p>
<p>I created and ran 2 test scripts with this command:</p>
<p>grails run-script testScript.groovy testScript2.groovy</p>
<p>And got this output:</p>
<p>testScript all done! Customer : 1, Cust sites: [Site : 1] and Site : 1<br />
In script 2: [Customer : 1]</p>
<p>script1.groovy:<br />
def s = new Site(name: &#8220;foo&#8221;)<br />
s.save()<br />
if (s.hasErrors()) {<br />
	println &#8220;Site Errors: ${s.errors}&#8221;<br />
}<br />
def cust = new Customer(name: &#8220;bar&#8221;, sites: [s])<br />
cust.save()<br />
if (cust.hasErrors()) {<br />
	println &#8220;Cust errors: ${cust.errors}&#8221;<br />
}<br />
println &#8220;testScript all done! $cust, Cust sites: ${cust.sites} and $s&#8221;</p>
<p>script2.groovy:<br />
println &#8220;In script 2: ${Customer.list()}&#8221;</p>
<p>Again, if you have an example that demonstrates the problem, I&#8217;d love to see it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Armin Heinzer</title>
		<link>http://naleid.com/blog/2008/03/19/using-gant-to-execute-a-groovy-script-within-the-grails-context/#comment-6</link>
		<dc:creator>Armin Heinzer</dc:creator>
		<pubDate>Thu, 20 Mar 2008 13:20:57 +0000</pubDate>
		<guid isPermaLink="false">http://tednaleid.wordpress.com/?p=7#comment-6</guid>
		<description>Great work, exactly what I has been looking for!

In my script, I got the error: "failed to lazily initialize a collection of role: ..., no session or session was closed", but this seems to be the same problem like in the Shell (in the Console it works): http://www.nabble.com/domain-relationships-in-shell---console-td15434264.html#a15435468

Armin</description>
		<content:encoded><![CDATA[<p>Great work, exactly what I has been looking for!</p>
<p>In my script, I got the error: &#8220;failed to lazily initialize a collection of role: &#8230;, no session or session was closed&#8221;, but this seems to be the same problem like in the Shell (in the Console it works): <a href="http://www.nabble.com/domain-relationships-in-shell---console-td15434264.html#a15435468" rel="nofollow" onclick="">http://www.nabble.com/domain-relationships-in-shell&#8212;console-td15434264.html#a15435468</a></p>
<p>Armin</p>
]]></content:encoded>
	</item>
</channel>
</rss>
