Search code examples
mavendependenciesmaven-3dependency-management

Dealing with other dependencies in your own Maven dependency


I want to reuse and centralize the utils I created for my Spring REST API for my future projects. That's why I thought I'd outsource them to my own project and make them available as a Maven dependency.

These Util files e.g. a basic service, basic controllers also contain Spring annotations, i.e. I need some Spring dependencies in my Util dependency. Now I'm a bit unsure whether I'm making a mistake or not.

First of all, I'm not sure if I should even use spring dependencies in a utility dependency or try to remove everything. Otherwise, I'll have to specify a spring version, but it might differ from the version I want to use later in the project it's included in. How am I supposed to solve this?


Solution

  • It is perfectly reasonable to have dependencies for your dependencies (these are called transitive dependencies). Of course, you should keep the number as low as possible, but on the other hand, you do not want to reinvent the wheel.

    When somebody uses your dependency, they will automatically draw the transitive dependency on spring. Now, several cases can occur:

    • If this is the only reference to spring, the version is just used as you stated it.
    • If at some other point, a different version of spring is given, Maven dependency mediation kicks in. It decides by a "nearest is best" rule which version to take.
    • But: You can always set the spring version in <dependencyManagement> and then overwrite all transitively given version numbers.