Quantcast

Calling on groovy script from another

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Calling on groovy script from another

Lloyd Meinholz-2
I have been working on some scheduled jobs that are written in groovy. There is some common code to each of the scripts that I have pulled out into a Utils class. I would like to "include" the code from this script in each of the job scripts. I am accomplishing this now by compiling the Utils.groovy into a jar file and putting that in the .groovy/lib of the process that executes the jobs. Is there a cleaner alternative to doing this?

It may not make sense from a language perspective, but it would sure be nice from a script developers perspective to be able to do something like:

import Utils

in my job scripts (as long as Utils was in the same package as the job script).

Thanks,

Lloyd

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling on groovy script from another

Andre Steingress
Hi Lloyd,

one option would be to use Script.evaluate:

evaluate(new File('./MyUtils.groovy'))

Cheers, André

[0] http://groovy.codehaus.org/api/groovy/lang/Script.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling on groovy script from another

Jochen Theodorou
In reply to this post by Lloyd Meinholz-2
Am 22.02.2012 00:39, schrieb Lloyd Meinholz:

> I have been working on some scheduled jobs that are written in groovy.
> There is some common code to each of the scripts that I have pulled out
> into a Utils class. I would like to "include" the code from this script
> in each of the job scripts. I am accomplishing this now by compiling the
> Utils.groovy into a jar file and putting that in the .groovy/lib of the
> process that executes the jobs. Is there a cleaner alternative to doing
> this?
>
> It may not make sense from a language perspective, but it would sure be
> nice from a script developers perspective to be able to do something like:
>
> import Utils
>
> in my job scripts (as long as Utils was in the same package as the job
> script).

if you mention .groovy/lib, then I assume you use the command line and
with that a GroovyClassLoader. GCL has the ability to compile scripts on
the fly. If you have a Script

println Utils.toString(new MyGroovyClass())

and Utils.groovy and MyGroovyClass.groovy are in a classpath root GCL
can see and if they are not existing as class already, then GCL will
compile those files for you automatically.

So the important part is that there is a classpath element pointing to
Utils.groovy or Utils.class and you are done. You don't need to put it
in .groovy/lib, for example the current directory is also automatically
part of the classpath. If you don't want to use the classpath option on
the command line you can also edit the groovy-starter.conf and add the
path there.

bye blackdrag


--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling on groovy script from another

Lloyd Meinholz-2
Thanks, somehow missed those options.

Lloyd


On Wed, Feb 22, 2012 at 4:52 AM, Jochen Theodorou <[hidden email]> wrote:
Am 22.02.2012 00:39, schrieb Lloyd Meinholz:

I have been working on some scheduled jobs that are written in groovy.
There is some common code to each of the scripts that I have pulled out
into a Utils class. I would like to "include" the code from this script
in each of the job scripts. I am accomplishing this now by compiling the
Utils.groovy into a jar file and putting that in the .groovy/lib of the
process that executes the jobs. Is there a cleaner alternative to doing
this?

It may not make sense from a language perspective, but it would sure be
nice from a script developers perspective to be able to do something like:

import Utils

in my job scripts (as long as Utils was in the same package as the job
script).

if you mention .groovy/lib, then I assume you use the command line and with that a GroovyClassLoader. GCL has the ability to compile scripts on the fly. If you have a Script

println Utils.toString(new MyGroovyClass())

and Utils.groovy and MyGroovyClass.groovy are in a classpath root GCL can see and if they are not existing as class already, then GCL will compile those files for you automatically.

So the important part is that there is a classpath element pointing to Utils.groovy or Utils.class and you are done. You don't need to put it in .groovy/lib, for example the current directory is also automatically part of the classpath. If you don't want to use the classpath option on the command line you can also edit the groovy-starter.conf and add the path there.

bye blackdrag


--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Loading...