How to use groovy in AEM (including in sightly components and templates)

Java is great but, Groovy is awesome.

Why Groovy on AEM ?

Short answer: I feel that Java (syntax) was not designed for web development and this is where Groovy comes to the rescue by allowing you still have the best of both worlds – the goodness of java libraries tied with dynamic script like syntax for fast paced web development.

Long: We are not really going to dive to this extent here. There are heaps of comparison already on the web.

Lets get started.

  1. In this example I'll be using the Adobe AEM Maven Archetype. Head to over to https://github.com/Adobe-Marketing-Cloud/aem-project-archetype and follow the instructions to generate your AEM maven project.

  2. The groovy-all jar file dependency:
    Download the groovy-all-2.4.3.jar and place it under your jcr content jcr_root/apps/{sitename}/install

  3. Modify your parent project pom.xml file as such:
    a. Change the maven-compiler-plugin to use the groovy compiler.

Replace:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>  

With:

<plugin>  
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <compilerId>groovy-eclipse-compiler</compilerId>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>utf-8</encoding>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>2.9.2-01</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>2.4.3-01</version>
        </dependency>
    </dependencies>
</plugin>  
  • Add the groovy dependency to your parent and core module pom.xml file:
<dependency>  
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.3</version>
</dependency>  
  • This this be enough to start using groovy by simply placing your groovy files in your core module under src/main/groovy . However in this example I would like to extend the WCMUsePojo class in groovy, so I'll make use of the uber jar. To add it to your maven project simply add the dependency as such:
<dependency>  
    <groupId>com.adobe.aem</groupId>
    <artifactId>uber-jar</artifactId>
    <version>6.1.0</version>
    <classifier>obfuscated-apis</classifier>
    <scope>provided</scope>
</dependency>  

This is it. All groovy files will be trranspiled to java class before being deployed to AEM.

You may also find a working example on github: https://github.com/puttyman/aem-groovy-example

Sid Dahari

I'm a Full stack web developer passionate about programming languages, web technologies and operating systems with vocation for open source & clean code. I'm also a big fan of kitesurfing.

St Kilda, Melbourne, Australia
comments powered by Disqus