9

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?

Sudoer
  • 451
  • 2
  • 5
  • 13

1 Answers1

14

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?

Community
  • 1
  • 1
Renato
  • 12,940
  • 3
  • 54
  • 85
  • "for example, it will not complain if you try to use packages not exported by the bundle you're depending upon...." Does this mean OSGI dependencies are enforced (and detected) at runtime? I was thinking the export-package was verified at compile time. – Sudoer Feb 19 '13 at 17:18
  • 1
    The dependencies will be RESOLVED when you INSTALL the bundle in the OSGi container. As OSGi is a dynamic environment, you may install a bundle at any time... If the bundle gets to the RESOLVED state, then it may be STARTED. In other words, installation is a kind of dynamic compiling... if the bundle passes that, it is guaranteed that at the time of installation, all imports were satisfied, so the bundle can get started. – Renato Feb 19 '13 at 22:14
  • 1
    There's no way OSGi can resolve your bundle dependencies before you actually try to install it because that will depend on the set of bundles available at the time of installation. Maven, on the other hand, uses the normal Java compilation procedure, ignoring OSGi visibility rules... that's the main difference. – Renato Feb 19 '13 at 22:17