What does the private-package manifest header do?

前端 未结 3 1911
忘了有多久
忘了有多久 2021-02-19 15:57

I am familiar with Import-Package and Export-Package, but this is a new one for me. What does it do?

3条回答
  •  囚心锁ツ
    2021-02-19 16:35

    If you know what static linking is then you understand Private-Package. Otherwise read on.

    Private-Package is bnd's way of telling you the packages that must be inside the jar but that are not exported. It is not an OSGi header but a bnd 'instruction'.

    The instruction defines the packages from the classpath (with wildcards) that must be included in the JAR. bnd is unique that it populates the JAR from a specification and not some directory like most build tools. The reason is that unless modules are 'designed' and their layout carefully considered they are rarely provide modular benefits.

    In general Private-Package specifies the packages holding the classes that should not be shared with other bundles, i.e. the implementation classes. Though in general they come from the corresponding project that bnd is used in it is perfectly acceptable to get them from any other JAR on the classpath.

    One use case is a library with utils. Turning a util library in a bundle is usually wreaking havoc with dependencies since utils tend to depend on lots of unrelated things; you use one tiny method and suddenly you drag in 30Mb of dependencies. bnd itself heavily uses this model with the aQute.lib* packages, packages from this library should never be exported. It is a trade of between bundle size and 'downloading the internet'. The difference is visible between the Eclipse's and Apache Felix's worlds. Apache Felix bundles are usually standalone and do not require all kinds of support and util bundles while Eclipse's bundles have a tendency to require lots of plumbing bundles. I think this difference is largely caused by Eclipse's PDE that makes it impossible to include packages from other projects unless you copy the source code which is of course a big no-no.

    In the C world they have something called static linking. After a program is linked, any unresolveds are retrieved from a library and added to the target. To a certain extent Private-Package is the same idea. There is actually a header Conditional-Package that statically links the specified packages and anything they transitively depend on (as long as they fall in the pattern).

    In the end bnd shows the expanded packages that are not exported in the Private-Package header in the manifest. After all, a manifest is supposed to describe the contents ...

    If you're the author you can remove the header with -removeheaders: Private-Package (or in maven with <_removeheaders>Private-Package

提交回复
热议问题