March 15, 2020
Estimated Post Reading Time ~

How to create an OSGI Bundle in AEM

The focus of this tutorial is to learn how to create an OSGi bundle in AEM and deploy it. This tutorial is intended for AEM beginners who are facing issues in creating their first OSGI service.

After completing this tutorial. you will have a clear understanding of:-
·       How the OSGI Bundle is created in AEM
§  Create a mvn archetype project.
§  Importing Maven Project in Eclipse.
§  Building OSGi bundle using Maven.
·       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.
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:
1
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 codebase
·       When prompted for confirmation, Specify Y.

·       Once you see a success message. Change the command prompt to the generated project. For example C:\AdobeCQ\MyFirstOSGIBundle and run the below Maven command
1
mvn 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 a project with the same name as Artifact Id mentioned in pom.xml i.e MYFirstOSGIService.
·       Open command prompt and execute the below command.
1
mvn clean install -P autoInstallBundle
Note:-You have to be in the directory where the project pom is located. In our case, it is the MyFirstOSGIBundle folder.
·       Once the above command gets executed you should be able to see the bundle inside the 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:
1
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 the above scenario, then follow below steps to deploy OSGI bundle to AEM:
·       Go to http://localhost:4502/system/console/bundles.
§  Click Install/Update on Top-right corner to open bundle upload window.
§  Select checkbox for start bundle, to start bundle immediately.
§  Upload the jar file and click install.

·       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 the 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 a message from the OSGi service.

·       Another Way of executing OSGI Service is using sling.getService.
§  Create an Interface which calls 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.




By aem4beginner

No comments:

Post a Comment

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