范围“import”和“pom”类型依赖有什么区别?

时间:2022-01-11 03:39:00

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.

部分中。

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")? Is the only difference that such "grouped" dependencies have lower priority while resolving dependencies precedence?

上面的解决方案和没有导入范围的这个pom的简单依赖之间有什么区别(我看到后者被称为“依赖关系分组”)?这种“分组”依赖关系在解决依赖关系优先级时具有较低优先级的唯一区别是什么?

3 个解决方案

#1


141  

You can only import managed dependencies. This means you can only import other POMs into the dependencyManagement section of your project's POM. i.e.

您只能导入托管依赖项。这意味着您只能将其他POM导入项目POM的dependencyManagement部分。即

...
<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. 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.

然后发生的是,在other-pom-artifact-id的dependencyManagement部分中定义的所有依赖项都包含在POM的dependencyManagement部分中。然后,您可以在POM的依赖项部分(及其所有子POM)中引用这些依赖项,而无需包含版本等。

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的正常依赖关系,那么来自other-pom-artifact-id的依赖关系部分的所有依赖关系都会被传递到项目中 - 但依赖关系定义在dependencyManagement部分中其他-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中的依赖管理,它还包含有关导入依赖项的具体信息。

#2


11  

You cannot have a pom type project as a simple dependency in another project. (Well, you can - but it will not do anything useful). There can only be a parent-child relationship. This is essentially managing dependency through inheritance.

您不能将pom类型项目作为另一个项目中的简单依赖项。 (嗯,你可以 - 但它不会做任何有用的事情)。只能有亲子关系。这主要是通过继承来管理依赖。

import scope for pom type dependency in <dependencyManagement> section allows you to achieve the equivalent of multiple inheritance.

部分中的pom类型依赖项的导入范围允许您实现多重继承的等效。

You could have different poms - each managing a bunch of related dependencies. The projects which use these could import these poms and then specify the dependencies that they need without needing to worry about the version. This is essentially the bill of materials concept, which is illustrated in the links specified by @DB5.

你可以有不同的poms - 每个poms管理一堆相关的依赖项。使用它们的项目可以导入这些poms,然后指定它们所需的依赖项,而无需担心版本。这基本上是物料清单概念,在@ DB5指定的链接中说明。

This helps to keep parent poms of complex multi-module projects from getting too large and unwieldy.

这有助于使复杂多模块项目的父pom变得过于庞大和笨拙。

#3


4  

Two concepts, very much similar to object-oriented programming paradigm, will help to answer the question:

两个与面向对象编程范例非常相似的概念将有助于回答这个问题:

  1. 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). This is like declaring a data type in program and make it available for use.

    dependencyManagement部分仅在当前项目中声明依赖关系及其详细信息 - 目的是管理细节并在其他项目中重用,可以通过继承(父)或导入(范围)。这就像在程序中声明数据类型并使其可供使用。

  2. The dependency section defines the actual use of the dependencies in the project, optionally inherit the details (i.e., version, etc.) of the dependencies declared under the dependencyManagment. That's why you will have missing dependencies if you only put them in dependencyManagment. This is analogous to instantiating an variable instance of a data type in a program where it is needed.

    依赖项部分定义项目中依赖项的实际使用,可选地继承在dependencyManagment下声明的依赖项的详细信息(即版本等)。这就是为什么如果只将它们放在dependencyManagment中,你将缺少依赖项。这类似于在需要它的程序中实例化数据类型的变量实例。

#1


141  

You can only import managed dependencies. This means you can only import other POMs into the dependencyManagement section of your project's POM. i.e.

您只能导入托管依赖项。这意味着您只能将其他POM导入项目POM的dependencyManagement部分。即

...
<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. 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.

然后发生的是,在other-pom-artifact-id的dependencyManagement部分中定义的所有依赖项都包含在POM的dependencyManagement部分中。然后,您可以在POM的依赖项部分(及其所有子POM)中引用这些依赖项,而无需包含版本等。

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的正常依赖关系,那么来自other-pom-artifact-id的依赖关系部分的所有依赖关系都会被传递到项目中 - 但依赖关系定义在dependencyManagement部分中其他-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中的依赖管理,它还包含有关导入依赖项的具体信息。

#2


11  

You cannot have a pom type project as a simple dependency in another project. (Well, you can - but it will not do anything useful). There can only be a parent-child relationship. This is essentially managing dependency through inheritance.

您不能将pom类型项目作为另一个项目中的简单依赖项。 (嗯,你可以 - 但它不会做任何有用的事情)。只能有亲子关系。这主要是通过继承来管理依赖。

import scope for pom type dependency in <dependencyManagement> section allows you to achieve the equivalent of multiple inheritance.

部分中的pom类型依赖项的导入范围允许您实现多重继承的等效。

You could have different poms - each managing a bunch of related dependencies. The projects which use these could import these poms and then specify the dependencies that they need without needing to worry about the version. This is essentially the bill of materials concept, which is illustrated in the links specified by @DB5.

你可以有不同的poms - 每个poms管理一堆相关的依赖项。使用它们的项目可以导入这些poms,然后指定它们所需的依赖项,而无需担心版本。这基本上是物料清单概念,在@ DB5指定的链接中说明。

This helps to keep parent poms of complex multi-module projects from getting too large and unwieldy.

这有助于使复杂多模块项目的父pom变得过于庞大和笨拙。

#3


4  

Two concepts, very much similar to object-oriented programming paradigm, will help to answer the question:

两个与面向对象编程范例非常相似的概念将有助于回答这个问题:

  1. 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). This is like declaring a data type in program and make it available for use.

    dependencyManagement部分仅在当前项目中声明依赖关系及其详细信息 - 目的是管理细节并在其他项目中重用,可以通过继承(父)或导入(范围)。这就像在程序中声明数据类型并使其可供使用。

  2. The dependency section defines the actual use of the dependencies in the project, optionally inherit the details (i.e., version, etc.) of the dependencies declared under the dependencyManagment. That's why you will have missing dependencies if you only put them in dependencyManagment. This is analogous to instantiating an variable instance of a data type in a program where it is needed.

    依赖项部分定义项目中依赖项的实际使用,可选地继承在dependencyManagment下声明的依赖项的详细信息(即版本等)。这就是为什么如果只将它们放在dependencyManagment中,你将缺少依赖项。这类似于在需要它的程序中实例化数据类型的变量实例。