Foren

Share service classes between two portlet with Maven project

thumbnail
Anil T, geändert vor 8 Jahren.

Share service classes between two portlet with Maven project

Expert Beiträge: 313 Beitrittsdatum: 14.01.12 Neueste Beiträge
Hi Team,

I've created two portlets like A and B. Portlet A has custom table services and util classes. Now I want to use these util classes in portlet B. I am using maven for building these portlet. Please suggest how to access those util classes in portlet B?

Thanks,
Anil T
thumbnail
Andrew Jardine, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Hi Anil,

All you have to do is add a dependency to Portlet B that references the (portlet-service) module from Portlet A. It will then include the jar that is created wen you build A into your portlet B libs when you build portlet B
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Anil T:
I've created two portlets like A and B. Portlet A has custom table services and util classes. Now I want to use these util classes in portlet B. I am using maven for building these portlet. Please suggest how to access those util classes in portlet B?


The proper way is to do a "mvn install" in portlet A. This copies all artifacts into your local repo. If you have a repository (such as nexus or artifactory), then you can do "mvn deploy" to make available to your dev team and CI build server.

In portlet B, you just list it as a dependency and maven will include the jar in the target's WEB-INF/lib directory.

Just make sure you also list as a required deployment context in liferay-plugin-package.properties.

NOTE: Copying jars as Andrew suggests will work, but it is quite easy to forget to copy the jar to all of the places where you have a dependency; using the maven repo will guarantee that it gets used when a new build is done.

Also Note: When you are going to issue the "mvn liferay:build-service" command, it is a good idea to bump the version number in the pom. That way you can ensure that developers will not use a stale service jar. The downside here is that during initial development you're going to be changing versions quite frequently and it can be tough keeping the dependent projects up to date with the right version number. Likely you'll find a mix of few version changes initially but switch to a frequent version change when things have stabilized.
thumbnail
Andrew Jardine, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
NOTE: Copying jars as Andrew suggests will work, but it is quite easy to forget to copy the jar to all of the places where you have a dependency; using the maven repo will guarantee that it gets used when a new build is done.


ACK! I certainly didn't mean to MANUALLY copy it. I meant to add a dependency to your pom (for the project that needed the lib) so that it would be pulled in automatically. I expected someone using maven to already understand the fact that mvn install is required to add artifacts to a local repo emoticon
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
emoticon

I try not to assume anything in a post as I never know what level the next person that reads it may be at.

In some cases folks may understand you meant the "mvn install", but in other cases they may have read "copy manually".

I'm on a project now where the jar has been copied into src/main/webapp/WEB-INF/lib and a system scope in the pom to include it. Definitely bad practice, but it's not my project, it's just a throwaway demo, so it's being done that way cuz it will get thrown away. If it were not throwaway code, I'd protest a little more.
thumbnail
Anil T, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Expert Beiträge: 313 Beitrittsdatum: 14.01.12 Neueste Beiträge
Hi David,

Thanks for the detailed explanation.

I've tried with maven by adding dependency in pom.xml file and also provided portlet name for the option required-deployment-contexts in liferay-plugin-package.properties. But it was giving that Portlet A's package structure is not finding in the portlet B. I am now facing compilation issues. For my portlet A's pom.xml file it has packaging as war and changed this as jar also did not get any solution. Please let me know how to resolve this dependency.

Thanks,
Anil T
thumbnail
Andrew Jardine, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Can you share with us your POM files?
thumbnail
Anil T, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Expert Beiträge: 313 Beitrittsdatum: 14.01.12 Neueste Beiträge
Hi Andrew,

Issue has resolved.
thumbnail
Andrew Jardine, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Hi Anil,

Great to hear that you have it all resolved! When you have a minute can you share with us what you did to solve the problem? Someone else in the future might stumble upon this thread and use the same steps you did to solve their issue.
thumbnail
Anil T, geändert vor 8 Jahren.

RE: Share service classes between two portlet with Maven project (Antwort)

Expert Beiträge: 313 Beitrittsdatum: 14.01.12 Neueste Beiträge
Hi Andrew,

My projects pom.xml file has packaging element configured as "war" so my plugin is unable to create a jar file and place in the maven repository. I had changed packaging from war to jar and it got placed in the maven repository. That's all.