Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

java - Simple OSGi application with dependencies inside bundle

Simple OSGi application with dependencies inside bundle.

I am playing with karaf. I started by writing a simple rest application. https://github.com/YaroslavTir/osgi-jersey-hibertate/tree/stackoverflow/examples/karaf-rest-core

I took karaf examples as backbond and it was quite simple to run my first bundle with rest endpoints, but then I faced an issue when I added maven dependency. I added guava as an example and got an exception when installing the bundle in karaf

 <dependencies>
       ...
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <configuration>
                    <instructions>
                        <Export-Package>org.apache.karaf.core.rest.blueprint</Export-Package>
                        <Import-Package>
                            *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>


> git clone https://github.com/YaroslavTir/osgi-jersey-hibertate
> mvn clean install 
> feature:repo-add mvn:org.apache.karaf.examples/karaf-rest-core-features/LATEST/xml
> feature:install karaf-rest-core-blueprint

Error executing command: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=karaf-rest-core-blueprint; type=karaf.feature; version="[4.3.1.SNAPSHOT,4.3.1.SNAPSHOT]"; filter:="(&(osgi.identity=karaf-rest-core-blueprint)(type=karaf.feature)(version>=4.3.1.SNAPSHOT)(version<=4.3.1.SNAPSHOT))" [caused by: Unable to resolve karaf-rest-core-blueprint/4.3.1.SNAPSHOT: missing requirement [karaf-rest-core-blueprint/4.3.1.SNAPSHOT] osgi.identity; osgi.identity=org.apache.karaf.examples.karaf-rest-core-blueprint; type=osgi.bundle; version="[4.3.1.SNAPSHOT,4.3.1.SNAPSHOT]"; resolution:=mandatory [caused by: Unable to resolve org.apache.karaf.examples.karaf-rest-core-blueprint/4.3.1.SNAPSHOT: missing requirement [org.apache.karaf.examples.karaf-rest-core-blueprint/4.3.1.SNAPSHOT] osgi.wiring.package; filter:="(&(osgi.wiring.package=com.google.common.collect)(version>=29.0.0)(!(version>=30.0.0)))"]]

I read somewhere that org.apache.felix.maven-bundle-plugin has a bug and add optional dependency to the bundle, and this looks like true, as I can see in MANIFEST.MF/Import-Package dependncy like com.google.appengine.api that should not be there. :

   <dependencies>
       ...
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <configuration>
                    <instructions>
                        <Export-Package>org.apache.karaf.core.rest.blueprint</Export-Package>
                        <Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <Embed-Directory>target/dependency</Embed-Directory>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

I know that should be super simple, and I just miss some small but important detail.

question from:https://stackoverflow.com/questions/65884569/simple-osgi-application-with-dependencies-inside-bundle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your error means that your bundle karaf-rest-core-blueprint has a dependency on com.google.common.collect and this dependency can't be resolved in the container.

In other words, you did'nt install guava into Karaf.

$ bundle:install -s mvn:com.google.guava/failureaccess/1.0.1
$ bundle:install -s mvn:com.google.guava/guava-29.0-jre

In a real app, you should create a feature which installs all your dependencies/bundles.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...