OSGi: How to use a shared library in app and integration test?

I have a Jira server app written in Scala and I have been looking into writing integration tests using Arquillian. I have been struggling with the setup for quite a while and finally got it working. However, I would like to validate some assumptions. If anyone has some advice it would be highly appreciated. I assume anyone writing Server/DC plugins in any other JVM based language (Groovy, Kotlin) would have the same issues.

Here is my struggle:
My app (plugin bundle) is written in Scala so it contains the Scala library as a dependency. My Arquillian tests (test bundle) are written in Scala too so they require the Scala dependency as well. When I wrote a JUnit test (in Scala) for the following service method

trait IssueSearchServiceApi {
  def searchByJql(term: String)(implicit user: ApplicationUser): Either[ServiceError, Seq[Issue]]
}

I got…

java.lang.LinkageError: loader constraint violation: when resolving interface method "com.example.service.api.IssueSearchServiceApi.searchByJql(Ljava/lang/String;Lcom/atlassian/jira/user/ApplicationUser;)Lscala/util/Either;" the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoader) of the current class, it/com/example/service/IssueSearchServiceTest, and the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoader) for the method's defining class, com/example/service/api/IssueSearchServiceApi, have different Class objects for the type scala/util/Either used in the signature

I assume this is because the Either instance returned by the service method is constructed from an Either class that was loaded by the plugin bundle class loader (via OSGi class loader chaining). However, this instance cannot be cast into an Either class that is loaded by the test bundle class loader - they are not the same and hence the linkage error (this video explains it nicely).

Because it is not possible to add the Scala lib to the OSGi container, the only solution that I found was to export the Scala lib (via Export-Package) from the plugin bundle. This makes sure that the Scala classes in the test bundle are loaded from the same place as the Scala classes in the plugin bundle.

I don’t really like this approach because my plugin now exports the whole Scala lib. However, I have not found any other workaround so far. Can anyone confirm that this is the only way to do this? Or, how else would you deal with shared libraries that are not provided by the OSGi container?

I’d go this way. You can configure the build process to add this export only for test build (which is not perfect too).