#aem #Groovy
Adobe Experience Manager (AEM): Groovy Scripting
Apache Groovy is an Object-oriented programming language used for the Java platform. It can be used as a scripting language for the Java platform.
Some of the benefits of Groovy scripting are
• Groovy source code gets compiled into Java Byte code so it can run on any platform that has JRE is installed.
• the syntax is simple and easy.
• Dynamic nature - generating classes transparently at runtime when needed
• Reduced syntax
With groovy, one can manipulate content in the JCR, call OSGi services, or execute arbitrary code using the AEM, Sling, or JCR APIs
There are different options in AEM to run the Groovy Scripts
Let’s talk about 3 options here
The first option is by enabling the Apache Felix Script Console Plugin
The second option is by enabling ICFNext aem-groovy-console extension.
Based on these two options, we should be able to enable a console in AEM to execute the Groovy scripts - console provides an interface for running Groovy scripts in the AEM container.
Some of the useful scenarios for the groovy console are
• Manipulate content in the JCR, call OSGi services, or execute arbitrary code using the AEM, Sling, or JCR APIs
• Reporting - List of unused Assets in DAM and Components, Asset details, content details, etc.
• Data Load - you should able to load data from different sources e.g. excel this will help to perform content migration from external systems
• Activate/Deactivate resources
The third option is through oak-run tool
Apache Felix Script Console Plug in to execute the groovy scripts in AEM
Script Console is a Felix web console plugin that allows the evaluation of scripts within the OSGi container.
Support evaluation of script in any e.g. Groovy, JavaScript, Ruby, etc. You would need to ensure that the relevant language bundle is deployed.
Install the script console plugin - @rg.apache.felix.webconsole.plugins.scriptconsole-1.0.2.jar
Install the groovy bundle - @org/codehaus/groovy/groovy-all/2.4.9/groovy-all-2.4.9.jar
The script exposes some of the pre-defined variables
request - Current HttpServletRequest instance
response - Current HttpServletResponse instance
reader - Direct access to the Reader of the request - same as request.getReader(). Use it for reading the data of an HTTP request body.
out - Direct access to the PrintWriter of the response - same as response.getWriter(). Use it for writing to the HTTP response body.
osgi - Provides convenience methods for scripts, mainly osgi.getService(foo.bar.Service.class) to retrieve OSGi services available in OSGi Container (Class notation depending on scripting language).
bundleContext - OSGi BundleContext instance for the script console plugin bundle. Can be used to access the OSGi runtime
The console is accessible through - @console/sc
Some of the benefits of using the console plugin, this is a standard plug in from Apache Felix, also enables the option to execute multiple language scripts.
But this won’t provide some advanced features like notification, Scheduling, also it exposes limited binding variables that can be used directly – more script should be written to achieve the required functionalities.
Groovy console extension from ICFNext
ICFNext has developed a Groovy Console for AEM, this enables a console to execute the script in AEM also provides some advanced features like notification, scheduling, etc. also exposes different binding variables to simplify the scripting. Also, the framework can be customized as required.
the package - @/aem-groovy-console, @/aem-groovy-console/releases
oak-run tool to execute the groovy script
Oak-run Console is based on Groovy Shell and hence one can use all Groovy constructs
java -jar oak-run-1.8.0.jar console C:\Albin\SW\AEM\6.5\distribution\Author\crx-quickstart\repository\segmentstore --read-write
:load C:\Albin\blogData\groovy\setProperty.groovy
setProperty(session, "/content/we-retail/us/en/jcr:content", "propName11", "propValue222", false)
Sample Scripts: @um-repo/youttubedata/tree/master/aem.groovy.samples