Liferay - Share Utils class between 2 different portlets

liferay, portlet, share

Solution

If you're using the Liferay SDK, you can use the `clients` (recently changed to `shared`) directory to put your common code.

A good example is how deploy-listener-shared is used in conjunction with deploy-listener-hook.

From what it looks like, all the configuration you need to do is to modify your `build.xml` files that will use the client\shared classes. If you look at build file of deploy-listener-hook you can see all you need to add is the.

For the new SDK:

<property name="import.shared" value="my-utils-shared" />

For the older SDK:

<property name="dependent.clients" value="my-utils-client" />

Hope this helps!

Problem

I'm developing a Liferay application, consisting on 2 different portlets, an both have to make certain operations in common, so I decided to put that operations in static methods in an external Utils class. I have to externalize that class to avoid duplicating the same code in both portlets, and I want to have the portlets in different WAR files. I know I can package the Utils class in a JAR file, but we are still developing and we don't want to regenerate the JAR and restart the Tomcat for every change. Which is the best option and how can I perform it?

Original source