How to handle disabled plugin dependency from the dependent code

I have a plugin A with an optional dependency on classes from a plugin B.
I have a control over the source code of a plugin B only.
The workflow I am trying to implement is following:

  1. If plugin B is enabled → use classes from it and execute dependent code
  2. If plugin B disabled/not installed → skip use of plugin B classes in the dependent code

Steps that I took so far:

  1. In my pom.xml for plugin A the dependency artifact marked as provided
  2. In the Import section the package is listed as optional.
  3. I have a class (component) with a constructor where I take a class from plugin B and set it as a field of the instance:
@Component
public class MyEventListener implements InitializingBean, DisposableBean {
    @Autowired
  MyEventListener(@ComponentImport PluginBClass dataManager) {

    this.dataManager   = dataManager;

    this.MyIndexer       = new MyIndexer(edataManager);

  }

 {
  //use here dependency if it is available
 }
}

When I disable plugin B and try to enable plugin A, UPM says that some of dependencies are not available and the plugin remains disabled. Is there any approach to load the dependency conditionally only if the plugin B is enabled.

Hello Aiaz,

dynamic import or ModuleTypes might be a solution here and both topics are worth researching them.

Best regards,
Robert

Hi @RobertKrause ,

Thanks for the suggestion.
Does the same solution apply to the case when I have a control only over a dependent code (plugin A) but not the dependency (plugin B) ?

Regards,
Aiaz