from Maven2 to OSGI bundle - maven-2

I want to convert a Maven2 project in a OSGI bundle. I am trying to use maven-bundle-plugin that should create the manifest according to maven dependencies. The problem is that i can't manage to let it work. When I install and run the bundle generated, there is always an error like:
org.osgi.framework.BundleException: The bundle "My_OSGI_Bundle" could not be resolved. Reason: Missing Constraint: Import-Package: com.sun.javadoc;
Each time I add manually the missing constraint among the of the maven-bundle-plugin. But there is always another missing constraint. Now I'm stuck on com.sun.javadoc that I can't add in any way. Anyway... I can't add all the packages manually! Have you any better idea, please? Thank you
===UPDATE===
without adding anything in the export tag, the first error the run of bundle gives to me is:
org.osgi.framework.BundleException: The bundle "Partes_0.1.0.SNAPSHOT [55]" could not be resolved.
Reason:Missing Constraint: Import-Package: com.eviware.soapui.config; version="0.0.0" at
org.eclipse.osgi.framework.internal.core.AbstractBundle.getResolverError(AbstractBundle.java:1327) at
org.eclipse.osgi.framework.internal.core.AbstractBundle.getResolutionFailureException(AbstractBundle.java:1311) at ..

This error means that something inside your bundle depends on com.sun.javadoc, e.g. as a direct result of using that API from your code. You need to find a bundle that exports that API.
You might now state that you don't actually use com.sun.javadoc. I believe you have probably dragged in a whole load of extra third-party dependencies into your bundle... the phrase "each time I manually added the missing constraint" sets off alarm bells! So the dependency probably exists in something that shouldn't be in your bundle at all.
The best thing to do is go back to the beginning before you started adding random bits to your bundle in an attempt to make the problem go away. That is, make sure your bundle only contains your code. Then find out what the missing import is, and find a bundle that exports it.

Related

Intellij How to ignore a file with errors?

In IntelliJ, I have a couple of separate applications which dont really depend on each other (and sometimes associated tests). Sometimes one of them has an error (cant find a package to import or so) and I really dont have time to fix it because I need to work on something else, I have no other choice but completely commenting out the offending file. Can I tell Intellij To please just ignore a certain file when I want to execute another application?
You should move the separate applications to separate modules. When you run an application, it will compile only the module containing this application and its dependencies, and will not compile the other modules.
Under "Project Settings" -> "Module", exclude the package you don't want to run:

Adding a directory path under IntelliJ 12 claims Library 'LibraryName' is not used and has no effect

I maintain an IntelliJ plugin (Codename one) and we need to control the users classpath. I'm adding a classpath either via the plugin or manually by going to here:
And pressing the + sign where I pick Java:
Then choose classes:
This seems to work OK:
But the completion and other such functionality doesn't work and when I go back the entry is disabled and I get this error message:
This doesn't really tell me anything?
A workaround is to open the .iml file in a text editor, and add the following to the orderEntry list:
<orderEntry type="library" scope="PROVIDED" name="LibraryName" level="project" />
Unfortunately this isn't very practical and it only solves some of the problems I'm experiencing. Any direction or hint would be appreciated here.
Edit: Adding screenshot of preferences UI:
Second Edit: Screenshot of the module section
The answer from kukido is good, for additional details though check out this: http://devnet.jetbrains.com/message/5509300
Essentially what I want is to still have my module but add a dependency programmatically which can be done by:
ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
model.addLibraryEntry(library);
model.commit();
This doesn't really tell me anything?
IntelliJ is trying to warn you that you have created a library, but it is not referenced anywhere in the project. It is gently asking "Did you mean to add this library as a dependency?".
The entry is not disabled. It is greyed out to show that it is not referenced in any module as a dependency. Once you add the library to the dependencies list, the color will change.
It is a two-step process:
Create a library
Reference new library in a module
Code completion is not working because your module is not aware of the library classes.
Module dependencies
Non-referenced and referenced libraries

Eclipse RCP: Dependencies correct (?) but get a NoClassDefFoundError

I have a RCP project where I cannot fix a NoClassDefFoundError: One plugin depends on another plugin. The plugin-dependencies are set in the manifest, packages exported, and there is no error at compile time. Both plugins are in the product dependencies and visible in the installation details of the product.
But when I run the application I get a java.lang.NoClassDefFoundError when the one plugin wants to use a class from the other plugin.
Any hints how to find the reason for this are greatly appreciated.
Thanks,
Michael
I found the problem: I created the plugin which could not be loaded from an existing Java project. And somehow I deleted the "." in the entry Bundle-classpath in the plugin manifest (the plugin has some jars which -> so lib/xyz.jar was in the Bundle-classpath entry but not the ".").
For the class-loader of the bundle the "." means to search for classes from the root path of the bundle (or something like that), so it could not find the classes. However, there were no errors in the IDE so it was hard to find.
Is the configuration for running the application correct i.e. all dependencies are also put in the running configuration?

Embedding JARs into the OSGi bundle with maven-bundle-plugin

I’m trying to embed some JARs into single OSGi bundle using the feature of maven-bundle-plugin
The thing that worries me is that all packages of embedded JARs are put into the Import-Package header of the generated MANIFEST.MF.
If I specify explicitly to use only the packages I need, like in the following snippet:
Import-Package: org.osgi.framework
The build fails with BND error (unresolved references).
So, the question here is how can I build the bundle with embedded JARs with "Import-Package" header I need?
All the packages that are imported in your classes will be imported by bnd. Perhaps you do not want those packages imported because you know that at runtime you won't be needing them. If you cannot stop bnd from importing them, you can make them optional so that your bundle will still resolve even if they are not supplied by another bundle (at wire time). Try to add this:
<Import-Package>*;resolution:=optional<Import-Package>
To your maven bnd configuration in maven.
One possible reason why you are seeing "unexpected" packages in Import-Package header is the following:
A general good practice that supports collaboration model in OSGi is to import all packages that you export -- see this blog post by Peter Kriens for detailed explanation why. Bnd (and hence also maven-bundle-plugin) follows this practice by default and automatically imports all exported packages. Therefore you should first check your Export-Package header and make sure that you export only the packages you want.
Also if you want to export packages from the embedded dependencies then you should be careful to avoid duplication inside your bundle -- see section Embed-Dependency and Export-Package of the maven-bundle-plugin documentation.
You should use Bundle-ClassPath if you want to make classes available inside a bundle that contains JARs e.g.
Bundle-ClassPath: foo.jar,other.jar
Import-Package: org.osgi.framework,org.other.imported
You'll need to list the classes that foo.jar and other.jar import/use, but you won't need to list any of the packages in foo.jar or other.jar unless you're actually exporting them.
You can remove some packages from import-package scope when you embed a JAR into your bundle:
<Import-Package>![package_name9]<Import-Package>
inside pom.xml or if you use external *.bnd files:
Import-Package: ![package_name]

Access project directory from eclipse plugin

This is a question already asked by someone:
How can an Eclipse plugin access the project directory?
The answer was good: using ResourcesPlugin.
But for some reason, under no circumstances will my Ganymede eclipse recognise the existence of org.eclipse.core.resources package which contains ResourcesPlugin.
Why is that?
This is usually something to do with the Manifest:
is the package imported (Import-Package), or the bundle required (Require-Bundle) in your plugin.
(sanity check) is the package exported by the source bundle (Export-Bundle).
If both of these check out, then I would look at the target platform. A typical base install for RCP only does not include the resources bundle.
(*) You might have specified a version constraint in the Manifest.MF for the dependency. And the available version is different. If so, remove the constraint or just click the Match in the Properties of the depedency
(*) Check your target platform and verify whether this org.eclipse.core.resources plugin exists
(*) If the error still persist, go to the Plug-ins tab in the launch configuration of your RCP. Check whether the o.e.c.resources is added. The Validate Plug-ins buttons should be helpful to determine any issues
Add the bundle org.eclipse.core.resources in your plugin dependencies. That should work just fine.