Creating a maven project in CQ5

By | 05:04 Leave a Comment
CRXDE lite is a nice little IDE for creating UI stuff like templates and dialogs. The browser based interface cannot match up to IDE's like Eclipse. Also writing Java code (Bundles) in crxde lite  is a major pain. Adobe has a solution for this , they have defined a Maven archetype that creates a maven project independent of IDE's. With maven plugins for Apache Sling and Apache Felix writing code is a lot simpler.

The sample maven command to create a cq5 project is

mvn archetype:generate -DarchetypeRepository=http://repo.adobe.com/nexus/content/groups/public/  -DarchetypeGroupId=com.day.jcr.vault -DarchetypeArtifactId=multimodule-content-package-archetype -DarchetypeVersion=1.0.2 -DgroupId=cq5tutorials  -DartifactId=myCqMavenproject -Dversion=1.0-SNAPSHOT -Dpackage=com.blogspot.cq5tutorials  -DappsFolderName=myCqMavenproject  -DartifactName="my Cq Maven project"   -DcqVersion="5.6.1"  -DpackageGroup="com.blogspot.cq5tutorials"



Run this MVN command and import it as a maven project into eclipse.[ If you get Mojo failure exception when running the command, try deleting the jar mentioned in error message from M2 repository and re running the command]. After the command is successfully executed import the project into Eclipse. You'll see three folders viz, the main folder (same name as your artifactId), content folder and the bundle folder.

The project is divided into two modules

1) Content : This will have all the UI elements like templates, template renders, components , client libraries and so on. The apps folder of your project will be in this module. You can also include etc and other folders. The content folder maps with jcr:root of the repository hence you can use vault tool to import and export data from eclipse to your repository. Since the repository is made of nodes this is translated into xml files and folders to convert them to native file system in eclipse.

2)Bundle: Since Cq5 uses OSGI architecture all your java classes will be written as OSGI components and services , these are then packaged in a bundle which export the functionality to be used in the UI elements. All your Servlets, filters , schedulers, taglibs will be written inside this folder.



The project will have three POM's the parent POM in the main folder, and a POM each for content and bundle.

The main folder will have both modules in it. The other two folders separated copies of content and bundle. Any changes you make in the content or bundle folder are automatically reflected in the main folder.

Content module has bundle module added as dependency. So when you build the main folder the bundle jar is included into content jar. Install maven goal converts the apps and other folders into nodes and stores it under jcr:root of your cq instance.Bundle is uploaded into felix console.

The paths and passwords to your author and publish are configured in the <properties> section of parent pom.

The pom declares few plugins , maven-scr-plugin is one of them. This a very important plugin that is responsible for converting annotated classes into OSGI components and services (uses felix scr annotations library) and generates two xml files that can be found in target folder of Bundle.

If you want to send select files from eclipse to the repository without building the project you can use Vaulteclipse plugin. The settings for this can be configured in META-INF/vault folder of Content.

The paths you want to be able to exported to eclipse must be updated in filter.xml file. To add a path add <filter> tag inside <workspaceFilter> tag.

Ex: <filter root="/etc/designs/myCqProject">
      </filter>

In case you want to exclude certain specific paths add <exclude> tag the corresponding filter tag. You can use regular expression in the patterns.

References: http://docs.adobe.com/docs/en/cq/aem-how-tos/development/how-to-build-aem-projects-using-apache-maven.html

NOTE: If you get No compiler found error when building , add your JDK as compiler in maven run configurations.

0 comments:

Post a Comment