Learn Liferay 7/DXP service builder – The right way!

admin

April 27, 2022

Service builder is most important part in Liferay development. In this post, I am going to talk about Service builder in Liferay 7/DXP. To explain it more effectively, I have taken example of Employee registration. Before we discuss about how use service builder in Liferay 7/DXP, let’s talk about some fact about it.

Liferay 7/DXP Service Builder at glance,

  • Liferay provides Service Builder tool for generating model objects, Service layer and DAO layer.
  • In Liferay 7, service builder generates two OSGI modules as follow,
  • Service API (in this example employee-api) module which contains service interfaces and wrapper services
  • Service module (in this example employee-service) contains service implementation layers
  • For more information on service builder, refer following link,

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/what-is-service-builder

Following are some prerequisite to start implementing service builder example,

Prerequisite:

Before we discuss steps to implement service builder, it is important for you to understand that Liferay 7/DXP uses OSGI. It makes Liferay 7/DXP modular application. Your service layer and portlets are different OSGI module and both are loosely coupled. You can learn more about OSGI in Liferay from following link,

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/osgi-and-modularity-for-liferay-6-developers

Following are the steps to generate service/API module using service builder. In next, we will understand how to use service layer module in portlet.

  1. Create Liferay workspace project:

To create a Liferay Workspace in IDE follow the following steps,

Open File Menu → Click on New → Select Liferay Workspace Project. Upon creation of the workspace, the structure should look like below:

Create Liferay workspace project

  1. Create a Liferay Services Builder

a. Right click on Liferay Workspace project in project explorer → Select New → Click on Liferay Module Project. Fill up the detail as follow,

  • Project Name: enter appropriate name e.g.” Employee”
  • Project Template Name: Select “servicebuilder”

Create a Liferay Services Builder

b. Now in Package Name enter appropriate name e.g. “com.enprowess.osgi.example” then click finish.

package Name enter

c. On click of ‘finish’, you will see following folder structure,

finish

  1. Modify “Service.xml”

Now that you have created skeleton for project. It is time to create service.xml. This is the file where you need to define entity details of your DB table. Following is the default service.xml.

created skeleton for project

To know more about service builder XML, please refer following link,

https://docs.liferay.com/portal/7.0/definitions/liferay-service-builder_7_0_0.dtd.html

  1. Build services:

You successfully created service.xml. When you build a service builder it generates classes for service layer. Now it’s time to build service, to run service builder, right click on employee-service folder from project explorer → Select Liferay → Click on build-service.

Congratulation! now you know how to generate service/API module in Liferay 7/DXP. Now let’s understand how you can use service module in portlet module. Here I assume that you already have create portlet module under Employee module.

  1. Update build.gradle:

Update build.gradle file to add service module in dependency. Now gradle will ensure that when you build your portlet module, it will add dependencies mentioned in build file. Following is the snapshot for your reference,

Update build.gradle

  1. Build portlet module:

Building portlet module is strait forward and simple if you are using gradle task view. To add gradle task view in your eclipse, Click Window menu → Click on show view option → select gradle task. Following are the steps to build and deploy portlet module.

  • Expand employee module → Click build target. It will build your portlet and add service jars in dependencies.
  • Similarly, once build is over, click on deploy target to deploy portlet with service layer on server.

Build portlet module

Congratulation! Now you know steps to implement service builder and use it in portlet.

Blog By,

Zeenesh Patel

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments