April 9, 2020
Estimated Post Reading Time ~

Register Groovy class as Component/Service in CQ

Being a CQ5 developer, I have to register components and services almost every day. Knowing how powerful Groovy is, I wanted to replace all Java code with Groovy. The first step towards it was being able to compile Groovy.

Although I was facing the following problem even after this:
– I was not able to register the component as a service or component. In fact, none of the SCR annotations seemed to work with Groovy. I had the following maven-scr plugin configuration.

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-descriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>


Tried a lot of things for this and this is how I got it to work(thanks to suggestions from a group):

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<configuration>
<scanClasses>true</scanClasses>
</configuration>
<executions>
<execution>
<id>generate-scr-descriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>


By default, the maven-scr plugin compiles only Java Classes. To make it aware of Groovy classes also, a configuration “scanClasses” has to be set to true.

Updated:
There are 2 more configuration changes that are needed:
1) I had to change the version of maven-scr-plugin from 1.7.4 to 1.17.0
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
    <version>1.17.0</version>
</plugin>


2) Change version of Felix scr annotation to 1.9.8
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.scr.annotations</artifactId>
    <version>1.9.8</version>
    <scope>provided</scope>
</dependency>


Adding this code worked like a charm.
Source: https://www.tothenew.com/blog/register-groovy-class-as-componentservice-in-cq/


By aem4beginner

No comments:

Post a Comment

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