-2

I want to know why OSGi do not respect the maven dependenceis.

I want to create one app in OSGi(AEM). I want to communicate(CRUD) to the database with the help of JPA(eclipselink).

I created maven project with aem-archetype.

Added all required dependencies(of JPA) into my maven project's pom file. No errors in Eclipse, I built the project via mvn clean install and installed it into AEM(CQ5) via mvn sling:install. All good till now. No Errors.

But when I go and see my bundle in the felix console, I see that it is not Active but in Installed state.The error reported is that it could not resolve the javax.persistence package. I was puzzled, I searched and I read about it here -

You have to make sure that you place the same version in another bundle and deploy first. https://forums.adobe.com/thread/2325007

I converted JPA jar to OSGi bundle and installed in my OSGi container, and the error was gone. Great!

But why OSGi is not watching out for the dependencies I wrote in pom.xml of my maven project. Why it needs JPA strictly from OSGi bundle?

Maybe this is due to any architectural benefit, but could anyone please explain me here about this behaviour of OSGi? And why/how this feature of OSGi is useful ?

anubhs
  • 566
  • 1
  • 8
  • 26
  • if someone could explain why my question was down voted. i would take care while asking questions next time. – anubhs Jul 01 '18 at 18:32
  • "I want to know why OSGi do not respect the maven dependencies" is not a good start. Why not ask why Maven doesn't respect OSGi dependencies? ;-) – Neil Bartlett Jul 02 '18 at 14:32
  • If it helps, OSGi doesn't respect gradle, ant or sbt dependencies either. – user8723658 Jul 02 '18 at 16:00

2 Answers2

3

The <dependency> section of your Maven POM only covers your compile time dependencies. That means when you run Maven to build your project those dependencies are used to compile the source code and build your bundle. Maven itself is not aware of AEM or OSGi or any other platform or framework (e.g. Spring).

Maven just compiles your code.

You, as a developer, are responsible that all those required compile time dependencies are also available at runtime.

What we usually do is to create an AEM content package Maven module and put all of our required third party dependencies (e.g. JPA bundles) into it. This content package is then deployed by Maven so that those dependencies are also available at runtime.

Jens
  • 20,533
  • 11
  • 60
  • 86
0

Reason is: what you are adding as dependency is getting added in build path of your project and being available for your classes.When you run mvn install,it checks presence of all dependency and creates a bundle/jar for you.By default this bundle has only your project classes not other dependencies. You need to check in depfinder whether external dependencies are already there in OSGi container,if not you have to load them in OSGi container either by embedding external dependencies in your bundle with the help of maven-bundle-plugin present in pom.xml or by making a bundle of jar file(I wont recommend that)which you have done.

I hope this helps!

  • As mentioned in this answer and backed by other authors too, we should deploy our OSGi bundle separately in container. shipping it with my project isnt a good practice. https://stackoverflow.com/questions/1340483/osgi-handling-3rd-party-jars-required-by-a-bundle – anubhs Jul 02 '18 at 05:28
  • Indeed...Reason why I was not recommending is it creates an overhead to deploy bundle separately on each environment and also an extra step to setup local environment for a developer. – Sachin Arora Jul 02 '18 at 17:36
  • yeah, that is an overhead.. thanks for mentioning! :) – anubhs Jul 03 '18 at 10:14