Liferay - Share Utils class between two different portlets

I am developing a Liferay application consisting of two different portlets, both of which must perform certain operations, so I decided to put these operations in static methods in an external Utils class.

I have to export this class to avoid duplicating the same code in both portlets, and I want the portlets to be in different WAR files.

I know that I can pack the Utils class into a JAR file, but we are still developing and we don’t want to regenerate the JAR and restart Tomcat for every change.

What is the best option and how can I do it?

+4
source share
4 answers

If you use the Liferay SDK, you can use the clients directory (recently changed to shared ) to host your shared code.

A good example is deploy-listener-shared in combination with deploy-listener-hook .

Be that as it may, all you have to do is modify your build.xml files that the client / generic classes will use. If you look at the deployment-listener assembly file , you can see all you need to add is this.

For the new SDK:

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

For older SDK:

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

Hope this helps!

+4
source

There is another way to create a JAR file, but it does not require a server restart (at least for Tomcat).

Write a build script for your JAR file so that it compiles, creates the JAR, and finally copies it to the following location:

 {tomcat}/webapps/ROOT/WEB-INF/lib 
  • Then in your portlet open "liferay-plugin-package.properties" (in Liferay Developer Studio / Liferay IDE this should open with a good graphical interface).
  • Then add the name of your JAR to the list of "portal-dependency-jars" in this file, so in the source that it would like (or just click the "Add" button in the GUI and select the JARs you want):

    Dependency portal jar = my custom-lib.jar, my-other-order lib.jar

  • Save the file and reinstall the portlet, and the JAR will be copied when you deploy the portlet.

I used this method for custom JARs and third-party JARs that I need to use in my portlets.

+2
source

At the development stage, just pack the jar file with both applications.

If any application depends on another, somehow everything is in order.

Another solution is to use the JRebel tool. This will allow you to redeploy the jar in tomcat without rebooting.

0
source

You can also have multiple portlets in one .war. You can simply define them as in portlet.xml .

0
source

Source: https://habr.com/ru/post/1441836/


All Articles