Magnolia Groovy module adds Groovy capabilities to Magnolia. Groovy is a popular dynamic language for the JVM. To know more about it, please visit the Groovy official website which has plenty of tutorials and excellent documentation both for beginners and advanced users of the language.
The module provides a web based Unix-like console where you can access contents in Magnolia repositories in a "groovish" way, a
scripts repository where you can store your scripts and the ability to plugin Groovy classes into Magnolia at runtime, without the need for deploying them and restarting the servlet container. All these tools make for a more agile approach to coding and maintaining Magnolia based websites.
Magnolia 5.7 updates
magnolia-module-groovy to version 2.7, which ships the latest stable version of Apache Groovy 2.5.
Earlier versions of Groovy were delivered in a single jar file called
groovy-all but this library does not exist anymore.
magnolia-module-groovy-2.7 now ships with only the minimum set of libraries:
Some of your custom Groovy scripts may require additional libraries.
Maven is the easiest way to install the module. Add the following to your bundle:
Note the changes in
artifactId since the 2.6 release.
<dependency> <groupId>info.magnolia.groovy</groupId> <artifactId>magnolia-groovy</artifactId> <version>2.7.1</version> </dependency>
Pre-built jars are also available for download. See Installing a module for help.
We have been gradually removing the old Content API from our modules since Magnolia 5.6. If you have custom code relying on classes from the old groovy module then you must do one of two things:
If you have Groovy scripts or classes which rely on the Content API, then you will need the compatibility module to run them. You are encouraged to update to Node API.
- Update your code for the new version of the groovy module.
- Or you can use the
magnolia-groovy-compatibilitymodule together with the
Add the following snippet to you pom file:
<dependency> <groupId>info.magnolia.groovy</groupId> <artifactId>magnolia-groovy-compatibility</artifactId> <version>2.7.1</version> </dependency>
Add these jar files to the
WEB-INF/lib folder of your webapp:
Repository data navigation simplified
Thanks to the . (dot) notation and other handy shortcuts such as
.parent you can read any content node and property, change their values, create nodes and delete them, all with clean, concise syntax. The module is shipped with some preinstalled scripts that demonstrate most of these features. As an example, here is how to navigate to and print the node data named
One noticeable thing about the above snippet is that it is all you need to write: no imports, no need to catch exceptions, etc. Compare it to the Java code that achieves the same result.
And, most importantly, with Groovy you do not need to deploy your class and restart the server. As an additional bonus, when using the Groovy Unix-like shell which comes with the module, when you navigate a repository, by calling the
print() method on a node its children and properties are shown in the output, giving you an overview of the data structure. The screenshot below illustrates this.
print() method differs from previous versions where the data structure was displayed automatically on pressing the Enter key. However, in many cases that caused the output to be unnecessarily verbose. What is printed now by default is simply the node path.
Being able to navigate a repository data like this can come in handy, for instance, when writing and testing your own scripts or trying things out. Or when you need to use the because the Magnolia UI, for whatever reason, is not available.
node here is basically an instance of a JCR Node coated in a special "groovysh" wrapper, you to call any of the Node interface methods and take advantage of the Groovy goodies at the same time.
If you want to print out all node properties but jcr ones, you can do:
?) followed by Enter to view the built-in help (including keyboard shortcuts) for the Groovy shell.
Autocompletion & Keyboard Shortcuts
Autocompletion in the Groovy console:
- Start writing a variable name and press Tab to autocomplete available variables in script context.
- Press Tab Tab to discover available variables in script context.
- Type . (dot) after a context variable and press Tab to autocomplete method names.
- Type . (dot) after a context variable and press Tab Tab to discover method names.
ctx context object is always available. In Groovy it represents
MgnlGroovyConsoleContext, a special instance of Magnolia's
Multiline code in the console
To enter multiline code in the Groovy console, press Shift + Enter at the end of each line. This enters the multiline mode. Once you've entered a block of text, press Enter at the end of the command to run it. This is useful when writing Groovy statements more complex than a simple one-liner.
|TAB||Trigger autocompletion of variables names and object methods|
|TAB TAB||Discover all available suggestions, if any, for the current identifier|
|SHIFT + ENTER||Activate multi-line mode in the console. To run the code, hit the ENTER key|
|↑ or CTRL + P||Show previous command from history|
|↓ or CTRL + N||Show next command from history|
|CTRL + R||Begin reverse search through history. Type characters to find matching commands.|
|CTRL + G||Cancel reverse search|
|← or CTRL + B||Move cursor one character left|
|→ or CTRL + F||Move cursor one character right|
|HOME or CTRL + A||Move to beginning of line|
|END or CTRL + E||Move to end of line|
Create and update properties
It is also possible to assign values to properties or create new ones.
This will assign the values on the right hand side to the properties on left hand side. Should those not exist, they will be automatically created. Moreover, the correct type will be detected based on the value assigned (
All the above assignments will be in-memory only unless explicitly persisted via a call to
save() on the current JCR session.
Use Groovy classes instead of Java classes
This feature allows you to virtually replace every Magnolia Java class with a Groovy one. Although not a major issue for most tasks, due to its dynamic nature Groovy is slower than Java on the first run. This because groovy code must be compiled into byte-code before it can be ran on the JVM. Nevertheless, replacing classes can come in handy in situations where you cannot deploy and restart the server, but need to quickly fix or add a piece of logic.
The Groovy module ships with a sample class
sample.commands.GroovyMailCommand that sends an email. Here is how you could use it to create a new scheduled job on-the-fly using a Groovy command and the Scheduler module.
GroovyMailCommand is accessible in Dev > Groovy
mailhost with your SMTP server address.
Create a command configuration in the Configuration app as shown below. We added it to the Mail module in
/modules/mail/commands/default, but you can add it to your own module or another of your choice. Specify the fully qualified name of the Groovy class (matching the path in the repository where it is saved
samples.commands) as if it were a plain Java class (which it actually is).
Create a scheduled job configuration referring to the command in the Configuration app >
/modules/scheduler/config/jobs. The parameters will be used at runtime by the Groovy script.
|0 0 * * * *|
|send mail every hour|
to email addresses with actual addresses.
You can use this command to replace a similar Java command at runtime. Or you can edit it and have it compiled and replaced on-the-fly (a kind of class hot deploy). This relies on the Magnolia observation mechanism. Every time the value of the
class node data changes the Groovy class will be recompiled.
MgnlGroovyRescueApp is a special Vaadin app that can be used to bypass the Magnolia filter chain. This is useful when you need to perform rescue operations on a corrupted Magnolia instance or when the Magnolia UI is not loading. To enable the servlet you must explicitly comment out the Magnolia filter chain in the
web.xml file and register the Groovy Rescue App.
All operations performed in the Groovy Rescue App are executed in system context, meaning no security restrictions are enforced. This might expose your data to risk of irreversible damages if you are not aware of what you are doing. In other words, use it at your own risk.
Register the Groovy Rescue App
- Stop Magnolia
/<CATALINA_HOME>/webapps/<contextPath>/WEB-INF/web.xmlin a text editor.
Comment out the
Add the following lines to web.xml in order to register the Groovy Rescue App:
<param-value>info.magnolia.widgetset.MagnoliaWidgetSet</param-value>- when using Magnolia CE or EE Standard.
<param-value>info.magnolia.widgetset.MagnoliaProWidgetSet</param-value>- when using Magnolia EE Professional.
- Save the web.xml.
- Start Magnolia.
- Open a Web browser and access the Groovy Rescue App at
Make the required changes
Use Groovy commands to navigate to the data you want to change.
In the following example we delete an erroneous configuration node
Assign the parent node content to
Get and remove the child node
Save the session.
Deregister Groovy Rescue App
- In web.xml, remove the servlet registration lines you added above.
- Remove comments from the filter and filter-mapping sections.
- Save web.xml.
Tomcat should notice that web.xml changed and read the changes automatically. If this does not happen, stop Magnolia, edit
web.xml, and start Magnolia again. Then try to access Magnolia.
Remote client connector
The Groovy module also allows a remote Java client application to run Groovy scripts against a Magnolia server. For more information see Groovy Remote Client Connection .