Spring-Scanner adding unwanted Import-Package statements for classed from dynamic dependency

Hi,

I have an older server/DC plugin which uses <DynamicImport-Package> statements in the pom.xml and its Java code takes care of handling 3rd-party dependencies properly, i.e. only using those code paths when the 3rd-party marketplace app is installed and activated. Everything works as intended. So far, so good.

Now, I want to update my plugin to use Spring-Scanner. Previously, I did that in other plugins successfully. This is the 1st time that a plugin uses dynamic dependencies. I took proper care of NOT declaring any of my classes, which use classes from the 3rd-party app, as @Component or @Autowired, because I know that this cannot work if the the 3rd-party app is not installed. But nonetheless I run into “missing dependency” errors when the 3rd-party app is not installed/activated. (Everything is working fine if the 3rd-party app is installed/activated.) I debugged and gathered the following:

  • File /META-INF/MANIFEST.MF contains DynamicImport-Package statements, as defined in my pom.xml. Good.
  • But /META-INF/MANIFEST.MF also contains Import-Package statements for the dynamic packages. Not good!
  • I deleted all DynamicImport-Package statements in pom.xml -> unwanted Import-Package statements still present!
  • I deleted all references relating to classes from the dynamic 3rd-party app -> unwanted Import-Package statements are gone.
  • I re-added DynamicImport-Package statements in pom.xml -> unwanted Import-Package statements still gone.
  • I re-added all references relating to classes from the dynamic 3rd-party app -> unwanted Import-Package statements are back again!
  • I removed 1 specific reference to a 3rd-party class -> the Import-Package statement for that class’s package is gone!

After long thought my only explanation is that Spring-Scanner adds Import-Package statements for each class it finds, even for dynamic dependencies. This is bad! How can I make sure dynamic dependency stay dynamic that way, without manually adjusting /META-INF/MANIFEST.MF? Can I exclude certain classes from scanning? Can I exclude certain 3rd-party packages from being added as Import-Package statements? What else can I do to fix this, apart from not using Spring-Scanner?

Thanks a bunch,
Andreas

You can use ! in front of a package to exclude it. Check out the QueryDSL documentation:
https://bitbucket.org/atlassian/atlassian-pocketknife-querydsl/src/master/

When including atlassian-pocketknife-querydsl and dependencies in an OSGi bundle, care should be taken to import the necessary packages, optionally import the product dependent packages and exclude the “never used” packages.

Note that a * import ensures that packages such as jsr305 and others have their packages imported (when provided) rather than using the bundled versions.

<Import-Package>
    <!-- exclude pocketknife bits we don't need -->
    !net.sf.cglib.proxy,
    !org.jvnet.hudson.annotation_indexer,

    <!-- import (alltheotherthings) -->
    *
</Import-Package>
1 Like

Thanks Steve! That did the trick :slight_smile: I didn’t know about !. I’ll keep that in mind.

1 Like