问题
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?
回答1:
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?
来源:https://stackoverflow.com/questions/14913615/osgi-bundle-vs-jar-dependency