May 19, 2020
Estimated Post Reading Time ~

Creating a maven project in CQ5

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= aem4beginner -DartifactId=myCqMavenproject -Dversion=1.0-SNAPSHOT -Dpackage=com.blogspot. aem4beginner -DappsFolderName=myCqMavenproject -DartifactName="my Cq Maven project" -DcqVersion="5.6.1" -DpackageGroup="com.blogspot.aem4beginner"

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 an error message from the 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 the 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 the native file systems 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 exports 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.

The content module has a bundled module added as a dependency. So when you build the main folder the bundle jar is included in the content jar. Install maven goal converts the apps and other folders into nodes and stores it under jcr:root of your cq instance. The 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 the 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 the META-INF/vault folder of Content.

The paths you want to be able to export to eclipse must be updated in the 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 expressions 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 a compiler in maven run configurations.


By aem4beginner

No comments:

Post a Comment

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