Friday, November 28, 2014

[Liferay] [Maven] [Service Builder] Building Liferay Service using Maven - "The Correct Way"

I found many posts which explain and show the way to build services using maven by modifying the pom.xml and ask you to include plugins with groupId : org.codehaus.mojo and artifactId : build-helper-maven-plugin.
Even though that works just fine, I could not take it as a right way to do. So what maven and liferay actually offer in this case?

We know that our portlets just use the service jar file they need. That makes service an independent project. That's exactly what maven makes it..

Try this with me.. lets build a brand new portlet. Portlet name is : mvn-service-builder-portlet [You could just name as mvn-service-builder. By mistake, I added portlet to artifactId and now portlet comes two times in the project name.]

1. mvn archetype:generate

2. You will now see a folder named mvn-service-builder-portlet but inside there would be two more folders

3. Now you can load these two projects in eclipse and see there is a service.xml file in mvn-service-builder-portlet-portlet > src > main > webapp > WEB-INF. In short where it should be :) 

4. Add your services.

5. Now navigate to the folder inside mvn-service-builder-portlet using command prompt. You can now run the commands to build service
mvn clean liferay:build-service install

6. liferay:build-service is going to build service and install is going to install the jar file to m2 repository in your local machine. 

7. If you see below error in eclipse or any other IDE where you imported the projects-
The hierarchy of the type 'Class name' is inconsistent

Then make sure the version of your service in pom of service is same as in portlet pom. If they are different, change the version to be the same. Usually it is ${pom.version}which you can replace to e.g. 1.0.0.0 or a version of your choice.

Now also choose a proper namespace for your service builder.

After this, re-run the service builder and install again. Refresh the project in IDE and you're done.

You don't need to add plugins manually to your pom.xml, this I feel the clear and correct way to develop services in portlets :)

Hope this helps someone.

Get the source here Maven Service Builder Sample Portlet Project

Until next time.. keep building services and enjoy :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.