maven-bundle-plugin include non existing dependencies - maven

I have a stange problem with the "maven-bundle-plugin" in a Eclipse Plugin Project.
When I compile the project, i see on the Manifest.xml, on the "Import-Package" section a lot of java packages imports for a lot of packages that doesn't exists on my classpath, for Example:
Import-Package:
COM.newmonics.PercClassLoader,
android.os,
bitronix.tm,
bitronix.tm.jndi,
bitronix.tm.resource.common,
bitronix.tm.resource.jdbc,
bitronix.tm.resource.jms,
bsh,
com.arjuna.ats.arjuna.common,
com.arjuna.ats.arjuna.recovery,
com.arjuna.ats.internal.jdbc,
com.arjuna.ats.internal.jta.recovery.arjunacore,
com.arjuna.ats.jbossatx.jta,
All of this packages don't exists on my project or in my dependencies, but maven-bundle-plugin add it to the Manifest, and I have no idea why. Any idea or suggestion? Thanks.

The bundle plugin generates Import-Package for the packages that are referred to by your project. These are the packages that should be exported by other bundles, so that your bundle can use them runtime. My first guess is that your classes refer to these packages, so check your source code first.
If your project really doesn't refer to those packages, please check if your pom.xml has specified these packages in an <Import-Package> directive in the plugin configuration. Perhaps it was copy/pasted from another project?
It could also be that you have embedded dependencies in your bundle jar that refer to the packages. To find out, you can unpack your jar (e.g. using the rjar tool) and grep the class files recursively for e.g. bitronix/tm.

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.

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

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!

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.

When including Sling Dynamic Include in our our maven pom, it is not installing the jar

We have a bit of a problem installing SDI via maven packaging.
We followed the install guide found at:
https://github.com/Cognifide/Sling-Dynamic-Include
We have placed the dependency into our core bundle in our project, and it appears as though sling-dynamic-include-1.0.0.jar is included in our core bundle's jar files, and the rest seem to install, however, no configurations appear in the system/console interface, and it doesn't appear as though there is any reference to it inside the console otherwise.
We have checked the packages through /system/console/depfinder, and can't find any reference to it.
It's fairly safe to say that the jar file is not being loaded into CQ5.
If we manually install the jar, everything is there, however, we need to integrate it into mvn.
I am sure we are missing something basic, and any help would be appreciated.
Adding to the maven dependencies is not enough.
You have to copy the jar into the install folder of your crx package.
If you use maven-vault-plugin, you can add it as embedded element.
<configuration>
<embeddedTarget>/apps/your-app/install</embeddedTarget>
<embeddeds>
<embedded>
<groupId>com.cognifide.cq</groupId>
<artifactId>sling-dynamic-include</artifactId>
<filter>true</filter>
</embedded>
</embeddeds>
</configuration>
It will copy it to the install folder.

how can maven-bundle-plugin find packages in Import-Package

Is there any relationship between the bundles mentioned in the <dependencies> tag and the <Import-Package> tag? I mean if we don't define dependency, then maven-bundle-plugin can not find the packages in <Import-Package> tag?
Maven uses the jars listed in the dependencies section to create a classpath. This class path is used by bnd (the engine in the maven bundle plugin) to analyze what your code is referring to through byte code analysis.
maven dependency -> classpath -> bnd analysis -> import statements
Import-Package is used to refine the list of package imports which the maven bundle plugin generates automatically for you. For example, you could declare some imports optional, or add imports for classes which are only accessed by reflection. In most cases it shouldn't be necessary to specify anything at all for Import-Package since the default is * - that is, any external packages referenced in your bytecode will be imported.
The dependencies section provides the pool of bundles used by the compiler to generate the bytecode, and bnd (which is what the bundle plugin under the covers) to improve the package imports. For example, it will add version ranges based on the exported versions in a providing bundle in the dependencies list (or in transitive dependencies).

Resources