We are now working on compatibility with Confluence 9 for our apps, and we noticed that it is challenging to build a single artifact that could be installed on both Confluence 8 and Confluence 9.
The major issue in our case is that a few classes we are using were moved to another package since Confluence 9.
Specifically, I am talking about ‘UrlMode’ & ‘WebResourceUrlProvider’ classes, which moved from ‘com.atlassian.plugin.webresource’ to ‘com.atlassian.webresource.api’ package since the Confluence 9 release.
So, the artifact might be compatible exclusively with Confluence 8 or Confluence 9.
This is not a direct answer to your question as I don’t have any experience with the UrlMode & WebResourceUrlProvider classes. However here are some general tips on how to build cross version compatible apps.
In such cases you must either use reflection or implement features yourself or use alternatives.
You can import packages as optional in OSGI in your maven pom. So you would propably import the com.atlassian.plugin.webresource and com.atlassian.plugin.webresource.api package as optional:
You can then check if the class exists using reflection and create an instance of it. However if you need the classes to be used with Injection it is more complicated. You might have a chance by passing the class instance to importOsgiService.
Something like this (not tested):
Class c = null;
try {
c = Class.forName("com.atlassian.webresource.api.WebResourceUrlProvider");
} catch (ClassNotFoundException e) {
c = Class.forName("com.atlassian.webresource.WebResourceUrlProvider");
}
Object webResourceUrlProvider = importOsgiService(c);
// You then will have to use something like c.getDeclaredMethod()
// and other reflection methods to actually call the methods you need.
If you could get this to work you could then build a wrapper class around WebResourceUrlProvider that takes the Class object and imported Osgi Service and reimplements the methods you need by calling the underlying methods using reflection.
In general reflection will be the way to go. So read up on reflection in Java.
Also as a general advise you can learn a lot by de compiling jdks of apps that have already been ported and see how they have dealed with similar problems.