Expand my Community achievements bar.

Making AEM Groovy Without the Console | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Making AEM Groovy Without the Console by Juan Ayala

Abstract

I was recently doing some research on error handling in AEM. I came across this Sling documentation page. Pretty standard stuff except for the very last line.

Thus to overwrite the default error handler servlet provide a servlet or script for the default extension, for example /apps/sling/servlet/errorhandler/default.groovy.
A groovy script, in the context of Sling. Was the documentation wrong? Did they mean to write jsp instead of groovy? I got down to doing some research.


By the end of this post, you will be writing the following in Groovy

OSGi components i.e. servlets, services, and sling models.

Scripts that Sling can resolve and execute.

GString templates that Sling can resolve and render.

Background

For years I have associated Groovy with the AEM Groovy Console. If you Google "aem groovy script" you will get bombarded with the Groovy Console. Now try "sling groovy script". Two different things.


I found this document filed under "Old Stuff". As it instructs, you can deploy the groovy-all jar into Felix. It has a script engine GroovyScriptEngineFactory. Sling will use it to run groovy scripts. This is in fact what the AEM Groovy Console is doing. Check out their app/pom.xml file. There they are embedding groovy-all, version 2.4.15.


Sling also has support for other templating engines. You know the usual, JSP & HTL. It can also support Thymeleaf, FreeMaker, and Groovy GString.


So even though Sling supports Groovy scripts and templates, AEM does not ship with it. This is how Sling gets its groove back.


Deploying The Groovy 3.0 Runtime

As I mentioned, the Groovy Console ships with Groovy 2.4. That version has become 2.5 and 3.0. Version 4.0 is still under development. So 3.0.9 is the latest stable release as of January 2022.


Unfortunately, it is not as simple to deploy 3.0 as it was in 2.4. As of 2.5, there is no groovy-all bundle anymore. Only a groovy-all POM that has references to the sub-project bundles. We will need to deploy the Groovy core bundle, its fragments, and dependencies as needed. I am using an AEMaaCS archetype project for the following examples.

Read Full Blog

Making AEM Groovy Without the Console

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
1 Reply

Avatar

Community Advisor

@kautuk_sahni  I Read your blog - it looks a bit interesting- Just wanted to understand more on what difference /benefit sling groovy brings if I compare it with legacy groovy console method?