英文:
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 <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.
答案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 <dependencies>
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.
...
<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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论