For those that develop CQ5 components, most people will be familiar with CRXDE or Eclipse to develop CQ5 components and OSGi bundles. The usual unit of deployment is a CRX package either created using the Package Manager or some build script offline using the vault format. The CQ5 package will contain inside it one or more OSGi bundles which are then picked up by the JCR Installer and deploy it to Apache Felix.
There is one other way of deploying code to CQ5 and that is using OSGi bundles with the JCR content bundled as resources inside the bundle itself. This mechanism is used by the applications developed on Apache Sling. Since CQ5 components are essentially Sling applications, I thought I’d describe how to go about doing offline development and deployment of an example CQ5 component using maven.
Let’s start with the pre-requisites:
- A local CQ5 instance
- A maven repository with the OSGi bundles OR Install the Archiva Servlet (link) on CQ5
Let’s assume you have these ready.
The first thing you need is a project structure and a pom file.
In my app, I created a project structure as follows:
This source structure will produce the jar file which includes the java classes (contained in src/main/java) and the JCR nodes, properties, and files that constitute the CQ5 components (contained in src/main/resources/SLING-CONTENT). You’ll notice the CQ5 folders for apps and etc/designs/ are there. You can add any other folders there too.
First a look at the pom.xml
Most of this is your usual maven configuration of the dependent libraries. If we concentrate on the plugins section, there are three plugins:
maven-scr-plugin is used to use the scr annotations in the java classes to register or wire the OSGi services.
maven-bundle-plugin uses BND to produce the OSGi bundle with the correct MANIFEST.MF contents. Note the entries in the configuration element. These include the Export-Package, Import-Package, Private-Package headers found in your typical OSGi bundle. The extra element here is the Sling-Initial-Content element which includes declarations of what parts of the bundle should be used to initialise content in the JCR (CRX in our case).
If we look closely, we have several entries delimited by commas:
Each entry contains the location in the bundle, a flag to overwrite in the repository, a flag to install the content on uninstall of the bundle, and the path in the repository that the contents should be written.
The actual contents of SLING-CONTENT can be specified according to the Sling Content Loader mechanism (more detail here)
Each folder represents a folder in CRX. Each node and its children and properties can be defined in a json file.
In the Sling-Initial-Content above, you will notice two entries for /etc/designs/my-sling-app. One for the the /etc/designs/my-sling-app node itself (which is of type cq:Page) and another for the its child (jcr:content) and child nodes and properties of jcr:content.
The third plugin (maven-sling-plugin) is used to install the bundle to Apache Felix in the install phase of mvn execution.
That’s it. Run mvn clean install from commandline and this will be installed on your local CQ5 instance and overwrite your OSGi bundle, /apps/technomaven, and /etc/designs/my-sling-app folders in CRX.
The full package of the code above can be found here. Enjoy.
– Sarwar Bhuiyan