简体   繁体   中英

AEM OSGI 3rd party dependency, bundle not Active

I'm working off an AEM project, I'm new to AEM btw. I'm trying to use a couple of 3rd party non-OSGI libraries, like Dozer and Spring. I'm really struggling with this, it is a pain in the ass, maybe it is because OSGI is kind of old school. My bundles get installed, but not Active in the OSGI console, as they complain about missing packages.

I want to do step by step. First I want to make work my bundles with just Dozer and nothing else. This is my pom dependencies:

<dependency>
   <groupId>net.sf.dozer</groupId>
   <artifactId>dozer-osgi</artifactId>
   <version>5.5.1</version>
</dependency>

Here is my maven-bundle-plugin configuration:

<plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>4.1.0</version>
                <configuration>
                    <instructions>
                        <Export-Package>somepackages*</Export-Package>
                        <Private-Package/>
                        <Import-Package>*</Import-Package>
                        <Sling-Bundle-Resources>/META-INF</Sling-Bundle-Resources>
                    </instructions>
                </configuration>
            </plugin>

As you can see, I'm using dozer-osgi already. I'm not sure why it doesn't pick it up. It complains about this:

org.dozer,version=[5.5,6) -- Cannot be resolved
org.dozer.loader.api,version=[5.5,6) -- Cannot be resolved

First, I don't understand why it says [5.5,6), because I'm telling it to use 5.5.1. Second, I'm using dozer-osgi already, I believe it should pick it up automatically.

I tried also using:

<Embed-Dependency>dozer-osgi</Embed-Dependency>

Whit dozer-osgi added, things start to get better, and more complex at the same time. It seems like now it loads dozer, but starts complaining about transitive dependencies:

android.dalvik -- Cannot be resolved
dalvik.system -- Cannot be resolved
javassist.util.proxy -- Cannot be resolved
org.apache.commons.beanutils,version=[1.9,2) -- Cannot be resolved
org.apache.commons.beanutils.converters,version=[1.9,2) -- Cannot be resolved

The beanutils utils is easy to fix. I just need to add the regarding dependencies like this:

    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>

and add "commons-beanutils" here:

<Embed-Dependency>dozer-osgi,commons-beanutils</Embed-Dependency>

Now, it complains about:

android.dalvik -- Cannot be resolved
dalvik.system -- Cannot be resolved
javassist.util.proxy -- Cannot be resolved
org.hibernate.proxy -- Cannot be resolved

The hibernate dependency error is new. I thought beanutils depended on hibernate, but not, is Dozer the one depending on it.

I tried adding hibernate, like this:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.9.Final</version>
    </dependency>


<Embed-Dependency>dozer-osgi,commons-beanutils,hibernate-core</Embed-Dependency>

And so on, it is like a look, it gets worse and worse everytime I try to add a transitive dependency. I also tried this, only keeping dozer-osgi in my dependencies:

<Embed-Dependency>dozer-osgi</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>

Result:

android.dalvik -- Cannot be resolved
dalvik.system -- Cannot be resolved
javassist.util.proxy -- Cannot be resolved
org.hibernate.proxy -- Cannot be resolved
org.apache.commons.beanutils -- Cannot be resolved
org.apache.commons.beanutils.converters -- Cannot be resolved

It is like if Embed-Transitive doesn't work at all.

What is the best way to work with 3rd party libraries when using Maven and AEM? In my case, Maven is using to install the bundles in my AEM instance.

In OSGi you have to distinguish between the build and the runtime. At build time your initial approach was totally fine.

You use dozer and the maven-bundle-plugin creates suitable Import-Package statements. You can check these by looking into the Manifest of the jar. The rules for package imports result in a range of version that should work with your code. So [5.5,6) is exactly the expected import range.

Now to runtime. Here you have to supply all dependencies of your code (including the transitive ones) as bundles in AEM (or more generally in the OSGi runtime). So you also need to install dozer OSGi as bundle. If this complains again then you also need to install its dependencies.

This is the default approach and normally totally fine.

Now if you want to make your bundle standalone in respect to having no additional runtime depenencies then you can try to embed all dependencies. Then you only need to install your bundle.

Be aware though that this is not easy. If your code uses some of the embedded classes in its own API then you get into lots of problems with embedding. So if your are not very experienced then better go the way of installing all dependencies as bundles.

To simplify the install process you can create a content package that contains all needed bundles.

You also have to check that all dependencies actually are bundles. In some cases the normal maven dependencies are not suitable. In this case have a look at apache servicemix bundles. It is a project that creates OSGi bundles for popular dependencies and deploys these to maven central.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM