Monday 4 August 2014

AEM Project Setup using Maven

This article focuses on how to setup a AEM/CQ project using Maven and explaining the basic structure of AEM projects. 

Lets start with a brief about APACHE MAVEN :

Maven is a build automation tool that helps managing the builds of the projects and their dependencies (jar files required). It is based on the concept of POM (Project Object Model). There are numerous number of maven plugins available over the internet, each of which fulfills a different purpose.As we are here for AEM Project setup we won't cover anything more about maven here.

Now let us see the folder structure of a AEM Project which you get to see in CRXDE Lite :
  • /apps : Top level folder whatever you code will be stored in this folder.
  • /apps/[YOUR-APP-FOLDER] : Root folder of one out of many projects that you will be creating under apps folder.
  • /apps/[YOUR-APP-FOLDER]/components : Folder that contains all the components and their respective JSPs.
  • /apps/[YOUR-APP-FOLDER]/templates : Folder that contains all the templates that you will be creating, which points to respective page component.
  • /apps/[YOUR-APP-FOLDER]/install : Folder that contains bundle of your project.
  • /apps/[YOUR-APP-FOLDER]/config : Folder that stored configuration specific to this project.
  • /etc/design//[APP-DESIGN-PAGE] : Design page of your application that stores all clientlibs (JS and CSS) and other static resources as well.
  • /content/dam/[YOUR-APP-DAM-FOLDER] : Folder that contains all the assets of your site.
  • /content/[YOUR-SITE] :  Location where site is stored .

Before we move forward here are some pre-requisites that should be available on your machine (installed) for building AEM project using maven : 

  1. First and foremost would be Apache MAVEN .
  2. Adobe AEM/CQ running instance.
  3. Any IDE eg Eclipse , Idea, Netbeans etc(Here we are using Intellij Idea for this example)
Link of source code for the demo project created https://github.com/ankit-gubrani/Codebrains

Here are the steps to create AEM Project using maven :


Step 1 : Create maven project using Adobe's multimodule-content-package-archetype. Using Command prompt go to the directory where you want to create project and run following command :

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

after this maven will prompt and ask for some information(like groupid, arifactid,version etc), provide that infomation and your project will be created.This will create a multimodule maven project with 2 modules (Bundle and Content)

Step 2 : Verify the code if it works fine or not. You can check it by running command :

mvn clean install 

if it completes without any error that means project was made successfully.

Step 3 : Import the project in any IDE . Here in the example I am using IntelliJ Idea : 


Step 4 : Now that everything is set in place , import the project in CQ running instance by running command : 

mvn clean install -PautoInstallPackage 

but if you are running CQ on different port or if username and passowrd is changed used following cmmand :

mvn clean install -PautoInstallPackage -Dcrx.port=3502 -Dcrx.username=newuser -Dcrx.password=newpassword

This will install the project in CQ instance and you can see following folder structure iunder your project : 




  
And this is it we are done with creating a AEM project using maven build tool. 

Using this you will create a two module maven project. Those modules are :

  1. Content Module : This module contains all the code that will pe under apps/[YOUR-APP-PROJECT] or /etc/[DESIGN-PAGE] or /content/[ANY-PAGE] folder.
  2. Bundle Module : This module contains all the java files that will be wrapped up into a bundle and will be installed into Feilx.



Here is the source code for the demo project created https://github.com/ankit-gubrani/Codebrains


Your comments and suggestions are welcome.