Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best strategy for dealing with optional dependencies

I'm currently in the process of removing the Spring dependency from Flyway. In the future though other types of dependencies might be needed to support a subset of users (such as JBoss VFS support).

Which is the best way to support optional dependencies (optional=true in Maven POM)?

Qualities of the solution would be:

  • Ease of use for end-users (minimum work required to use functionality if dependency is present)
  • Ease of use for developers (code dealing with optional dependency should be as readable and as straightforward as possible)
  • No unnecessary required dependencies (if some end-user don't need this functionality, no need to pull in the dependency)
like image 907
Axel Fontaine Avatar asked Sep 09 '25 10:09

Axel Fontaine


1 Answers

I think Maven's optional dependency functionality is quite limited.

http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

Optional dependencies will not get pulled down (as transitive dependencies) by default. However, if your users need to use these optional features the missing dependencies must be explicitly declared, in their POM.

Personally, it's not clear to me how this is helpful to users.... I suppose the optional dependencies in your POM do document which versions your code depends on. Not all users however will read the POM, all they'll see is the "NoClassDef Found" error :-(

My final observation is that this is one of those rare scenarios where a dependency manager like ivy offers more flexibility. Ivy has a concept called "configurations". Module authors can assemble different combinations of dependencies, for example "with-spring" or "without-spring".

like image 121
Mark O'Connor Avatar answered Sep 11 '25 07:09

Mark O'Connor