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:-

Pre-requisites for creating a mvn archetype Project:-

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"

create mvn archtype project command in aem
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.
    confirm mvn arch type project in aem
  • Once you see success message. Change the command prompt to the generated project. For example: C:\AdobeCQ\MyFirstOSGIBundle and run the below Maven commandmvn build eclipse project using pom
    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.
    Import existing maven project in eclipse
  • Click Next
  • Browse the path of the directory where your project was imported & click on Finish.
    import maven project select pom in aem
  • 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.
    eclipse maven project structure
  • Now create a package or java file inside the MyFirstOSGIBundle-bundle where you want to keep your java files.
    first osgi service aem
  • 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.
    sample osgi service in aem

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.
    • Click Install/Update on Top-right corner to open bundle upload window.
    • Select check box for start bundle, to start bundle immediately.
    • Upload the jar file and click install.
      upload install deploy an osgi bundle in aem
  • Your OSGi bundle is successfully created and deployed.
    deploy first osgi service bundle in aem

Display Data on JSP from OSGI Service:

<%@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>

call osgi service from jsp

  • Double click on your page in siteadmin. Your page should display message from osgi service.
    Display Data on JSP 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.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.