Maven:我应该保留还是删除声明的依赖项,这些依赖项也是传递依赖项?

发布于 2024-10-03 01:07:04 字数 268 浏览 4 评论 0原文

您认为删除 Maven pom 中可以找到的所有传递依赖项是一个好习惯吗?

示例:
我的项目取决于 A 和 B。
B 也是 A 的传递依赖。
我应该将 B 保留在我的 pom 中还是将其删除?

什么是最好的:
拥有所有已知的 jar,甚至是传递性的 jar,在 pom 上声明还是只保留顶级 jar?

这有点主观,但我正在尝试清理一些具有大量传递依赖项的巨大 poms(父级和子级)。我想让我的 pom 尽可能简单,但我也希望它们是可维护的。

Do you think it is a good practice to remove every transitive dependencies that can be found in a maven pom?

Example:
My project depends on A and B.
B is also a transitive dependency of A.
Should I keep B in my pom or remove it ?

What is the best:
having all known jars, even transitive one, declared on the pom or keeping only the top level jars ?

This is a little bit subjective, but I am trying to clean some huge poms (parent and children) with a lot of transitive dependencies. I want to keep my pom as simple as possible, but I want also them to be maintainable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-10-10 01:07:04

如果您的项目对 B 具有直接依赖关系,那么即使 B 是 A 的传递依赖关系,您也应该保留它。在下一个版本中,A 可能不会使用 B,您将不得不重构 pom.xml。

一般来说,Maven 依赖关系应该反映逻辑项目依赖关系。

If your project has direct dependencies on B then you should keep it even if B is a transitive dependency of A. It can be that in the next version A won't use B an you'll have to restructure the pom.xml.

Generally, Maven dependencies should reflect the logical project dependencies.

落花随流水 2024-10-10 01:07:04

更愿意避免声明传递依赖项,并在有充分理由的情况下将它们显式包含在 pom 中。以下是我的论点:

  • 我尝试使 pom 尽可能简单。声明了传递依赖项后,即使显式使用它们,Maven pom 也会变得更加冗长。

通过声明传递依赖项(即使您明确需要它们):

  • 引入了声明中的冗余,因为此信息已经存在于所需工件的 pom 描述符中< /em>.

  • 如果新版本的所需工件不再依赖于传递依赖,则必须从您的传递依赖中删除传递依赖如果显式声明了传递依赖项,请自行组装。

  • 通过显式声明传递依赖来操纵传递性信息。

在以下情况下显式包含依赖项是有意义的

  • 您有两个依赖项,例如 CD,它们需要不同的版本传递依赖项 B(或者您需要在项目中使用特定版本的 B)。在这种情况下,您必须选择 B 的版本并显式定义传递依赖项 T。 (*)

结论
我会尽量避免声明,除非专门声明工件是有意义的(如 (*) 的情况)。

I would prefer to avoid the declaration of the transitive dependencies, and explicitly include them in the pom if there is a good reason for that. Here are my arguments:

  • I try to keep the pom as simple as possible. With the transitive dependencies declared, even if they are used explicitly, the Maven pom becomes more verbose.

By declaring the transitive dependencies (even if you explicitly need them):

  • Redundancy in the declaration is introduced, because this information is already in the pom descriptor of the artifact that is required.

  • If a new version of the artifact required does not depend on the transitive dependency anymore, you have to remove the transitive dependency from your assembly yourself, if that transitive dependency is explicitly declared.

  • The information for the transitivity gets manipulated by declaring the transitive dependency explicitly.

It would make sense to include a dependency explicitly in the following case:

  • You have one two dependencies, say C and D, that require different versions of the transitive dependency B (or you require a specific version of B in your project). In this case you have to choose a version of B and explicitly define the transitive dependency T. (*)

Conclusion:
I would try to avoid the declaration, unless it makes sense to declare the artifact specifically (like in the case (*)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文