Register Groovy class as Component/Service in CQ

30 / Jun / 2014 by Vivek Sachdeva 0 comments

Being a CQ5 developer, I have to register components and services almost everyday. Knowing how powerful Groovy is, I wanted to replace all Java code with Groovy. First step towards it was being able to compile Groovy. I got it to work with the help of this blog.

Although I was facing following problem even after this:

– I was not able to register component as a service or component. Infact none of the SCR annotations seemed to work with Groovy. I had following maven-scr plugin configration.


    <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 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 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 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.

Hope this helps CQ5 developers who love Groovy(like me. :))…

Vivek Sachdeva

CQ5 developer

FOUND THIS USEFUL? SHARE IT

Leave a comment -