Use Groovy Script in AEM

The AEM Groovy Console provides an interface for running Groovy scripts in Adobe Experience Manager. Scripts can be created to manipulate content in the JCR, call OSGi services, or execute arbitrary code using the AEM, Sling, or JCR APIs without any change in the core code of the AEM project.

Introduction:

Apache Groovy is an Object-oriented programming language used for Java platforms like AEM. It is almost like a super version of Java which offers all of Java’s enterprise capabilities. Learning/understanding groovy comes naturally to a java developer so the learning curve to learn groovy for a java developer is very minimal and easy enough. It can manipulate without much hustle, easy, fast, and simple to use provided UI.

Requirements:

Installation:

There are 2 ways to install Groovy Console

1. Install a groovy console package in AEM through the package

Example:

  • OR Go to the folder from the command prompt and execute the below command on the project root folder.
    mvn clean install
  • After that go to http://localhost:4502/crx/packmgr
  • Click on “Upload Package” and select the “aem-groovy-console-all-17.0.0.zip” package from ....aem-groovy-console-17.0.0\all\target folder
ZIP file to be installed
Installation of the ZIP file

2. By updating pom.xml

  • Update the pom.xml under the “all” folder
  • Add embedded under embeddeds
<embedded>
    <groupId>org.cid15.aem.groovy.console</groupId>
    <artifactId>aem-groovy-console-all</artifactId>
    <type>zip</type>
    <target>/apps/gap-cloud-packages/groovyconsole/install</target>
</embedded>

  • Add dependency under dependencies (Here 17.0.0 is the version you can update it with the required version)
<dependency>
    <groupId>org.cid15.aem.groovy.console</groupId>
    <artifactId>aem-groovy-console-all</artifactId>
    <version>17.0.0</version>
    <type>zip</type>
</dependency>

Know about the Groovy GUI:

Go to http://localhost:4502/apps/groovyconsole.html and you can see the Groovy GUI like this

  1. Themes: once can set his preferred theme to style the groovy console and coding area in it. By default, you get to see the one you see in the picture.
  2. Run Script button: this button executes the currently written code in the coding area.
  3. New button: this button will clear out the coding area for you to start with a new groovy script.
    Be careful to save your current script before pressing this button.
  4. Open button: this will open a box showing all the available saved scripts to load in the current coding area to use. This will also show the sample scripts provided by the groovy console under the samples folder, which you may prefer to know how to perform a few basic operations in AEM using groovy.
    Be careful to save your current script before pressing this button.
  5. Save button: this will show a popup box to fill in a filename to save your current script with that filename. If you are editing an existing script, then it will still open this box with the existing script’s filename in it. If you wish to save it in a different file, then change the name and save or else leave the name as is and save.
  6. Service or adapter name search box: this is the very helpful section where you may start typing the name of any OSGI service or adapter name to look up and this will show you that service or adapter if available. If you choose that service or adapter, then a piece of script/statement would be inserted at the end of your current script which you may cut and place anywhere else as well you get access to the instance of that service or adapter.
  7. Coding Area: this section just under the Data section is the area where you will write your script.

There are a few more sections in the groovy console. Some of the most important of them all are Bindings, Imports, and Methods. These sections tell you about all the implicit binding objects, imports, and methods available for you to directly use inside your groovy script.

Batch Script Execution:

Saved scripts can be remotely executed by sending a POST request to the console servlet with either the scriptPath or scriptPaths query parameter.

Single Script

curl -d "scriptPath=/var/groovyconsole/scripts/samples/JcrSearch.groovy" -X POST -u admin:admin http://localhost:4502/bin/groovyconsole/post.json

Multiple Scripts

curl -d "scriptPaths=/var/groovyconsole/scripts/samples/JcrSearch.groovy&scriptPaths=/var/groovyconsole/scripts/samples/FulltextQuery.groovy" -X POST -u admin:admin http://localhost:4502/bin/groovyconsole/post.json

Examples:

There are a few samples already present in the console.
Go to Open>Scripts>samples
You can select any sample and try executing your first groovy script.

Happy Coding!

About Sagor Chowdhuri 17 Articles
AEM Developer

4 Comments

  1. Hi Sagor, I logined as Admin and getting “no permission to execute the script” everytime. even the service user being used has all administrator permissin. please advise.

Leave a Reply

Your email address will not be published.


*