您是否应该在pom中包括已经是某些依赖项的依赖项的那些依赖项? [英] Should you include those dependencies in your pom that are already the dependencies of some of your dependencies?

查看:110
本文介绍了您是否应该在pom中包括已经是某些依赖项的依赖项的那些依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您需要两个依赖项:A和B.同时A已经是B的依赖项.那么,您是否仍希望/需要将A和B一起添加为pom的依赖项?

Say there are two dependencies you need: A and B. And at the same time A is already a dependency of B. So do you still want/need to add A along with B as dependencies in your pom?

我相信当A和B是外部库时,这可能是必需的,其中所需的A版本可能与B所依赖的A版本不同.

I believe this may be needed when A and B are external libraries where the version of A needed may be different than the version of A that B is depending on.

但是,当您的模块以及A和B都是同一项目中的模块时,该怎么办?也就是说,知道它们的版本将会同步.

But how about when both your module and A and B are modules in the same project? i.e. knowing their versions are all going to be in sync.

推荐答案

如果您的模块使用B中的API,则最佳做法是将其显式添加到pom中,即使并非绝对必要.如果升级A,则很可能不再使用B,然后在不更改模块代码的情况下将导致构建失败.

If your module uses APIs from B it's best practice to add it explicitly to your pom, even though it's not strictly necessary. If you upgrade A, it could well be that it doesn't use B anymore and then you'll get a build failure without any changes to your module code.

关于版本,您应该使用 dependencyManagement 在父pom中.然后,您可以在子poms中跳过托管依赖项的版本. dependencyManagement中的版本会覆盖传递性依赖中的版本,以确保您在所有地方都使用相同的版本.

Regarding versions, you should manage those with dependencyManagement in a parent pom. You can then skip the version for the managed dependencies in the child poms. The version in the dependencyManagement overrides the version in transitive dependencies, ensuring you use the same version everywhere.

如果所有模块都在同一项目中,则它们也应该共享相同的项目版本.通常,这将是快照版本,例如1-SNAPSHOT

If all modules are in the same project, they should also share the same project version. Typically, this will be a snapshot version, e.g. 1-SNAPSHOT

每个模块将使用类似的内容:

Each module will use something like:

<project>
  <artifactId>A</artifactId>
  <version>1-SNAPSHOT</version>

在其他模块中像这样引用A和B:

And refer to A and B like this in other modules:

<dependency>
  <groupId>com.yourcompany</groupId>
  <artifactId>A</artifactId>
  <version>${project.version}</version>
</dependency>

要在构建发行版之前设置非SNAPSHOT版本,例如,可以使用maven-dependency-plugin的

To set a non-SNAPSHOT version before you build a release, you can for example use the maven-dependency-plugin's versions:set goal.

这篇关于您是否应该在pom中包括已经是某些依赖项的依赖项的那些依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆