-
Couldn't load subscription status.
- Fork 1
maven dependency management
To make this guide short: When using Maven multi-module projects, you should always use dependency-management. Details and explanations can be found in Maven dependency management documentation.
Additional best-practices:
-
Define all dependency versions for your project in a single
<dependencyManagement>section in your top-level POM. Only use<dependencyManagement>in child modulepom.xmlfiles if you explicitly need to import or define a BOM. -
Still define variables for versions and follow strict naming conventions. Especially if such POM gets reused in multiple projects this is an absolute-best-practice since it allows overriding versions easily in child POM. For a good example follow the patterns from spring-boot-dependencies.
-
Avoid to define
<scope>in<dependencyManagement>. Some people think it may be smart to add<scope>test</test>in<dependencyManagement>for dependencies likejunit. However, if you read our maven-test-reuse guide, you will notice that such dependencies likejunitmay also be used in test-infrastructure modules where they needcompilescope. Since maven is following convention-over-configuration the default scope iscompileif omitted by default. However, if<scope>is defined in<dependencyManagement>this will override the default for that dependency. Once you ever ran in the pitfall that you added a dependecy in a test-infrastructure module and ende up with compile errors because your code insrc/main/javacannot see code from libraries that you actually have declared as dependency, you will notice the problem of such "black-magic". Therefore strictly avoid this to make your life easier. -
If you create a library and provide a BOM please consider defining a
minimalBOM with only your own dependencies but no other 3rd party. You can still provide an additionalfullBOM with the<dependencyManagement>for all required 3rd party on top of yourminimalBOM. The rationale is that some projects may use and mix many different libraries together. When multiple BOMs are imported into your<dependencyManagement>that contribute different versions for the same dependency then it gets very tricky. In such case the order of yourimportscoped depdendency (BOM) determines which version wins. If that even happens along the inheritance hierarchy of multiple POMs you need a real maven guru to explain what is happening if something goes wrong. Most projects want to give precendence to BOMs from major frameworks like spring-boot or quarkus rather than some additional library for a small aspect. If such library provides aminimalBOM it will not conflict with the framework BOM and if the project only imports suchminimalBOM a lot of pitfalls and headaches can be avoided.
This documentation is licensed under the Creative Commons License (Attribution-NoDerivatives 4.0 International).
