how to use maven bundle plugin to exclude package out of bundle - osgi

i have a bundle that uses maven-jaxb2-plugin to generate all classes to target/generated/src/main/java. Then i use maven bundle plugin to create a bundle. It simply works.
Now, i want to exclude (remove) a generated package com.xxx.yyyy.common out of this bundle. So i use:
<Export-Package>
!com.xxx.yyyy.common,com.xxx.*
</Export-Package>
But after having a bundle, that package is still there inside the bundle.

I have made it works.
Basically, we need to use a combination between <Export-Package> and <Private-Package>. Tell maven bundle plugin to not export this package and this package is not a private package --> that means we dont need to keep this package in the bundle
<Export-Package>!com.xxx.yyyy.common</Export-Package>
<Private-Package>!com.xxx.yyyy.common</Private-Package>

The syntax you use, is just to exclude the package from being exported, not from being packaged. You need to use standard maven assembly instructions for excluding something from the packaged artifact. This is no goal for the maven-bundle-plugin, the maven-bundle-plugin only generates OSGi-Manifest entries from your packaged artifact.

Related

How to convert a maven jar into an OSGI bundle?

How to convert a maven jar into an OSGI bundle? For this jar file, it also has dependencies on other external jar files. I tried maven-bundle-plugin, but how to write Import-Package is a disaster for me...... Can anyone help me out?
More specifically, what I want to transform is ProvToolbox https://lucmoreau.github.io/ProvToolbox/, which has several prov packages. So I need to transform them one by one.
Adding import packages is easy using the UI from Eclipse - just open the manifest, and in the Dependencies tab, you can add them.
This explains how to transform a JAR into an OSGi bundle:
link
Here you have a perfect example to create a fat bundle with all transitiv dependecies.
https://github.com/apache/jackrabbit/blob/2.18/jackrabbit-bundle/pom.xml
Notice the following tag:
<Embed-Transitive>true</Embed-Transitive>

maven-bundle-plugin include non existing dependencies

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.

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.

Make OSGI bundle require JAR dependency

I know that when creating an OSGI Bundle I can declare that it needs other bundles to work correctly (in this situation other bundles need to export things that I will import in mentioned bundle).
But what if I need a jar file for a bundle to work?
Is it possible to write this information in MANIFEST.MF? I have the bundle and for some legacy reasons of other bundles that are used my bundle requires usage of a few jar files.
For building this bundle I use maven plugin for creating OSGI bundles (maven-bundle-plugin).
You cannot use normal JARs as dependency of a bundle. You can use only bundles (JARs with OSGI maniifest) as the dependency of a bundle.
You have the following options:
you embedd such jars into your bundle (http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html chapter titled "Embedding dependencies"). This means that that JAR will be inside your bundle, and the manifest will instruct the OSGI container to load the classes from that JAR as well.
try to find the OSGI version of your dependency jar. You can give a try to Spring EBR or to ServiceMix bundles
you create a bundle for each such jar with embedding (1. point) and then you add them as imported package or required bundle (imported package should be favoured).
I would prefer the second point, if not found, then the third.

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