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

Recently I was researching error handling in AEM . I found this Sling documentation page. Pretty standard stuff except for the 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 want to write jsp instead of groovy ? I did some research.


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

OSGi components, that is, servlets, services, and Sling models.

Scripts that Sling can resolve and execute.

GString templates that Sling can resolve and render.

The Background

For years I have associated Groovy with the AEM Groovy Console . If you google " aem groovy script ", you will be bombarded with the Groovy Console. Now try " sling groovy script ". They are two different things.


I found this document filed under "Old Stuff". As indicated, you can deploy the groovy-all jar to Felix. It has a GroovyScriptEngineFactory scripting engine . Sling will use it to run Groovy scripts. In fact, this is what the AEM Groovy Console is doing. Take a look at its app/pom.xml file . There they are embedding groovy-all, version 2.4.15 .


Sling also has support for other template engines . You know the usual, JSP and HTL. It is also compatible with Thymeleaf , FreeMaker and Groovy GString .


So while Sling supports Groovy scripts and templates, AEM does not include it. This is how we can put it back.


Deploying Groovy 3.0

As I mentioned, 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 version as of January 2022.


Unfortunately, it's not as simple to deploy 3.0 as it was in 2.4. As of 2.5 there is no single package. Just a POM with references to the subproject packages. We will need to deploy the main Groovy package, its snippets and dependencies as needed. I am using an AEMaaCS archetype project for the next few examples.

Read Full Blog

Making AEM Groovy Without The Console

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
0 Replies