Service builder is one of the most useful tool provided by Liferay to generate service and persistence layer. It generate all boilerplate code automatically
Starting from version 6.2 liferay supports creating plugin with Maven. Creating service builder with maven is not identical with how it was created with ANT. However it’s not that difficult though.
In this article, I will be showing how to create Liferay maven service builder. I will make it as simple as possible to make it fun to learn.
I will also show you how to set dependencies of service builder on other liferay maven plugin so Let’s jump-in
If you are new to create liferay plugin with Maven, I would recommend to refer my previous blog on creating Liferay Maven Plugin. It will clear the basic concepts like how maven works, how to set and activate maven profiles, how to configure maven profile in eclipse, how to create plugin and how to deploy it.
New Plugin type – Service Builder Portlet
Staring from 6.2, liferay introduced new plugin type called – Service Builder Portlet. It’s nothing new but a portlet with Service builder capability.
Maven requires different project structure for plugin of type Service builder. That is the reason this new plugin type is introduced.
It creates two sub projects (modules) under service builder plugin project. We will see more details in a short while so don’t confused and keep reading 🙂
Note:- In Maven, sub project is called module. Maven Project can have one or more modules. A Maven project itself can be a module if it’s defined as sub project of some other project. You can find more details about maven project vs module from below resource
Create Liferay Maven Service builder Portlet
Let’s create liferay maven service builder portlet. I am using Liferay IDE 3.1.2 GA3 which ships with Eclipse Neon 3 version. Click on File–>New–>Liferay Plugin Project
Give the details as shown in above screenshot. You can find the explanation about each of these fields from my previous blog on Liferay Maven plugins and refer the section Liferay Maven plugin – Create Liferay MVC Portlet
The only difference here is to choose plugin type as Service Builder Portlet rather than just plain Portlet. Click on Finish and you will see 3 different projects as below.
sample-service-builder (Main project)
- This is the plugin type project that we created. You can notice two sub projects or we can say modules (in maven context) are created
- First sub project (module) is portlet and second is of type service.
- Building or deploying of the service happens on this project.
sample-service-builder-portlet (portlet module)
- This module contains all implementation classes for target service.
- For example, localServiceImp, persistanceImpl, modelImpl etc are created in this module.
- This module contains the service.xml file in which you can define your model entities.
sample-service-builder-portlet-service (service module)
- This module contains interfaces, util, wrapper and some other classes that are ship in service jar. For example localServiceUtil.java etc.
- In short, this modules represents the service jar that would be generated when you build the service.
- You need to give maven dependency of this modules to other maven plugin (for example portlet) so they can access the service.
Enough theory. Let’s create the actual service and see how the classes are generated.
Go to sample-service-builder-portlet modules and open service.xml file under src–>main–>webapp–>WEB-INF folder. Add following entities.
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<entity name="Student" local-service="true" remote-service="true">
<column name="studentId" type="long" primary="true" />
<column name="studentName" type="long" />
<column name="dob" type="String" />
<column name="standard" type="Date" />
<entity name="Teacher" local-service="true" remote-service="true">
<column name="teacherId" type="long" primary="true" />
<column name="teacherName" type="long" />
<column name="dob" type="long" />
<column name="subject" type="long" />
I have just added Student and Teacher entities with few fields. You need to give proper namespace value or else it shows error.
If service builder is new for you, just go through my blog on service layer in liferay . Though I have created service builder with ANT in that blog, It gives you good inside about service builder in liferay.
I also kept local service and remote service true. In most of the cases remote service is not required but to demonstrate how and where the generated classes are created, I kept remote service true.
Build Liferay maven service builder
Open command prompt and type the path till sample-service-builder (outer) project and type following command.
mvn install liferay:build-service
Refresh the project. You can also press Ctrl+F5 and select your maven project to refresh. In our case select sample-service-builder and two sub projects (modules) and click Ok button.
Let’s see each of these modules one by one.
Module 1: sample-service-builder-portlet (portlet module)
Needless to say that this modules contains all IMPL classes in different packages like model.impl, service.impl, persistence etc.
Open the target folder of this module and you will see WAR file which represents the service builder WAR. When you deploy the service builder, this WAR actually deployed on liferay server.
Module 2: sample-service-builder-portlet-service (service module)
All interfaces, wrapper classes and Util classes are generated in this module. These classes would be part of service jar.
The service jar would also be generated under target folder in this module. sample-service-builder-portlet-service-1.0.0-SNAPSHOT.jar in our case.
You can notice one more thing here. The parent project (sample-service-builder) doesn’t have any target folder. It actually parent project of following two modules
- Portlet module
- Maven will create the actual WAR in this module.
- Service module
- Maven will create the service JAR in this module.
This is all about creating liferay service builder with Maven. Next you need to know how to give dependency of service builder to other liferay maven modules.
As I said, you need to give dependency of service module to other liferay maven plugin.
For example you created a liferay maven portlet called Dashboard where you want to access this service. In this scenario, you need to give dependency of service module of this (sample service builder ) maven project as below.
In <dependencies> section of Dashboard portlet, just give the dependency as below.
groupId, artifactId and version is the group Id, artifact Id and version of service module (sample-service-builder-portlet-service) respectively.
Last important thing. If you know how to create Liferay maven service builder, it would be very ease for you to understand the service builder in Liferay DXP. The concept of service builder in DXP is more or less similar to liferay maven service builder as described in this blog.
You can refer the code that I used to demonstrate the liferay maven service builder concept from Git hub link.
- Liferay supports creating service builder portlet with maven
- It creates two sub projects (called modules) under main service builder portlet plugin project.
- one module – called portlet module contains all impl classes. It also has service.xml file.
- second module – called service module contains all interfaces and util classes. It also contains the service jar.