[英]What is the difference between “pom” type dependency with scope “import” and without “import”?
Starting from Maven 2.0.9 there is possibility to include 从Maven 2.0.9开始,有可能包含
<type>pom</type>
<scope>import</scope>
in the <dependencyManagement>
section. 在
<dependencyManagement>
部分中。
As I understand it, it will be "replaced" with dependencies included in this pom as if they were originally defined here. 据我了解,它将被“替换”此pom中包含的依赖项,就像它们最初在此处定义一样。
What is the difference between solution above and simple dependency to this pom without import
scope (I saw the latter being called "dependencies grouping")? 上面的解决方案与没有
import
范围的这个pom的简单依赖之间有什么区别(我看到后者被称为“依赖关系分组”)? Is the only difference that such "grouped" dependencies have lower priority while resolving dependencies precedence? 这种“分组”依赖关系在解决依赖关系优先级时具有较低优先级的唯一区别是什么?
You can only import managed dependencies . 您只能导入托管依赖项 。 This means you can only import other POMs into the
dependencyManagement
section of your project's POM. 这意味着您只能将其他POM导入项目POM的
dependencyManagement
部分。 ie 即
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>other.pom.group.id</groupId>
<artifactId>other-pom-artifact-id</artifactId>
<version>SNAPSHOT</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
...
What then happens is that all the dependencies defined in the dependencyManagement
section of the other-pom-artifact-id
are included in your POM's dependencyManagement
section. 然后发生的是,在
other-pom-artifact-id
的dependencyManagement
部分中定义的所有依赖项都包含在POM的dependencyManagement
部分中。 You can then reference these dependencies in the dependency
section of your POM (and all of its child POMs) without having to include a version
etc. 然后,您可以在POM(及其所有子POM)的
dependency
部分中引用这些依赖项,而无需包含version
等。
However if in your POM you simply define a normal dependency to other-pom-artifact-id
then all dependencies
from the dependency
section of the other-pom-artifact-id
are included transitively in your project - however the dependencies defined in the dependencyManagement
section of the other-pom-artifact-id
are not included at all. 但是,如果在你的POM你简单地定义一个正常依赖于
other-pom-artifact-id
,那么所有dependencies
从dependency
一节的other-pom-artifact-id
在你的项目及物包括-但依赖于定义dependencyManagement
部分other-pom-artifact-id
根本不包括在内。
So basically the two different mechanisms are used for importing/including the two different types of dependencies (managed dependencies and normal dependencies). 所以基本上这两种不同的机制用于导入/包括两种不同类型的依赖(托管依赖和普通依赖)。
There is a good page on the maven website, which can explain this far better than I can, Dependency Management in Maven and it also contains specific information on importing dependencies . maven网站上有一个很好的页面,它可以解释这比我更好,Maven中的依赖管理,它还包含有关导入依赖项的特定信息。
You cannot have a pom
type project as a simple dependency
in another project. 您不能将
pom
类型项目作为另一个项目中的simple dependency
。 (Well, you can - but it will not do anything useful). (嗯,你可以 - 但它不会做任何有用的事情)。 There can only be a
parent-child
relationship. 只能有
parent-child
关系。 This is essentially managing dependency through inheritance
. 这主要是
managing dependency through inheritance
。
import
scope for pom
type dependency in <dependencyManagement>
section allows you to achieve the equivalent of multiple inheritance
. <dependencyManagement>
部分中的pom
类型依赖项的import
范围允许您实现multiple inheritance
的等效。
You could have different poms
- each managing
a bunch of related dependencies. 你可以有不同的
poms
- 每个poms
managing
一堆相关的依赖项。 The projects which use these could import
these poms
and then specify the dependencies that they need without needing to worry about the version. 使用它们的项目可以
import
这些poms
,然后指定它们所需的依赖项,而无需担心版本。 This is essentially the bill of materials
concept, which is illustrated in the links specified by @DB5. 这基本上
bill of materials
概念,在@ DB5指定的链接中说明。
This helps to keep parent poms
of complex multi-module projects from getting too large and unwieldy. 这有助于使复杂多模块项目的
parent poms
变得过于庞大和笨拙。
Two concepts, very much similar to object-oriented programming paradigm, will help to answer the question: 两个与面向对象编程范例非常相似的概念将有助于回答这个问题:
The dependencyManagement section only declares the dependencies and their details in the current project - the purpose is management of the details and re-use in other projects, either via inheritance ( parent ) or import ( scope ). dependencyManagement部分仅在当前项目中声明依赖关系及其详细信息 - 目的是管理细节并在其他项目中重用,可以通过继承( 父 )或导入( 范围 )。 This is like declaring a data type in program and make it available for use.
这就像在程序中声明数据类型并使其可供使用。
The dependency section defines the actual use of the dependencies in the project, optionally inherit the details (ie, version, etc.) of the dependencies declared under the dependencyManagment . 依赖项部分定义项目中依赖项的实际使用,可选地继承在dependencyManagment下声明的依赖项的详细信息(即版本等)。 That's why you will have missing dependencies if you only put them in dependencyManagment .
这就是为什么如果只将它们放在dependencyManagment中,你将缺少依赖项 。 This is analogous to instantiating an variable instance of a data type in a program where it is needed.
这类似于在需要它的程序中实例化数据类型的变量实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.