April 24, 2020
Estimated Post Reading Time ~

Create a Bundle for AEM using Maven

In this session, you will learn about developing and deploying bundles for AEM development.

(Following is the transcript of the video above. See the video to get the right context. For sample files, visit GitHub.)

Bundles help to modularize Java development using the Open Source Gateway Initiative (OSGi) framework. A bundle is essentially a Jar file that you deploy in the Apache Felix console. Felix console is the OSGi container.

To make it simpler, to accomplish a complex task, you can deploy a bundle in Felix console that runs with AEM, and then avail the services offered by the bundle. We are not going to explore a lot about OSGi bundles. We will see how a bundle can easily be developed, deployed, and used. You need to have installed Maven in your system. I am not going to talk about how it can be done.
There is plenty of information available on the Internet. Please install Maven.

I am on a Windows machine. After installing Maven, if I run mvn in a command prompt, this the result that I get. You should see something similar if you had a successful installation and you added it to your classpath.

I have created directory, aem-company. This is the company name we used for our sample project. Let me move to the directory.

I will run the following command to create an AEM project. (Please refer to the video.) The command is long; I have added the command in my GitHub repository.

Using this command, we will create an AEM project using a Maven archetype. An archetype is a project templating toolkit. Basically, it creates a sample project from which you can start your work.


Note that Adobe provides an Archetype repository URL for the developers. I don’t want to explain the rest of the parameters. It would be too complex for this session. After running the command, a project structure is created.

Let us explore it further. There are two folders: bundle and content. We will see bundles first. It primarily created a Java project. By default, it added the first Java class that you can use as a start.
You can use this project structure to add more classes.

It created an Interface, HelloService. And, it implements a getRepositoryName() method that should return the underlying repository name. In this case, Apache Jackrabbit Oak is the repository we use.

Go to the Impl folder where we have the actual implementation. @service and @content are two JCR annotations we will see later.

The implementation class has a getRepositoryName() implementation that returns the repository name.
Now, let us deploy the project.

Run the following command: mvn -PautoInstallPackage install
Let us wait for it to complete.

Now log into Apache Felix: http://localhost:4502/system/console/bundles

Locate the bundle by searching for AEM-Company.
You could see the service we deployed now: com.aemcompany.myproject.HelloService

Let us quickly access the service. I have developed a component that uses the service. Obtain it from my GitHub. I used a JSP page as the component’s default script since it’s easy to access a Java service in a JSP page.

<% com.aemcompany.myproject.HelloService repositoryService=sling.getService(com.aemcompany.myproject.HelloService.class); %> Hello, <B><%= repositoryService.getRepositoryName() %> </B>

Let us go to the AEM page we created. Drag and drop the component to the page.
Note that it displays the repository name.


By aem4beginner

No comments:

Post a Comment

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