Foren

How to add external lib dependency in OSGi module

thumbnail
Jitendra Rajput, geändert vor 7 Jahren.

How to add external lib dependency in OSGi module

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
Hi All,

I have created one mvc portlet and from that portlet i would like to refer my custom jar file. To refer custom jar from my module first i uploaded my custom jar to maven local repository using mvn install command.

And added dependency for custom jar in build.gradle like below.

compileOnly group: "com.custom.common.service", name: "custom-service", version: "1.0"


I have also added mavenLocal() repository in build.gradle file so that it can refer to local repo for my custom jar.

I am getting below error in server logs and my module is not getting started. Am i missing anything here ?
org.osgi.framework.BundleException: Could not resolve module: custom.search.portlet [1027]_  Unresolved requirement: Import-Package: com.custom.common.service.bean_ [Sanitized]
        at org.eclipse.osgi.container.Module.start(Module.java:429)
        at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:402)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1252)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1224)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:512)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:361)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:313) 


Any pointer on this would be really helpful..
Thanks !
Jitendra
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
thumbnail
Eric COQUELIN, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Expert Beiträge: 254 Beitrittsdatum: 03.11.13 Neueste Beiträge
Hi David,

Thank you for replying but it looks like more complex for me as well.

Indeed, I have the same issue in my environement using Eclipse IDE (3.0.1). I have setup a workspace in which I have created two modules:
  • one service builder (which comes in fact in 2 modules : API + Impl)
  • A single portlet (very simple)


From my single portlet, I would like to call the ***Util class available in my service API. But... How to tell gradle where's the API?

There are several questions for which I don't get any simple answer (or answer at all)
  • where is defined the "group" name of my new module? At least by default, there is nothing in the configuration files. Using some tricks, I got an error saying test.modules (where "test" is my workspace name). I don't know how to change that
  • When I will finally have found the group name I need, how will I point to it from my other module. Indeed, when you run a "gradle build", it doesn't generate anything in any repositories... If you run "gradle deploy", then it generates the jar file in your "bundles/osgi" directory in the Liferay Workspace directory. But, gradle is configured by default to look at "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public" only (as per settings.gradle in the root directory of the workspace)


My feeling is that more configuration is required and to be honnest I would have expected it to be automatically generated. Unless I misses something. Can you provide more help?

Thank you
thumbnail
Eric COQUELIN, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Expert Beiträge: 254 Beitrittsdatum: 03.11.13 Neueste Beiträge
I would like just to add that you can refer another module in the same workspace using the following syntax

compileOnly project(":modules:projet:sub-project-api")

where project is a "service builder" project type and sub-project-api being the api. Should work with any type of project as long as it is in the same workspace.

This being said, it doesn't answer the questions I have written:
  • how to customize group and name of a gradle projet (tried "group=..." but doesn't work)
  • how to deploy the jars to the gradle repository (gradle tasks doesn't suggest any command which could do that)


Thank you in advance for any help.
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
Eric COQUELIN:
This being said, it doesn't answer the questions I have written:
  • how to customize group and name of a gradle projet (tried "group=..." but doesn't work)
  • how to deploy the jars to the gradle repository (gradle tasks doesn't suggest any command which could do that)


There's no trick to it; your dependency artifacts have to be published into a maven repository. Maybe you get it into maven central if it is a popular and well used library (although I don't really know what their acceptance criteria is), or maybe you publish it to an enterprise nexus server (or artifactory or ..., many different players here).

Or maybe you just have to install it into your local maven repo:

mvn install:install-file –Dfile=path.to.file –DgroupId=com.example 
    –DartifactId=my-library –Dversion=1.0.0.0 –Dpackaging=jar 
    –Dsources=src.jar –Djavadoc=docs.jar


In all of these cases, you can now use the group:artifact:version format for accessing your jar in your build.gradle file (as long as the repo is declared correctly).
thumbnail
Eric COQUELIN, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Expert Beiträge: 254 Beitrittsdatum: 03.11.13 Neueste Beiträge
David,

Thank you for replying but I'm looking for a gradle answer emoticon

As from Liferay 7, Liferay encourages switching from Maven to Gradle. Thus, I don't see why Maven would be required.

Moreover, note that by default, at least for projects generated from the IDE, gradle configuration doesn't point to Maven repository. As a result, all the bundles are being downloaded and moved to the specific gradle directory. And finally, I can't imagine that you can't generate artifacts with Gradle to make them available to the gradle repository. Then, my question remains open.

Same for group and name. Using gradle, how can I achieve that?

Thanks,

Eric
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
Well I think then that you need to check out how to publish artifacts: https://docs.gradle.org/2.14/userguide/artifact_management.html and more specifically how to use the maven plugin for publishing: https://docs.gradle.org/2.14/userguide/maven_plugin.html#uploading_to_maven_repositories.

Liferay encourages switching from maven to gradle for the build tool, this is true. However, both tools still use the same repository format. So an artifact installed via maven command line is available to gradle. I'm just not aware of an actual gradle command line that can publish the artifact w/o having some build.gradle help; the maven command line option works like a charm.

I have to repeat, though, that the maven dependency resolution (repos) and the gradle dependency resolution (repos) are the exact same process leveraging the exact same sources.

Also, I think the general impression at Liferay is that portlet developers are typically not creating artifacts for repos. The whole Liferay Workspace concept keeps all of the dependent stuff for a Liferay deploy together in one project so no publishing is necessary. In this view, publishing is only for shared project-neutral jars that have no dependencies on any Liferay stuff.
thumbnail
Jack Bakker, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Master Beiträge: 978 Beitrittsdatum: 03.01.10 Neueste Beiträge
Hey David, as a fellow IntelliJ user, I am curious if you've explored whatever it is that Greg Amerson is writing in his comment at May 31, 2016 8:50 AM
in the thread entitled: "RE: How to reference "pure" bnd projects in a Liferay module project?"
https://web.liferay.com/community/forums/-/message_boards/message/74725387
The following line is particularly attractive to me:
"So as you are developing this, its a nice workflow. As you make changes to test.api and test.impl those jars are automatically getting published into local maven repo after every save."
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
Greg's talking Liferay IDE there, not intellij.

The black magic involved there is really nothing more than Eclipse' auto-publish on change; I don't think it is publishing into the local maven repo, it's just eclipse republishing artifacts as the changes are saved.

And after having used it a few times I can confirm it sometimes works but, as discussed in separate threads, sometimes felix gets confused and you have to restart the container.
thumbnail
Jack Bakker, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Master Beiträge: 978 Beitrittsdatum: 03.01.10 Neueste Beiträge
David H Nebinger:
Greg's talking Liferay IDE there, not intellij.

Yep, that's why they call it the IDE forum. Good to point this out to readers here in the Developer forum.
David H Nebinger:

The black magic involved there is really nothing more than Eclipse' auto-publish on change; I don't think it is publishing into the local maven repo, it's just eclipse republishing artifacts as the changes are saved.
And after having used it a few times I can confirm it sometimes works but, as discussed in separate threads, sometimes felix gets confused and you have to restart the container.

k
thumbnail
Jitendra Rajput, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
Not sure if it is correct approach or not but i simply moved jar into lib/ext and and updated below properties in portal-ext.

module.framework.system.packages.extra=
module.framework.web.generator.headers.DynamicImport-Package=
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
It is not the recommended way. For some dependencies, the transient dependencies you'd have to drop into lib/ext tends to grow out of control until you have all jars in lib/ext and no ability to version dependencies in the individual OSGi jars.

I would only do this if there were limited but especially no transient dependencies and this jar is an actual dependency of pretty much every module you are creating. A very limited use case indeed.
Piyush Purohit, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

New Member Beiträge: 3 Beitrittsdatum: 16.03.17 Neueste Beiträge
Hi David,
Can you explain how to do connectivity with MSSQL in module project(I'm using Liferay 7.0),
I've done all the things which you mentioned in your blog
https://web.liferay.com/web/user.26526/blog/-/blogs/osgi-module-dependencies

But I'm getting below error all the time -
com.microsoft.sqlserver.jdbc.SQLServerDriver cannot be found by Test_1.0.0
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
My blog post doesn't do anything directly with any JDBC drivers, and neither should yours.

My spring xml files use portal properties to instantiate a connection so no direct reference to any JDBC driver is necessary.









Come meet me at the 2017 LSNA!
Piyush Purohit, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

New Member Beiträge: 3 Beitrittsdatum: 16.03.17 Neueste Beiträge
Thanks for the reply David.
Could you please explain how to do the setup one you have mentioned in your reply
"My spring xml files use portal properties to instantiate a connection so no direct reference to any JDBC driver is necessary"
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
thumbnail
Praveen Mistry, geändert vor 7 Jahren.

RE: How to add external lib dependency in OSGi module

Junior Member Beiträge: 27 Beitrittsdatum: 23.03.17 Neueste Beiträge
Hi David,

I have same issues, Can you please have a look Link

Thank You.
Harsh Kanakhara, geändert vor 6 Jahren.

RE: How to add external lib dependency in OSGi module

Junior Member Beiträge: 74 Beitrittsdatum: 06.04.17 Neueste Beiträge
Hi
Jitendra Rajput,

I have created ReST Client in Spring using maven build tool. I am packaging my rest client module and installing it in maven local repository. I am mentioning my dependency in build.gradle using group:name:version format. During compile time I can access all the utility classes but when my portlet gets deployed it is just getting INSTALLED and not getting STARTED.

Getting Error : Unresolved Module.

Any help would be appreciated emoticon

Regards,
Harsh Kanakhara.
Harsh Kanakhara, geändert vor 6 Jahren.

RE: How to add external lib dependency in OSGi module

Junior Member Beiträge: 74 Beitrittsdatum: 06.04.17 Neueste Beiträge
Everyone who are facing this same problem. I got it fixed by mentioning jar name in includeResources property inside bnd file.

Regards,
Harsh Kanakhara.
thumbnail
Massimo Bevilacqua, geändert vor 6 Jahren.

RE: How to add external lib dependency in OSGi module

Regular Member Beiträge: 210 Beitrittsdatum: 27.12.16 Neueste Beiträge
Me too...I needed jsoup in my portlet and I added this to bnd.bnd file

Include-Resource:\
@jsoup-1.10.3.jar