Is javac enough to build an OSGi package?
To build a package from source using a tool like javac, you need to provide it with a linear classpath. Unfortunately, it won't work in some situations that are perfectly legal from an OSGi perspective:
-
dependencies with embedded JARs in them;
-
the same packages that are contained in different dependencies.
Since javac doesn't understand OSGi metadata, I can't just but the dependencies in the classpath. It seems like a more subtle approach to batch processing is needed.
How is this problem solved by people using OSGi in an automated process (continuous integration)? Funnily enough, there are many resources on the internet on how to create a JAR package (metadata generation, JAR generation) assuming you have the classes / internal JAR to inject internally, but very little on how to actually get the class classes compiled.
Let's take an example: my package needs two other packages to compile, both contain Xerces as an embedded JAR, but in two different and incompatible versions. This is not a problem as only one of them exports some xerces packages, which my package in turn imports. Perhaps not a very clean situation, but something that can "finish" in an OSGi container without issue.
Now how can I compile it? I cannot put two dependencies in my classpath (the built-in Xerces JAR will not be found by javac), then I cannot flatten out (the two versions of Xerces will collide, and maybe the one that was not exported will be the first one) the solution is to create a "classpath" at the package level, not package-wide, javac is not used at all.
a source to share
Java classes are compiled by javac, which means: in order to compile a package, you must include all the bundles that you depend on in the classpath. You are correct that javac doesn't know OSGi. Therefore, you need other automation tools.
To see how to do this, you can take a look at Felix , which implements the OSGi R4 service platform.
The OSGi package is usually packaged with ant or maven which are supported by eclipse. Using eclipse the dependencies are configured in the Plugin Settings tab of the project tabs.
A good example of how to set up an OSGi project in eclipse is Sherlog
a source to share
Javac is required to build any java file into a class file. OSGI packages, like any other set of java packages, are a collection of class files and a manifest that contains several required explanatory entries within a JAR file.
If you want your package to function properly, it must have all of its dependencies. If you want to export some of your services or classes, you must also declare them in the same way. Once you understand the concepts, the special OSGI headers in the archive make sense.
a source to share