Why third party dependency is required exclusively from OSGi container even if I have it in my maven dependencies? - maven

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 ?

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.

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!

Related

Eclipse: How to include dependencies in osgi bundle export?

Using Eclipse, I have a (sort of) working OSGI bundle. It uses Maven to pull a lot of dependencies.
If I do right-click > Run As "Maven Build" and select the "package" as the goal, I end up with a jar with all my dependencies (good), but if I do "Export > OSGI Bundle" the dependencies are missing.
The issue is that I have another project (WebSphere Liberty Feature Project) that includes the bundle, but when it pulls it, it is also missing dependencies, so the resulting .esa file also misses the dependencies.
Is there a way to have eclipse process the dependencies so I don't have to manually package it outside eclipse or write a maven project exclusively for the purpose?
Thank you!
I was having the same issue when I came across your post.
I assumed that the jars would be included in the exported jar and be found at runtime.
Originally, I had created a seperate "lib" directory and added the libraries to it, but they would not be included when exporting either as Bundle export or Liberty feature export (ESA)
I solved the problem first be using the "Java Archive into OSGi Bundle" import wizard.
You can select a jar dependency and add it to you bundle of your choice with the wizard. What I noticed when I used this, is that the jars were added to the "BundleContent" folder in the chosen bundle.
As I had a number of libraries to include, I simply moved them all to the "BundleContent" folder, updated the build time and runtime classpaths and then when exporting, the dependencies were all included and at runtime, the classes could then be found when they previously were not.
In your POM, have you used maven-bundle-plugin and its usage-details for creating a bundle. If not, you can use it to define creation of your bundle and can also define dependencies to be embedded when the bundle is created.

no osgi ready dependencies

Currently I'm working with osgi and karaf.
My problem is the no "osgi ready" dependencies , which means a jar that is not ready to be deployed as a bundle into karaf for example.
I tried two solutions in order to deal with this kind of problems :
I tried to to use "Embed-Dependency" which will include the jar
dependency with the project... I don't think this could be a solution
because when I try to embed the jar , it will ask me to include other
jars that the first jar depend on , and so on ..
I tried to convert the no "osgi ready" jars into bundles using bnd tool or from "Plug-in from Existing JAR Archive" from eclipse project.
And this led to the same result , each jar will call another jar that it depend on it..
I am not sure if I'm doing it the wrong way or what is the problem exactly.
Any tips how to deal with no osgi ready dependencies ?
The simplest way to start is to use the wrap: protocol to auto create a jar. Behind the scenes it uses bnd to create a bundle on the fly. Simply prepend wrap: to the mvn url of the jar.
When you try to install the jar using bundle:install -s wrap:mvn:... karaf will tell you which imported packages are missing. Install jars that provide these packages in the same way. The pom of the jar can give you a hint what is missing.
This can mean to install lots of jars if your initial jars has lots of dependencies.
Once you have a list of jars that are installable together you can either create a feature using wrap protocol or you can make bundles from the individual at build time.
In any case you should take a look are the servicemix bundles. It provides OSGi ready bundles for many libraries.

JBoss Fuse vs. standard Maven dependency

I'm just learning OSGi, JBoss Fuse (6.1) and Karaf. How can I use a standard (not bundle) Maven dependencies without changing them?
I have a simple Maven bundle project. It depends on some third party libraries. It uses them via its blueprint.xml . I understand if the manifest.mf marks packages in the Import-Package entry then there have to be installed bundle with Export-Package in its manifest.mf. In my case if I install these standard Maven projects they won't export the required packages. So I've got the "Unresolved constraint in bundle" error message during the installation. Is the Maven Bundle or Shade plugin able to solve this issue? Or if they can't which is the most elegant way to resolve the dependencies?
Somewhere I've read the Jboss Fuse is able to resolve dependencies from the Maven repository. Can I use this mechanism somehow?
Thank you!
This may be a bit late now, but you can do that using features.
<features>
<feature name="wrap_features" version="1.0">
<bundle>mvn:org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1</bundle>
<bundle>wrap:mvn:org.jdbi/jdbi/2.70</bundle>
<bundle>wrap:mvn:com.microsoft/sqljdbc4/4.2</bundle>
<bundle>wrap:mvn:org.springframework/spring-web/4.2.4.RELEASE</bundle>
<bundle>wrap:mvn:org.springframework.security/spring-security-core/4.0.3.RELEASE</bundle>
<bundle>mvn:com.doi.ws/mssql-fragment/1.0.0</bundle>
<bundle>mvn:com.doi.ws/mssql-impl/1.0.0</bundle>
<bundle>mvn:com.doi.ws/doi-services/1.0</bundle>
</feature>
</features>
You install the features before deploying your project to the fuse server using the features command
Meanwhile I found an almost good solution: with wrap: namespace/prefix Fuse automatically generates a bundle during installation:
osgi:install -s wrap:mvn:cglib/cglib/2.2.2
But I still have to install each dependency manually. Is there any way to automatize the installation of dependencies?
Thx!
Use Bundle-ClassPath manifest header.
Make sure all your maven dependencies are available in the final jar file under a single directory called 'lib'.( This can be achieved using maven resources plugin that copies maven dependencies in any output directory).
Use maven-bundle-plugin and customize the bundle manifest Bundle-ClassPath attribute with its element. Add a path to lib folder so that all the plain vanilla jars are available as a part of bundle classpath.

OSGI Bundle vs jar dependency

I'm trying to understand the difference between the following
<dependency>
<groupId>com.myspace.order</groupId>
<artifactId>dal</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
AND
<dependency>
<groupId>com.myspace.order</groupId>
<artifactId>dal</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>bundle</type>
</dependency>
The dal artifact itself has packaging specified as bundle as:
<packaging>bundle</packaging>
Now when I deploy the dal artifact, I see it published in the repo as a jar (with a manifest within it). In this case, what should my dependency on dal be. Should it be of type bundle or jar? If I am doing OSGI, I assume way would be to have the type specified as bundle. Is this correct? Or, can I just have a jar dependency here?
When you declare a dependency in Maven, you can only depend on a normal Jar, not a bundle, because Maven does not recognize the OSGi environment restrictions.
See this question:
Why can't maven find an osgi bundle dependency?
At the time you compile your project, you don't need to worry (but should!) about the OSGi environment yet... for example, it will not complain if you try to use packages not exported by the bundle you're depending upon....
When you try to deploy your bundle within a OSGi container, if you correctly declared your dependencies on the 'dal' packages you use, including of course the version (which usually you should leave for the maven-bundle-plugin to do for you based on your POM), it will only be resolved if there's a bundle within the container which exports the required packages in the right version (or version range).
Considering that 'dal' seems to be a bundle already, you just have to make sure to deploy the your bundle and 'dal' together and everything will work fine.
However, if you by mistake added a dependency on a private package of 'dal', although Maven will happily compile it for you, when you thrown it in OSGi you will be greeted by a nasty wiring exception :)
Notice that a bundle is just a normal jar which contains OSGi metadata in the manifest (Bundle-SymbolicName, Bundle-Version etc). So if you don't use OSGi, a bundle will work as any other jar.
But anyway, if you want some more info, check this question:
What is the meaning of type "bundle" in a maven dependency?

POM file for integrating Grails with Maven, with dependencies in BuildConfig.groovy

I've got a Grails application as part of a larger Java source bas that's all built with Maven. I'd like to include the Grails app in the Maven continuous integration, nightly builds, etc.
(Ideally I'd also like it to be part of a Maven parent-project hierarchy, so I could pick up live code changes from other projects without having to install them into my local repository. I would also like a pony.)
I tried running "grails create-pom", setting "pom true" in BuildConfig.groovy, and using the POM to manage my dependencies. However, this left me stuck in org.xml.sax.Locator classloader hell. So I went back to using BuildConfig.groovy for my dependencies.
I understand that "pom true" is a recent (Grails 2.1.0) feature, and it should be possible to integrate Grails into a Maven build while still managing dependencies in BuildConfig.groovy. Problem is, I can't find any documentation on what should go into the POM in that case. What do I put in my POM?

Resources