Contents
Contents
This document describes the process of setting up a local development environment for a simple CQ5 project with Eclipse. It then describes how to integrate logic into the project through Java coding and JSP scripting. Lastly, it points to open source software to enable collaborative and automated developing.
The setup described here is an alternative among others and may vary from project to project.
The local development environment involves:
A CQ5 installation that will act as your local environment.
CRX Explorer within the CQ5 instance to create and edit nodes and properties within the CRX repository.
FileVault (VLT), a Day developed utility that maps the CRX repository to your file system.
Eclipse to edit the project source on your local file system.
Apache Maven to run local snapshot builds.
This section describes the creation of a simple project structure in CQ5:
Install CQ5 on your machine. Please refer to the section called “Installing a CQ WCM instance - Generic procedure” for the detailed procedure. In the current context, CQ5 runs locally on port 4502.
If already installed then ensure it is running and connect.
In the CRX Explorer, create the project structure:
Under the
/appsfolder, create the nt:foldermyApp.Under the
myAppfolder, create the nt:foldercomponents.Under the
myAppfolder, create the nt:foldertemplates.Under the
myAppfolder, create the nt:folderinstall.
In your browser, navigate to the
Miscellaneoustab. Underdesigns, create the design page of your application:Title:My Application Design Page.Name:myApp.Template:Design Page Template.
FileVault (VLT) is a tool developed by Day that maps the content of a CRX instance to your file system. The VLT tool has similar functionalities to those of an SVN client, providing normal check in, check out and management operations, as well as configuration options for flexible representation of the project content.
To install VLT, follow the steps:
In your file system, go to
<cq-installation-dir>/crx-quickstart/opt/filevault. The build is available in both tgz and zip formats.Extract the archive.
Add
<cq-installation-dir>/crx-quickstart/opt/filevault/vault-cli-<version>/binto your environment PATH so that the command filesvltorvlt.batare accessed as appropriate. For example,<cq-installation-dir>/crx-quickstart/opt/filevault/vault-cli-1.1.2/binOpen a command line shell and execute vlt --help. Make sure it displays the following help screen:

Eclipse is open source software used to edit the project source locally on your file system. Apache Maven is also open source software, used to run local snapshot builds: it compiles Java code and stores the compiled code in a jar file.
In this section, you will install Eclipse and a Maven plugin which embeds the Maven functionality within Eclipse:
Download Eclipse - select the
Eclipse IDE for Java EE Developersoption.Install Eclipse: extract from the downloaded zip file to your destination directory.
Start Eclipse:
Navigate to the directory into which you extracted the contents of the Eclipse installation zip file. For example
C:\Program Files\Eclipse\.Double-click on
eclipse.exe(oreclipse.app) to start Eclipse.
Create a new
workspacefor your project and name itmyApp.Install the Maven plugin (m2) from Sonatype. Disable Maven SCM handler for Subclipse (Optional) and Maven Integration for AJDT (Optional).
After installation it is recommended to restart Eclipse.
In this section, you will create 2 Maven projects:
one called UI (after User Interface) which contains the CQ5 project structure with the JSP scripts.
the other called Core which contains the Java code (source and compiled). The compiled code is stored in a jar file.
The advantage of such a structure is that it adds modularity and autonomy to the logic your application because each jar file (bundle) can be managed separately.
To create the UI Maven project, follow the steps:
In Eclipse open the Workbench.
Create the UI Maven project:
In the Menu bar, click
File, selectNew, thenOther....In the dialog, expand the
Mavenfolder, selectMaven Projectand clickNext.Check the
Create a simple projectbox and theUse default Workspace locationsbox, then clickNext.Define the Maven project:
Group Id:com.day.cq5.myappArtifact Id:uiName:CQ5 MyApp UIDescription:This is the UI module
Click
Finish.
Set the Java Compiler to version 1.5:
Right-click the
uiproject, selectProperties.Select
Java Compilerand set following properties to 1.5:Compiler compliance levelGenerated .class files compatibilitySource compatibility
Click
OK.In the dialog window, click
Yes.
Create the
filter.xmlfile which defines the content that will be exported by VLT:In Eclipse, navigate to
ui/scr/mainand create thecontentfolder.Under
content, create theMETA-INFfolder.Under
META-INF, create thevaultfolder.Under
vault, create thefilter.xmlfile.In
filter.xml, copy the following code tofilter.xml:<?xml version="1.0" encoding="UTF-8"?> <!-- Defines which repository items are generally included --> <workspaceFilter vesion="1.0"> <filter root="/apps/myApp" /> <filter root="/etc/designs/myApp" /> </workspaceFilter>Save the changes.
Check out the CQ5 content into your ui project with VLT:
From the system command line, navigate to the directory holding your Eclipse workspace
<eclipse>/<workspace>/myApp/ui/src/main/content.Execute the command: vlt --credentials admin:admin co http://localhost:4502/crx
This command creates the folder
jcr_rootunder<eclipse>/<workspace>/myApp/ui/src/main/content. This maps to the CRX root (/). Underjcr_rootthe following files and folders are created, as defined infilter.xml:apps/myAppetc/designs/myApp
It also creates two files,
config.xmlandsettings.xmlin<eclipse>/<workspace>/myApp/ui/src/main/content/META-INF/vault. These are used by VLT.
To enable Eclipse to map the file paths used in the JSP scripts, create a link to the
appsfolder underui:Right-click
ui, selectNew, thenFolder.In the dialog window, click
Advancedand check theLink to folder in the file systembox.Click
Browse, then specify<eclipse>/<workspace>/myApp/ui/src/main/content/jcr_root/apps.Click
OK.Click
Finish.
To enable Eclipse to identify the Java classes, methods and objects used in the JSP scripts, export the needed Java libraries from the CQ5 server to your file system and reference them in the ui project.
In this example, you will reference the following libraries:
libs/cq/installstored in the CQ5 serverlibs/sling/installstored in the CQ5 serverlibs/wcm/installstored in the CQ5 server<cq-installation-dir>/crx-quickstart/server/lib/containerstored in your file system
Proceed as follows:
In your file system, create a CQ5 libraries folder called
cq5libs. This folder can be created anywhere.Under
cq5libs, create the folders:cq,slingandwcm.From the system command line go to
.../cq5libs/cqand execute vlt co http://localhost:4502/crx /libs/cq/install . to export the libraries stored under/libs/cq/installfrom the CQ5 server.From the system command line go to
.../cq5libs/slingand execute vlt co http://localhost:4502/crx /libs/sling/install . to export the libraries stored under/libs/sling/installfrom the CQ5 server.From the system command line go to
.../cq5libs/wcmand execute vlt co http://localhost:4502/crx /libs/wcm/install . to export the libraries stored under/libs/wcm/installfrom the CQ5 server.In Eclipse, right-click the
uiproject, selectBuild Path, thenConfigure Build Path. In the dialog select theLibrariestab.Click
Add External JARS..., navigate to.../cq5libs/cq/jcr_root, select all the jar files and clickOpen.Click
Add External JARS..., navigate to.../cq5libs/sling/jcr_root, select all the jar files and clickOpen.Click
Add External JARS..., navigate to.../cq5libs/wcm/jcr_root, select all the jar files and clickOpen.Click
Add External JARS..., navigate to<cq-installation-dir>/crx-quickstart/server/lib/container, select all the jar files and clickOpen.Click
OK.
To create the Core Maven project, follow the steps:
In Eclipse, create the Core Maven project:
In the Menu bar, click
File, selectNew, thenOther....In the dialog, expand the
Mavenfolder, selectMaven Projectand clickNext.Check the
Create a simple projectbox and theUse default Workspace locationsbox, then clickNext.Define the Maven project:
Group Id:com.day.cq5.myappArtifact Id:coreName:CQ5 MyApp CoreDescription:This is the Core module
Click
Finish.
Add the necessary plugins and dependencies to the core project:
Open the
pom.xmlfile undercore.Copy-paste following code before the </project> tag:
<packaging>bundle</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>1.4.3</version> <extensions>true</extensions> <configuration> <instructions> <Export-Package> com.day.cq5.myapp.*;version=${pom.version} </Export-Package> </instructions> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.day.cq.wcm</groupId> <artifactId>cq-wcm-api</artifactId> <version>5.1.20</version> </dependency> <dependency> <groupId>com.day.cq</groupId> <artifactId>cq-commons</artifactId> <version>5.1.18</version> </dependency> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.api</artifactId> <version>2.0.3-incubator-R708951</version> </dependency> </dependencies>Save the changes.
Deploy the CQ5 specific artifacts as defined in the pom.xml (
cq-wcm-api,cq-commonsandorg.apache.sling.api) to the local Maven repository:From the system command line go to
<your-user-dir>/.m2/repository/com/day/cq/wcm/cq-wcm-api/5.1.20(create the folders if they don't exist) and execute vlt co http://localhost:4502/crx /libs/wcm/install/cq-wcm-api-5.1.20.jar . to export the library from the CQ5 server.From the system command line go to
<your-user-dir>/.m2/repository/com/day/cq/cq-commons/5.1.18(create the folders if they don't exist) and execute vlt co http://localhost:4502/crx /libs/cq/install/cq-commons-5.1.18.jar . to export the library from the CQ5 server.From the system command line go to
<your-user-dir>/.m2/repository/org/apache/sling/org.apache.sling.api/2.0.3-incubator-R708951(create the folders if they don't exist) and execute vlt co http://localhost:4502/crx /libs/sling/install/org.apache.sling.api-2.0.3-incubator-R708951.jar . to export the library from the CQ5 server.
![[Note]](../resources/note.png)
Note You don't need to perform this step if the three CQ5 artifacts are globally deployed for the project on a Maven repository (e.g. using Apache Archiva).
Set the Java Compiler to version 1.5:
Right-click the
coreproject, selectProperties.Select
Java Compilerand set following properties to 1.5:Compiler compliance levelGenerated .class files compatibilitySource compatibility
Click
OK.In the dialog window, click
Yes.
Create the package
com.day.cq5.myappthat will contain the Java classes undercore/src/main/java:Under
core, right-clicksrc/main/java, selectNew, thenPackage.Name it
com.day.cq5.myappand clickFinish.
When editing UI code use the following sequence:
Create a template and a component with the CRX Explorer.
Update the changes with VLT (export from the repository to your file system) .
Create a component script (JSP) with Eclipse.
Check in the changes from the file system into the repository with VLT.
The following example illustrates this process:
Create a new template with the CRX Explorer:
In the CRX Explorer, under
/apps/myApp/templates, create a new template:Name:contentpageType:cq:TemplateUnder the
contentpageNode, edit the Propertyjcr:titleand add asValue:MyApp Content Page TemplateUnder the
contentpageNode, add a new Node:Name:jcr:contentType:cq:PageContentUnder the
jcr:contentNode, edit the Propertysling:resourceTypeand add asValue:myApp/components/contentpageUnder the
jcr:contentNode, add a new Property:Name:personNameValue:myName
Create a new component with the CRX Explorer:
In the CRX Explorer, under
/apps/myApp/components, create a new component:Name:contentpageType:cq:Component
Use VLT to update the changes made from your repository to your file system, and therefore Eclipse:
From the system command line navigate to
<eclipse>/<workspace>/myApp/ui/src/main/content/jcr_root.Execute: vlt st --show-update to see the changes made on the repository.
Execute: vlt up to update the changes from the repository to your file system.
Create the component script (JSP) with Eclipse:
In Eclipse, navigate to
ui/src/main/content/jcr_root/apps/myApp/components/contentpage.Right-click
contentpage, selectNew, thenFile.In the dialog, name the file
contentpage.jspand clickFinish.Copy the following code into
contentpage.jsp:This is the contentpage component.
Save the changes.
With VLT check in the changes from the file system into the repository:
From the system command line navigate to
<eclipse>/<workspace>/myApp/ui/src/main/content/jcr_root.Execute: vlt st to see the changes made on the file system.
Execute: vlt add apps/myApp/components/contentpage/contentpage.jsp to add the
contentpage.jspfile to VLT control.Execute: vlt ci to commit the
contentpage.jspfile to the repository.
From CQ5 create a page based on this template. Open the page to make sure it displays the following message:
This is the contentpage component.
![]() | Tip |
|---|---|
It is possible to define the VLT commands as External Tools in Eclipse. This enables you to run the VLT commands from within Eclipse. |
When editing Core code use the following sequence:
Create a Java class.
Compile the Java class.
Reference the jar file in the ui library.
Embed the Java Class logic into the JSP script.
Use VLT to check these changes to the JSP script (in the file system) into the repository.
Use VLT to deploy the jar file (with the compiled class) from the file system into the repository.
The following example illustrates this process:
Create the Java class:
In Eclipse, under
core/src/main/java, right-click thecom.day.cq5.myapppackage, selectNew, thenClass.In the dialog window, name the Java Class
HelloPersonand clickFinish. Eclipse creates and opens the fileHelloPerson.java.In
HelloPerson.javareplace the existing code with the following:package com.day.cq5.myapp; import com.day.cq.wcm.api.Page; public class HelloPerson { private Page personPage; public static final String PN_PERSON_NAME = "personName"; public HelloPerson(Page personPage) { this.personPage = personPage; } public String getHelloMessage() { String personName = personPage.getProperties().get(PN_PERSON_NAME).toString(); return personName != null ? personName : "--empty--"; } }Save the changes.
Compile the Java class:
Right-click the
coreproject, selectRun As, thenMaven Install.Make sure that a new file
core-0.0.1-SNAPSHOT.jar(containing the compiled class) is created undercore/target.
Reference this jar file in the ui library to enable the code completion when accessing this class with the JSP script:
In Eclipse, right-click the
uiproject, selectBuild Path, thenConfigure Build Path. In the dialog select theLibrariestab.Click
Add JARS...and navigate tocore/target, select thecore-0.0.1-SNAPSHOT.jarfile and clickOK.Click
OKto close the dialog.
Embed the Java Class logic into the JSP script:
In Eclipse, open the JSP script
contentpage.jspinui/src/main/content/jcr_root/apps/myApp/components/contentpage.Replace the existing code with the following:
<%@ page import="com.day.cq5.myapp.HelloPerson" %> <%@include file="/libs/wcm/global.jsp"%> <% HelloPerson hello = new HelloPerson(currentPage); String msg = hello.getHelloMessage(); %> Hello, <%= msg %>.</br></br> This is the contenpage component.
Save the changes.
With VTL check in the changes to the JSP script from the file system to the repository:
From the system command line navigate to
<eclipse>/<workspace>/myApp/ui/src/main/content/jcr_root.Execute: vlt st to see the changes made on the file system.
Execute: vlt ci to commit the modified
contentpage.jspfile to the repository.
Deploy the jar file containing the compiled class from the file system into the repository with VLT:
In Eclipse, under
core/target, copy thecore-0.0.1-SNAPSHOT.jarfile.In Eclipse navigate to
ui/scr/main/content/jcr_root/apps/myapp/installand paste the copied file.From the system command line navigate to
<eclipse>/<workspace>/myApp/ui/src/main/content/jcr_root.Execute: vlt st to see the changes made on the file system.
Execute: vlt add apps/myApp/install/core-0.0.1-SNAPSHOT.jar to add the jar file to VLT control.
Execute: vlt ci to commit the jar file to the repository.
In your browser, refresh the CQ5 page to make sure it displays following message:
Hello, myName. This is the contentpage component.
In CRX Explorer, change the value myName and make sure that the new value is displayed when you refresh the page.
This section points to three open source softwares which enhance the development of CQ5 projects by adding collaboration and automation features:
Subversion (SVN) to manage a central repository where all the developers involved in the project can commit and retrieve the code and the content they generate on their local instance.
Apache Archiva to centrally store and retrieve the project libraries.
Apache Continuum to automate the build process.
As the CQ5 repository can be mapped to your file system with VLT, it is now easy to set up a central repository with SVN. This is used by all developers in the project as a place they can commit, and retrieve, the code and content they generate on their local instances.
The setup of an SVN server is not covered in this document as many tutorials are already available online.
When VLT is in operation it creates .vlt files within the local directory structure. These .vlt files hold timestamps and other VLT-specific information that should not be checked into the SVN repository. This can be prevented by adding .vlt to the ignore list of the local SVN setup. To do this you add the following code to the local SVN setup file - settings.xml in the .subversion directory of your user's HOME directory:
[miscellany] ### Set global-ignores to a set of whitespace-delimited globs ### which Subversion will ignore in its 'status' output, and ### while importing or adding files and directories. global-ignores = .vlt
Java libraries, called artifacts in Maven language, can be managed centrally through Apache Archiva, an artifact repository that is used to store and retrieve the project artifacts. The setup of Archiva is well detailed online and can be referenced during setup. The Archiva server requires little management outside that of configuring local developer accounts to obtain access to the snapshots and full releases.
Once the project content and code is centrally available through an SVN server, it is possible to automate the build process and run the build on a daily basis (for example a nightly build). This is done with Apache Continuum, a continuous integration server with the sole duty of providing build management of artifacts and releases.
The setup of Continuum is also well detailed online.
![[Tip]](../resources/tip.png)