What is the difference between using and not using <type>pom</type> for a dependency (in the <dependencies> section)?

huangapple go评论67阅读模式
英文:

What is the difference between using and not using <type>pom</type> for a dependency (in the <dependencies> section)?

问题

I cannot really find any documentation on this. Yet I sometimes see this in the <dependencies> section of some pom.xml files:

<dependency>
    <groupId>com.group.id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
</dependency>

But I cannot understand the difference with:

<dependency>
    <groupId>com.group.id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>1.0.0</version>
</dependency>

From my knowledge, <type>pom</type> is only relevant for dependencies found in the <dependencyManagement> section.

I don't understand how it can be useful in the <dependencies> section.

英文:

I cannot really find any documentation on this. Yet I sometimes see this in the &lt;dependencies&gt; section of some pom.xml files :

&lt;dependency&gt;
    &lt;groupId&gt;com.group.id&lt;/groupId&gt;
    &lt;artifactId&gt;artifact-id&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;

But I cannot understand the difference with:

&lt;dependency&gt;
    &lt;groupId&gt;com.group.id&lt;/groupId&gt;
    &lt;artifactId&gt;artifact-id&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;

From my knowledge &lt;type&gt;pom&lt;/type&gt; is only relevant for dependencies found in the &lt;dependencyManagement&gt; section.

I don't understand how it can be useful in the &lt;dependencies&gt; section.

答案1

得分: 2

Use case:

在大多数项目中,您始终具有相同的五个依赖项。但是,您无法通过父POM来处理这个问题,因为您可能已经需要使用固定的父POM。

然后,您可以创建一个类型为pom的项目onlypom,其中只包含一个带有这五个依赖项的pom.xml文件。然后,在您的项目的<dependencies>部分中,您添加一个类型为pom的依赖项到onlypom,这样您就可以避免始终复制/粘贴五个依赖项条目。

英文:

Use case:

You always have the same five dependencies in most of your projects. But you cannot handle this through a parent POM because you e.g. already need to use a fixed parent POM.

Then you can create a project onlypom of type pom that only contains a pom.xml with the said five dependencies. Then you add a dependency to onlypom of type pom in your projects &lt;dependencies&gt; section, so that you can avoid to always copy/paste five dependency entries.

答案2

得分: 0

你只能导入托管依赖项。这意味着你只能将其他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>
...

然后,所有在other-pom-artifact-id的dependencyManagement部分中定义的依赖项都将包括在你的POM的dependencyManagement部分中。然后,你可以在你的POM(以及所有子POM)的dependency部分中引用这些依赖项,而无需包括版本等信息。

但是,如果在你的POM中只是简单地定义了对other-pom-artifact-id的普通依赖关系,那么来自other-pom-artifact-id的dependency部分中的所有依赖关系都会传递地包括在你的项目中,但是other-pom-artifact-id的dependencyManagement部分中定义的依赖关系则根本不会包括在其中。

因此,基本上这两种不同的机制用于导入/包括两种不同类型的依赖关系(托管依赖关系和普通依赖关系)。

Maven网站上有一个很好的页面,可以更好地解释这一点,名为"Dependency Management in Maven",它还包含有关导入依赖项的具体信息。

英文:

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.

...
&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;other.pom.group.id&lt;/groupId&gt;
            &lt;artifactId&gt;other-pom-artifact-id&lt;/artifactId&gt;
            &lt;version&gt;SNAPSHOT&lt;/version&gt;
            &lt;scope&gt;import&lt;/scope&gt;
            &lt;type&gt;pom&lt;/type&gt;
        &lt;/dependency&gt;   
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
...

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.

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.

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.

huangapple
  • 本文由 发表于 2023年5月17日 19:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定