Maven依赖管理

361 阅读2分钟

其中一个Maven的核心特征是依赖管理。管理依赖关系变得困难的任务一旦我们处理多模块项目(包含数百个模块/子项目)。 Maven提供了一个高程度的控制来管理这样的场景。

传递依赖发现

这是很通常情况下,当一个库说A就依赖于其他库说B的情况下,另一个项目Ç想用A,则该项目需要使用库中B。

在Maven帮助下以避免这样的要求来发现所有需要的库。 Maven通过读取依赖项项目文件(pom.xml中),找出它们的依赖等。

我们只需要在每个项目POM定义直接依赖关系。 Maven自动处理其余部分。

传递依赖,包括库的图形可能会快速增长在很大程度上。可能出现情况下,当有重复的库。 Maven提供一些功能来控制传递依赖程度

Feature描述
Dependency mediationDetermines what version of a dependency is to be used when multiple versions of an artifact are encountered. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.
Dependency managementDirectly specify the versions of artifacts to be used when they are encountered in transitive dependencies. For an example project C can include B as a dependency in its dependencyManagement section and directly control which version of B is to be used when it is ever referenced.
Dependency scopeIncludes dependencies as per the current stage of the build
Excluded dependenciesAny transitive dependency can be excluede using "exclusion" element. As example, A depends upon B and B depends upon C then A can mark C as excluded.
Optional dependenciesAny transitive dependency can be marked as optional using "optional" element. As example, A depends upon B and B depends upon C. Now B marked C as optional. Then A will not use C.

依赖范围

传递依赖发现可以使用各种依赖范围如下文所述受到限制

Scope描述
compileThis scope indicates that dependency is available in classpath of project. It is default scope.
providedThis scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime.
runtimeThis scope indicates that dependency is not required for compilation, but is required during execution.
testThis scope indicates that the dependency is only available for the test compilation and execution phases.
systemThis scope indicates that you have to provide the system path.
importThis scope is only used when dependency is of type pom. This scopes indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section.

依赖关系管理

通常情况下,我们已经一套项目在一个共同的项目下。在这种情况下,我们可以创造让所有的公共依赖一个共同的POM,然后进行分项目POMS为这个POM父。下面的例子将帮助你理解这个概念

经典介绍:

源码结构


愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨

更多详细源码参考来源:minglisoft.cn/technology