简体   繁体   English

Maven如何将可传递依赖项添加到类路径中?

[英]How does maven add transitive dependencies into classpath?

I am quite confused about how does maven handle the transitive dependencies and how does it write to the classpath. 我对maven如何处理传递依赖关系以及如何将其写入类路径感到困惑。

Say if I have two paths for E (A,B,C,D,E are ejb): 假设我有两条E路径(A,B,C,D,E是ejb):
1. A<---B<---E (E dependent(comile) on B which dependent on A) 1. A <--- B <--- E(E依赖(编译)于B,而A依赖于A)
2. A<---C<---D<---E (all the dependencies are compile dependencies) 2. A <--- C <--- D <--- E(所有依赖项都是编译依赖项)

Question is: How does E built, and which artifacts are in E's classpath(META-INF). 问题是:E是如何构建的,以及E的类路径(META-INF)中包含哪些构件。

Thanks a lot! 非常感谢!

Maven will use the "nearest definition" in determining which version of dependency E to use in your build. Maven将使用“最近定义”来确定要在构建中使用哪个版本的依赖项E From the official Maven documentation : Maven官方文档中

"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. “最近的定义”表示所使用的版本将是依赖树中最接近您的项目的版本。 if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. 如果将A,B和C的依赖关系定义为A-> B-> C-> D 2.0和A-> E-> D 1.0,则在构建A时将使用D 1.0,因为从A到D的路径E较短。 You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0 您可以在A中向D 2.0显式添加一个依赖项,以强制使用D 2.0

In your example: 在您的示例中:

1. A <--- B <--- E 1.0
2. A <--- C <--- D <--- E 2.0

The 1.0 version of dependency E will be used, because when building A the path is shorter to this version. 将使用依赖项E1.0版本,因为在构建A ,路径比该版本短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM