Search code examples
osgiaemapache-felixmaven-bundle-plugin

How to force my OSGI bundle to ignore a conflicted a bundle while resolving dependencies


I am using AEM 5.6.1. I am getting an exception with JAXB my bundle when a jax-b api 2.2.12 manually is installed in console. As JAX-b(2.1.0) is already provided internally in AEM by below bundle. This exception doesn't happen when the 2.2.12 is not present. Also, if jax-b 2.2.12 is installed after my bundle, no problems at all.

However this new version is being used by another bundle developed by a different vendor. Hence I can't remove this new bundle from felix console.

The system bundle which provides the original JAX-B 2.1.0

`<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.fragment.xml</artifactId>
<version>1.0.2</version>
</dependency>`

I was just wondering how could I force my bundle to ignore looking for jax-b 2.2.12 and take only the system default 2.1.0 version by some configuration using maven pom. Specifically in maven bundle plugin in the POM.

Activate method in my ABC class:

`@Activate
public void activate(ComponentContext ctx) throws JAXBException {
        abcContext = JAXBContext.newInstance(ABC.class);
    }`

Current Bundle plugin import package definition:

<Import-Package>                            
                        *;resolution:=optional
                    </Import-Package>

Exception:

16.03.2016 03:13:05.709 *ERROR* [Background Update foo.barsupport-bundle (606)] foo.barsupport-bundle [foo.bar.calendar.impl.CalendarEventParserImpl] The activate method has thrown an exception (javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory not found by cqse-httpservice [25]]) javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory not found by cqse-httpservice [25]]
         at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:241)
         at javax.xml.bind.ContextFinder.find(ContextFinder.java:477)
         at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:656)
         at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
         at foo.bar.integration.calendar.impl.CalendarEventParserImpl.activate(CalendarEventParserImpl.java:33).

Any help would be really appreciated.


Solution

  • In general, your problem is that your bundle by default accept [2.0, 3.0) which is perfect for the Semantic Versioning (Major.Minor.BugFix). But, if your bundle use the newer version, and if it communicates with some of thoses classes to your framework which use an older version of that bundle, you can get those kind of ClassNotFoundException in Runtime.

    The uses directive is created to make sure that if there is multiple compatible versions, and multiple importers, those importers will communicate between them with the same version. It generally occurs when a bundle exports packages where other packages imported by other. This is why a good practice is to have API bundle which doesn't export anything or Impl bundle that exports anything, it free you from the need of uses..