How to share jar within multiple portlets?
Asked Answered
S

2

6

I'm using several jars in my portlets, for instance c3p0. Now I want to avoid to put the jars in every of my portlets lib folders. How can I share one jar file within multiple portlets? And how should I integrate them in the Eclipse IDE? Add an external jar or put them in one dedicated project and include them from there?

Sappy answered 10/9, 2012 at 10:8 Comment(1)
Hi Strauberry, I might have a solution for you that will solve your changing JAR a lot problem. I've had the exact same situation!Cheroot
C
6

Another solution would be to put them under ../liferay-portal-<version>/tomcat-<version>/webapps/ROOT/WEB-INF/lib

If you place them here you can add the JAR as a dependency for your portlet in the portlets liferay-plugin-package.properties (if you're using Liferay Developer Studio or Liferay IDE then there's a nice GUI for this).

Then on deployment Liferay will copy the required JARS from ROOT/WEB-INF/lib to your portlets WEB-INF/lib

This I believe is the Liferay support mechanism for doing it, and doesn't require a restart because the JARs are copied to the portlets classpath on deployment.

Cheroot answered 11/9, 2012 at 8:9 Comment(2)
Hi Jonny, that sounds great! I have only one question left: where do I place the jar files on my computer during development? In my jar files there are class files I want to use the methods from (of course haha). Do I add them as "External Jars" to my project so the Java compiler is abled to access them?Sappy
I would place them where ever best suits you, I have mine under a util/additional-libs folder in my Liferay Plugins-SDK. I also have a ant build file for each one that compiles, JARs, and then copies the JAR to ROOT/WEB-INF/lib. So when the portlet is redeployed the updated JAR is already in the right place to be copied to the portlet. However if you're not using Liferay IDE, yes add them as External JARs in the "Configure Build Path" section...And checkout Liferay IDE because it will pick up the JARs automatically! :)Cheroot
G
2

One way I can think off is to put all of them in the global path.

For e.g. in Tomcat you could place them at ../liferay-portal-<version>/tomcat-<version>/lib/ext and then when you configure the server-runtime library in eclipse you will have these jars in your build-path.

This link may also help you decide, but it speaks for *-service.jar but I am not sure it would work other jars in any one portlet.

Greige answered 10/9, 2012 at 10:52 Comment(5)
Thank you for your answer! Is there a way to use the browser interface for deploying it? I'm changing the commonly used jar quite often at the moment (heavy development) so a manual copy&paste and restart would be very anoying...Sappy
jars going in the global class path need a server restart to be loaded. I don't know about any other way to achieve your requirement. Though there are tools which help to load the jars even without server-restart, so you may try to look for these.Greige
Benefit from putting them to tomcat/lib/ext is that then all web apps will share the same jars, loaded by the same class loader. If you are passing instances of classes between web applications (for example via session) you may run into ClassCastExceptions if each webapp has its own copies. Drawback in using lib/ext is that server restart is then required to deploy new versions of the jars.Forejudge
Another drawback I've experienced is that if you're using versioned jars (i.e. maven style), old jar isn't deleted when you put a new one : you have to go to the ROOT/WEB-INF/lib folder and delete the old versioned jar yourself....which can lead to problems when delivering your software ^^Midsummer
Your answer here, seems more appropriate for this question as well.Accursed

© 2022 - 2024 — McMap. All rights reserved.