Easy way to create Liferay maven service builder

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 a plugin with Maven. Creating a service builder with maven is not identical to how it was created with ANT. However, it’s not that difficult though.

Liferay maven service builder

In this article, I will be showing how to create a 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 plugins, so Let’s jump-in.

If you are new to create the Liferay plugin with Maven, I recommend referring to 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 a plugin, and how to deploy it.

New Plugin type – Service Builder Portlet

Starting from 6.2, Liferay introduced a new plugin type called – Service Builder Portlet. It’s nothing new but a portlet with Service builder capability.

Maven requires a different project structure for a plugin of type Service builder. That is the reason this new plugin type is introduced.

It creates two sub-projects (modules) under the service builder plugin project. We will see more details in a short while, so don’t confuse and keep reading 🙂

Note:- In Maven, the subproject is called a module. Maven Project can have one or more modules. A Maven project itself can be a module if defined as a subproject 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 a 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 ProjectLiferay maven service builder - create service builder portlet

Give the details, as shown in the above screenshot. You can find the explanation about each of these fields from my previous blog on Liferay Maven plugins and refer to 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.

Liferay maven service builder - generated plugins

Explanation

sample-service-builder (Main project)

  • This is the plugin type of project that we created. You can notice two sub-projects, or we can say modules (in maven context) are created
  • The first sub-project (module) is portlet, and the second is of type service.
  • The building or deploying the service happens on this project.

sample-service-builder-portlet (portlet module)

  • This module contains all implementation classes for the 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 module represents the service jar that would be generated when you build the service.
  • You need to give maven dependency of these modules to other maven plugins (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 the 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">

<service-builder package-path="com.opensource.techblog.sampleservice">
    <namespace>sample_sb</namespace>
    
    <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>
    <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" />
    </entity>
</service-builder>

Explanation

I have just added Student and Teacher entities with few fields. You need to give the proper namespace value, or else it shows an error.

If the service builder is new for you, just go through my blog on Liferay’s service layer. Though I have created a service builder with ANT in that blog, It gives you a good insight into Liferay’s service builder.

I also kept local service and remote service true. In most 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 a command prompt and type the path till the sample-service-builder (outer) project and type the following command.

mvn install liferay:build-service

Refresh the project. Likewise, 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 the Ok button.

Let’s see each of these modules one by one.

Module 1: sample-service-builder-portlet (portlet module)

Liferay maven service builder - classes of portlet module

Needless to say that this module 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 the WAR file, which represents the service builder WAR. When you deploy the service builder, this WAR is actually deployed on the Liferay server.

Module 2: sample-service-builder-portlet-service (service module)

Liferay maven service builder - classes of service module

All interfaces, wrapper classes, and Util classes are generated in this module. These classes would be part of the service jar.

The service jar would also be generated under the 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 the parent project of the 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.

Set Dependency

This is all about creating a 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 on the service module to other Liferay maven plugins.

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 on the service module of this (sample service builder ) maven project as below.

In the <dependencies> section in pom.xml of Dashboard portlet, just give the dependency as below.

<dependency>
    <groupId>com.opensource.techblog.sampleservice</groupId>
    <artifactId>sample-service-builder-portlet-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

groupId, artifactId, and version is the group id, artifact id, and version defined inside the pom.xml of service module (sample-service-builder-portlet-service).

Last important thing. If you know how to create a Liferay maven service builder, it would be straightforward for you to understand the service builder in Liferay DXP.  The concept of service builder in DXP is similar to Liferay maven service builder described in this blog.

Download

You can refer to the code that I used to demonstrate the Liferay maven service builder concept from the Git hub link.

Summing Up

  • Liferay supports creating service builder portlet with maven.
  • It creates two sub-projects (called modules) under the main service builder portlet plugin project.
  • One module – called the portlet module, contains all impl classes. It also has a service.xml file.
  • The second module – called the service module, contains all interfaces and util classes. It also contains the service jar.

Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

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.