Groovy Integration with CQ5

27 / Feb / 2014 by ankit.gubrani 0 comments

Groovy being one of the fresh open source platforms in the market of Programming language, it is attracting Developer to use it because of its friendliness, easy codeabiltiy and many other features.

Tempted by many of such features, CQ developers are also inclining towards using Groovy in CQ environment. To the rescue of many such CQ developers, in this blogI am going to cover how to get integrate Groovy with CQ5 environment.

As Sling Framwork provides built-in support for groovy (https://issues.apache.org/jira/browse/SLING-315) , so it can be done simply by uploading the latest groovy jar to the Felix. But the limitation is, this jar only allows your CQ5 environment to compile the groovy script i.e groovy compiler in CQ5 environment.

PROBLEM

One of the few problems that arises while using Groovy with maven, you need to specify the Compiler Plugin which is to be used to compile the source of your project. Reason being, the default compiler, javac, cannot compile groovy classes. So you need to add the Groovy-eclipse-compiler in the plugin section of parent pom.xml as guided on .

Limitation of using Groovy-eclipse-compiler plugin directly with IntelliJ Idea , is that the bundle of the module using this plugin will not become active , it will remain in Installed state with some dependency issues.

SOLUTION

After checking a few plugins suggested by many of my CQ developers, I added the same Plugins that were used by cq-groovy-console (https://github.com/Citytechinc/cq-groovy-console/blob/develop/pom.xml)

To install the plugin/s, add the following code to the parent POM.It will allow the bundle to get installed in the Active State.

[js]<plugins>
<plugin>
                   <groupId>org.codehaus.groovy</groupId>
                   <artifactId>groovy-eclipse-compiler</artifactId>
                   <version>2.8.0-01</version>
                   <extensions>true</extensions>
                   <dependencies>
                       <dependency>
                           <groupId>org.codehaus.groovy</groupId>
                           <artifactId>groovy-eclipse-batch</artifactId>
                           <version>2.1.8-01</version>
                       </dependency>
                   </dependencies>
               </plugin>
               <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.1</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.8.0-01</version>
                       </dependency>
                       <dependency>
                           <groupId>org.codehaus.groovy</groupId>
                           <artifactId>groovy-eclipse-batch</artifactId>
                           <version>2.1.8-01</version>
                       </dependency>
                   </dependencies>
               </plugin>
</plugins>[/js]

Above code works good with JDK 1.7 as well as with the legacy version. You can also use the 2.8.0-01 version of groovy-eclipse-compiler and 2.1.8-01 version of groovy-eclipse-batch as suggested on  http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven .

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *