OSGI bundle dependency issue - maven

I have got the project for migration. Currently, version is CQ5.6. Maven build is deploying successfully. However, after build, bundles in osgi show in installed state. Two dependencies causing the issue.
org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
I was getting an error earlier as below during build.
ERROR
[INFO] --- maven-bundle-plugin:2.3.4:bundle (default-bundle) # myPRJ-taglib ---
[ERROR] Error building bundle com.mypack.deewealth:myPRJ-taglib:bundle:1.0.0-SNAPSHOT : Unresolved references to [org.apache.felix.shell] by class
(es) on the Bundle-Classpath[Jar:dot, Jar:OSGI-INF/lib/recaptcha4j-0.0.8-kohsuke-1.jar, Jar:OSGI-INF/lib/commons-io-2.1.jar, Jar:OSGI-INF/lib/commons-
lang-2.4.jar, Jar:OSGI-INF/lib/crx-packagemgr-1.0.22.jar, Jar:OSGI-INF/lib/squeakysand-osgi-0.4.0.jar, Jar:OSGI-INF/lib/jsoup-1.6.1.jar, Jar:OSGI-INF/
lib/stax-api-1.0-2.jar, Jar:OSGI-INF/lib/org.apache.sling.settings-1.1.0.jar, Jar:OSGI-INF/lib/cq-compat-runmode-0.2.0.jar, Jar:OSGI-INF/lib/commons-c
ollections-3.2.1.jar, Jar:OSGI-INF/lib/squeakysand-jsp-0.4.0.jar, Jar:OSGI-INF/lib/squeakysand-commons-0.4.0.jar]: [org/apache/sling/settings/impl/Run
ModeCommand.class]
To solve this I added below dependancy in pom.xml, as we added in
Dependency
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.shell</artifactId>
<version>1.4.2</version>
<scope>provided</scope>
</dependency>
Under import statement.
<Import-Package>
....
....
org.apache.felix.shell
</Import-Package>
After that, build was successful, but bundle was in resolved state because of
org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
Any suggestion, why this is causing the problem.

org.apache.felix.shell,version=[1.0,2) -- Cannot be resolved
mean that you are trying to use these felix packages from within AEM. However - there are no OSGi bundles in AEM that exports these packages.
You can download the bundle from maven repo and upload it into the AEM Felix console and use it.
OR you can create it as your parent pom dependency as one of module and wrap along with your project jar and use it.
To veify the availability of any package and dependencies use AEM Dependency finder.

I don't know CQ but I can explain why this happens in general. You had the error during build time because of missing dependency. You correctly solved it by adding the dependency which fixed your build. What happened behind the scenes is your bundle was updated with information that it needs (imports) org.apache.felix.shell,version=[1.0,2) package and that package will be provided by some other bundle at runtime.
Then at runtime the resolver got that information and tried to find a bundle that provides (exports) org.apache.felix.shell,version=[1.0,2) package. It couldn't find one and so the resolution failed and your bundle was left in installed (I believe that's what you meant to write instaed of resolved in your last sentence) state!
To solve that, you need to make sure org.apache.felix.shell,version=[1.0,2) package is available at runtime. I don't know CQ and how to install bundles in it but since you can install your own bundle I assume you can also install org.apache.felix.shell bundle the same way.

The shell jar you have installed is still unresolved as it depends on some jars that might not be present. You need to find the depth of all dependencies and resolve them.

Related

Maven (Tycho) cannot resolve OSGi Core bundle

I have a dependency on "org.osgi:osgi.core" (7.0.0) in my POM. The reason is that I need access to the "org.osgi.framework" package. I am using Maven (3.6) and Tycho (1.5.1) for building. The build platform runs Debian 10 and Java 11.
I get the following error:
Missing requirement: osgi.core 7.0.0.201802012106 requires 'osgi.unresolvable; (&(!(must.not.resolve=*))(must.not.resolve=*))' but it could not be found
However, if I remove the dependency I get the following error:
Missing requirement: my.bundle 0.0.0.qualifier requires 'java.package; org.osgi.framework 1.7.0' but it could not be found
What is going wrong? How can I resolve this problem?
I get the following error:
Missing requirement: osgi.core 7.0.0.201802012106 requires 'osgi.unresolvable; (&(!(must.not.resolve=*))(must.not.resolve=*))' but it could not be found
The "companion jars" are not meant for runtime and since resolving is a runtime operation (even when performed during build, i.e. for deployment purposes) should not be included (and are therefore marked with the unresolvable requirement).
However, if I remove the dependency I get the following error:
Missing requirement: my.bundle 0.0.0.qualifier requires 'java.package; org.osgi.framework 1.7.0' but it could not be found
This means you have no runtime framework available! Add a runtime dependency on the equinox framework (not intentionally being biased, but since you're using tycho I'm assuming eclipse/equinox landscape. If you have Apache Felix framework available to p2/tycho then use that if you like):
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.x.0</version>
<scope>runtime</scope>
</dependency>
// of course use tycho mechanism for above.

Why third party dependency is required exclusively from OSGi container even if I have it in my maven dependencies?

I want to know why OSGi do not respect the maven dependenceis.
I want to create one app in OSGi(AEM). I want to communicate(CRUD) to the database with the help of JPA(eclipselink).
I created maven project with aem-archetype.
Added all required dependencies(of JPA) into my maven project's pom file.
No errors in Eclipse, I built the project via mvn clean install and installed it into AEM(CQ5) via mvn sling:install. All good till now. No Errors.
But when I go and see my bundle in the felix console, I see that it is not Active but in Installed state.The error reported is that it could not resolve the javax.persistence package.
I was puzzled, I searched and I read about it here -
You have to make sure that you place the same version in another
bundle and deploy first. https://forums.adobe.com/thread/2325007
I converted JPA jar to OSGi bundle and installed in my OSGi container, and the error was gone. Great!
But why OSGi is not watching out for the dependencies I wrote in pom.xml of my maven project. Why it needs JPA strictly from OSGi bundle?
Maybe this is due to any architectural benefit, but could anyone please explain me here about this behaviour of OSGi? And why/how this feature of OSGi is useful ?
The <dependency> section of your Maven POM only covers your compile time dependencies. That means when you run Maven to build your project those dependencies are used to compile the source code and build your bundle. Maven itself is not aware of AEM or OSGi or any other platform or framework (e.g. Spring).
Maven just compiles your code.
You, as a developer, are responsible that all those required compile time dependencies are also available at runtime.
What we usually do is to create an AEM content package Maven module and put all of our required third party dependencies (e.g. JPA bundles) into it. This content package is then deployed by Maven so that those dependencies are also available at runtime.
Reason is: what you are adding as dependency is getting added in build path of your project and being available for your classes.When you run mvn install,it checks presence of all dependency and creates a bundle/jar for you.By default this bundle has only your project classes not other dependencies.
You need to check in depfinder whether external dependencies are already there in OSGi container,if not you have to load them in OSGi container either by embedding external dependencies in your bundle with the help of maven-bundle-plugin present in pom.xml or by making a bundle of jar file(I wont recommend that)which you have done.
I hope this helps!

bundle state is installed/ couldnt reslove imported packages- AEM 6.2

I have installed maven bundle into http://localhost:4502/system/console/bundles
but the bundle state is installed. The error with imported packages is as follows:
jersey.repackaged.jsr166e,version=[2.22,3) -- Cannot be resolved
okhttp3 -- Cannot be resolved
okhttp3.internal -- Cannot be resolved
okhttp3.logging -- Cannot be resolved
okio -- Cannot be resolved
I added various dependencies to pom file but still bundle is not changed to active state and hence i cant invoke this service from my component jsp page. Please help.
Most of the scenarios its the problem with you internal network . Maven will try to download the jars but it will be failing .
try adding a lower jar version dependency in pom file
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-jsr166e</artifactId>
<version>2.14</version>
</dependency>
or you can download the jar directly and put it in you local maven folder
C:\Users\xxx.m2\repository and then use install/update to install the jar.
Once you successfully install the jar trying refreshing the bundle
Hope this helps

Intellij dependency broke due to no bundle file

Recently I encounter a issue maybe related to this. When project imported, there is a issue regarding not being able to find a maven dependency within ./m2 location. However, there is a .jar there. The problem is that Intellij insist to look for .bundle file rather than .jar file. Any solution for that?
Library Maven: org.apache.directory.jdbm:apacheds-jdbm1:bundle:2.0.0-M2 has broken classes path:   /Users/Aertoria/.m2/repository/org/apache/directory/jdbm/apacheds-jdbm1/2.0.0-M2/apacheds-jdbm1-2.0.0-M2.bundle
You appear to be encountering IDEA-117578 Dependency with type "bundle" isn't resolved correctly.
It's possible you can work around it by explicitly adding it as a jar dependency to your project:
<dependency>
<groupId>org.apache.directory.jdbm</groupId>
<artifactId>apacheds-jdbm1</artifactId>
<version>2.0.0-M2</version>
</dependency>

OSGI Bundle vs jar dependency

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?
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?

Resources