最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

maven - Same dependency in parent, child pom - Stack Overflow

programmeradmin5浏览0评论

What if I have two versions of the same dependency: one in parent pom, another in child pom? How does Maven resolve it — assuming no <dependencyManagement>?

Please back it up with a doc quote, if possible.

<!-- parent pom -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-artifact</artifactId>
    <version>1.1</version>
</dependency>
<!-- child pom -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-artifact</artifactId>
    <version>1.0</version>
</dependency>

I found this in the Maven documentation

Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

However, save for this brief mention

In general, all dependencies of those projects are used in your project, as are any that the project inherits from its parents, or from its dependencies, and so on.

it doesn't mention parent poms explicitly, so I'm not sure whether it describes my case as well.

What if I have two versions of the same dependency: one in parent pom, another in child pom? How does Maven resolve it — assuming no <dependencyManagement>?

Please back it up with a doc quote, if possible.

<!-- parent pom -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-artifact</artifactId>
    <version>1.1</version>
</dependency>
<!-- child pom -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-artifact</artifactId>
    <version>1.0</version>
</dependency>

I found this in the Maven documentation

Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

However, save for this brief mention

In general, all dependencies of those projects are used in your project, as are any that the project inherits from its parents, or from its dependencies, and so on.

it doesn't mention parent poms explicitly, so I'm not sure whether it describes my case as well.

Share Improve this question edited 2 days ago Cagepi asked Feb 18 at 3:09 CagepiCagepi 1911 silver badge7 bronze badges 3
  • How is it defined in both the parent and the child? dependencyManagement or not could change the result for instance. – Gaël J Commented 2 days ago
  • 2 Anyway, just try and you'll see with mvn dependency:tree which one is picked up. (Though that won't necessarily explain why) – Gaël J Commented 2 days ago
  • @GaëlJ I want a reference, not just an observation – Cagepi Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Usually, you should not put dependencies into the parent POM.

I guess that the parent POM dependency wins. But this is something that you could try for yourself. You can also look at the effective POM. The problem is probably equivalent to the question: What happens if you declare the same dependency twice?

I doubt that there is any documentation about this except the source code itself.

发布评论

评论列表(0)

  1. 暂无评论