Create OSGI Bundle in AEM
The focus of this tutorial is to learn how to create OSGI bundle in AEM and deploy it. This tutorial is intended for AEM beginners who are facing issue in creating there first OSGI service.
After completing this tutorial. you will have a clear understanding of:-
- How OSGI Bundle is created in AEM
- How to Deploy a Bundle to AEM.
- Display Data on JSP from OSGI Service.
Pre-requisites for creating a mvn archetype Project:-
- Maven should be installed. See how to install maven
Steps for creating an OSGI Bundle in AEM:-
Create a mvn archetype project:
- Open command prompt and go to your working directory (for example, C:\Projects\practice).
- Run the below Maven 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 -DgroupId=com.aem -DartifactId=MyFirstOSGIBundle -Dversion=1.0-SNAPSHOT -Dpackage=com.aem -DappsFolderName=aemcq5tutorials -DartifactName="MY First OSGI Service" -DcqVersion="5.6.1" -DpackageGroup="Aem Cq5 Tutorials"
Note: Please make sure that this command will download a new multi module project on your local disk. So run this command from a suitable location where you keep your code base
- When prompted for confirmation, Specify Y.
- Once you see success message. Change the command prompt to the generated project. For example:
C:\AdobeCQ\MyFirstOSGIBundle
and run the below Maven commandmvn eclipse:eclipse
- Now you can import your project into Eclipse.
Importing Maven Project in Eclipse
- Open Eclipse and go to File–>Import–>Existing Maven project.
- Click Next
- Browse the path of the directory where your project was imported & click on Finish.
- You should now see the project imported in your eclipse.
- This sample project will have many extra files so don’t worry about them. You can either let them be where they are or else delete them in case you do not want them to be a part of the bundle. We are concerned with MyFirstOSGIBundle-bundle.
- Now create a package or java file inside the MyFirstOSGIBundle-bundle where you want to keep your java files.
- Write your own custom logic or paste the below sample code.
package com.aem; /** * This class is used to display a message which user sends from jsp. * @author ankur.ahlawat * */ public class MyFirstOSGIService { public String displayMessage(String val){ return "Welcome to AEM CQ5 Tutorials:- Your Message is"+ val; } }
Note:- Now you have two option:-
- Upload bundle automatically to project MYFirstOSGIService.
- Upload bundle manually to any project.
Building OSGi bundle using Maven:
Uploading and deploying Bundle Automatically to install folder:
- Start your author instance and create project with same name as Artifact Id mentioned in pom.xml i.e MYFirstOSGIService.
- Open command prompt and execute below command.
mvn clean install -P autoInstallBundle
Note:-You have to be in the directory where project pom is located. In our case it is MyFirstOSGIBundle folder.
- Once above command gets executed you should be able to see the bundle inside install folder.
Uploading Bundle manually to system/console/bundles:
- Open command prompt and go to your project folder. i.e C:\Project\practice\MyFirstOSGIBundle folder.
- Run below maven command:
mvn clean install
- The OSGi bundle can be found in the following folder: C:\Project\practice\MyFirstOSGIBundle\bundle\target. The file name of the OSGi component is MyFirstOSGIBundle-bundle-1.0-SNAPSHOT.
Deploy the Bundle to AEM :
If you have opted for manual deployment in above scenerio, then follow below steps to deploy OSGI bundle to AEM:
- Go to http://localhost:4502/system/console/bundles.
- Your OSGi bundle is successfully created and deployed.
Display Data on JSP from OSGI Service:
- Create a Template, Page and a Basic Component as shown in previous tutorial.
- Open component.jsp and enter below code
<%@include file="/libs/foundation/global.jsp"%> <%@ page import="com.aem.MyFirstOSGIService" %> <h1><%= properties.get("title", currentPage.getTitle()) %></h1> <% MyFirstOSGIService hw = new MyFirstOSGIService(); %> <h3><%= hw.displayMessage("OSGI Service Executed") %></h3>
- Double click on your page in siteadmin. Your page should display message from osgi service.
- Another Way of executing OSGI Service is using sling.getService.
- Create an Interface which call our main class.
- Enter below code in component.jsp
<%@include file="/libs/foundation/global.jsp"%> <h1><%= properties.get("title", currentPage.getTitle()) %></h1> <% com.aem.MyFirstOSGIServiceInterface firstService = sling.getService(com.aem.MyFirstOSGIServiceInterface.class); String ff = firstService.displayMessage("Hi i am sling Service call"); %> <h2>This page invokes the AEM KeyService</h2> <h3>The value of the key is: <%=ff%></h3>
Click Here To Learn How to Create Multi Module Project in AEM using AEM Plugin.
Leave a Reply