Message-ID: <30886788.19722.1406762585767.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_19721_1434383430.1406762585767" ------=_Part_19721_1434383430.1406762585767 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Groovy is a great language just on its own in various scenarios.= It is also extremely useful in mixed Groovy/Java environments. With this i= n mind, Groovy has been designed to be very lightweight and easy to embed i= nto any Java application system.
There are three main approaches for natively integrating Groovy with Jav= a. Each of these is discussed in more detail below.
Alternatively, you can use the Bean Scripting Framework to embed any scripting language in= to your Java code, giving you other language options if you needed them (th= ough we can't imagine why ). Using BSF allows you to be more loosely c= oupled to your scripting language; however, native integration is more ligh= t weight and offers closer integration.
It is often useful to make your groovy scripts extend a base class of yo= ur choosing so that the script can access common methods. This can be achie= ved by setting the script base class property on the compile configuration = and passing this new compiler configuration to the shell.
You can use the GroovyClassL= oader to load classes dynamically into a Java program and execute them = (or use them) directly. The following Java code shows an example:
If you have an interface you wish to use which you implement in the Groo= vy script you can use it as follows:
This works fine if the Groovy class implements the inferface MyInterface= . myObject can from then on be used as every other Java object implementing= MyInterface.
One thing to remember is that the parseClass will try to create an objec= t from your String fileName. Another way to do the gcl.parseClass is:=
Note that all of the error handling has been removed -- you won't be abl= e to do this in a java class. I actually use the Interface invocation for G= roovy inside of a Utility Class.
The most complete solution for people who want to embed groovy scripts i= nto their servers and have them reloaded on modification is the GroovyScrip= tEngine. You initialize the GroovyScriptEngine with a set of CLASSPATH like= roots that can be URLs or directory names. You can then execute any Groovy= script within those roots. The GSE will also track dependencies between sc= ripts so that if any dependent script is modified the whole tree will be re= compiled and reloaded.
Additionally, each time you run a script you can pass in a Binding that = contains properties that the script can access. Any properties set in the s= cript will also be available in that binding after the script has run. Here= is a simple example:
This will print "Hello, world!".
An interactive Groovy interpreter can be embedded in a running applicati= on for experimentation and testing. For a tutorial on how to do, including = example code, see the cookbook example Embedding a Groovy Console in a Java Server Applic= ation.
An example for the integration of Groovy as scripting language into an a= pplication can be found at Integrating Groovy in an application - a= success story
You can use the groovy-all-x.y.z.jar from the GROOVY_HOME/embeddable dir= ectory of your Groovy installation, or find the according artifact from you= r Gradle / Maven / Ant+Ivy build. Please also have a look at the download section to see how to get Groovy.= p>