April 9, 2020
Estimated Post Reading Time ~

Groovy Integration with CQ5

Groovy is 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 such features, CQ developers are also inclining towards using Groovy in the CQ environment. To the rescue of many such CQ developers, in this blog, I am going to cover how to get integrate Groovy with the CQ5 environment.
Sling Framework 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 the CQ5 environment.

PROBLEM
One of the few problems that arise while using Groovy with maven, you need to specify the Compiler Plugin which is to be used to compile the source of your project. The 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.
<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>

The above code works well 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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.