简体   繁体   English

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

[英]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-iddependencyManagement部分中定义的所有依赖项都包含在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 ,那么所有dependenciesdependency一节的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: 两个与面向对象编程范例非常相似的概念将有助于回答这个问题:

  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 ). dependencyManagement部分仅在当前项目中声明依赖关系及其详细信息 - 目的是管理细节并在其他项目中重用,可以通过继承( )或导入( 范围 )。 This is like declaring a data type in program and make it available for use. 这就像在程序中声明数据类型并使其可供使用。

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

相关问题 没有显式导入的类之间的依赖关系 - Dependency between classes without explicit import 如何在特定的POM依赖项上导入特定的类? - How to import a specific class on a specific POM Dependency? 导入和静态导入之间的区别? - Difference between import and static import? Python和Java中import语句的含义有什么区别? - What's the difference between the meaning of import statement in Python and Java? keytool命令的import和-importcert有什么区别 - What is the difference between keytool command's import and -importcert 尽管为Jcraft Jsch添加了Pom依赖项,但无法导入类 - Unable to import the classes inspite of adding Pom dependency for Jcraft Jsch 我的Maven项目能否从依赖项中的pom导入依赖项? - Can my maven project import dependencies from a pom inside a dependency? 在Spring框架中使用@Import和@ImportResource注释有什么区别? - What is the difference between the use of @Import and @ImportResource annotations in Spring framework? 导入实用程序有什么区别。*并且只需要Android中的乐趣? - What are difference between import utility.* and only need fun in Android? 在两个项目之间使用依赖的超级pom之间的区别 - Difference between using super pom of dependency between two project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM