如果它们来自我父母的其他子模块,我应该依赖 Maven 中的传递依赖吗? [英] Should I rely on transitive dependencies in Maven if they come from other sub-module of my parent?

查看:24
本文介绍了如果它们来自我父母的其他子模块,我应该依赖 Maven 中的传递依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们正在开发 mortgage 子模块,并且我们在模块代码中直接使用 Google Guava 类,但是依赖于 guava 定义在同一父模块下的其他子模块中,我们只能通过对投资"模块的传递依赖来访问 Guava 类:

Suppose we are working on mortgage sub-module, and we are directly using the Google Guava classes in module code, but the dependcy for the guava is defined in other sub-module under the same parent and we have access to Guava classes only by transitive dependency on "investment" module:

banking-system (parent pom.xml)
|
|-- investment (pom.xml defines <dependency>guava</dependency>)
|
|-- mortgage (pom.xml defiens <dependency>investment</dependency>)

我们还应该在mortgage pom.xml中给Guava放一个<dependency>吗?

Should we still put a <dependency> to Guava in the mortgage pom.xml?

缺点在我们的 pom.xml 中看起来像重复,优点是:如果有人开发投资"会丢弃番石榴,那么它不会阻止我们的抵押子模块成功构建.

The cons looks like duplication in our pom.xml, the pros are: if someone developing "investment" will drop guava, then it will not stop our mortgage sub-module from being successfuly build.

如果是,那么我们应该指定什么<version>?(在父 pom 中没有 + <dependencyManagement>?)

If yes, then what <version> shoudle we specify? (none + <dependencyManagement> in parent pom?)

如果是,那么我们是否应该在某个模块中使用 <provided> 范围?

If yes, should we use a <provided> scope in some module then?

注意:请记住,我是在特定情况下询问的,当模块具有共同的父 pom 时(例如,作为一个整体的应用程序).

也许这个结构不是最好的例子,想象一下:

Maybe this structure was not the best example, imagine:

banking-app
    banking-core (dep.on: guava, commons, spring)
    investment (dep.on: banking-core)
    mortgage (dep.on: banking-core)

是否还应该Investment在使用@Component时显式声明Spring,如果使用Guava的LoadedCache则声明Guava?

Should still Investment explicitly declare Spring when it use @Component, and declare Guava if it uses Guava's LoadedCache?

推荐答案

我们在模块代码中直接使用 Google Guava 类,但是番石榴的依赖关系在相同的其他子模块中定义父级,我们只能通过传递访问 Guava 类对投资"模块的依赖 [...] 我们还应该在抵押 pom.xml 中放入 Guava 吗?

we are directly using the Google Guava classes in module code, but the dependcy for the guava is defined in other sub-module under the same parent and we have access to Guava classes only by transitive dependency on "investment" module [...] Should we still put a to Guava in the mortgage pom.xml?

是的,你应该在你的模块中声明 Google Guava 依赖,而不是期望它可以作为传递依赖.即使它适用于当前版本,但在以后的直接依赖版本中可能不再如此.

Yes, you should declare Google Guava dependency in your module and not expect it to be available as transitive-dependency. Even if it works with the current version, it may not be the case anymore in later versions of direct dependencies.

如果你的代码依赖于一个模块,你的代码应该只直接依赖于这个模块的类,而不是这个模块的传递依赖.正如您所提到的,不能保证 investment 模块将来会继续依赖 Guava.您需要在父级的 pom.xml 或模块本身中指定此依赖项,以确保它在不依赖传递依赖项的情况下可用.这不是重复,你怎么能告诉 Maven 你的模块依赖于 Guava?

If your code depends on a module, your code should depends only directly on classes of this module, not a transitive-dependency of this module. As you mentioned, there is no guarantee that the investment module will continue to depend on Guava in the future. You need to specify this dependency either in the parent's pom.xml or in the module itself to ensure it will be available without relying on transitive dependencies. It's not duplication as such, how else can you tell Maven your module depends on Guava?

我没有看到任何情况下您需要遵守最低限度的最佳实践.

I do not see any situation in which minimal best practices are respected where you would need to do otherwise.

如果是,那么我们应该指定什么<version>?(在父 pom 中没有 + <dependencyManagement>?)

If yes, then what <version> shoudle we specify? (none + <dependencyManagement> in parent pom?)

,在父模块中使用 <dependencyManagement> 并在没有版本的子模块中使用 <dependency> 是最好的:你将确保您的所有模块都使用相同版本的依赖项.由于您的模块作为一个整体是一个应用程序,因此它可能会更好,因为它可以避免各种问题,例如在类路径上存在相同依赖项的不同版本而造成破坏.

Yes, using <dependencyManagement> in parent and using a <dependency> in your child module without version is best: you will make sure all your modules uses the same version of your dependency. As your modules are an application as a whole, it is probably better as it will avoid various issues such as having different versions of the same dependency being present on the classpath causing havoc.

即使出于某种原因,您使用相同父模块的模块需要不同版本的依赖项,仍然可以使用 <version> 覆盖此特定模块的版本.

Even if for some reason one of your module using the same parent requires a different version of our dependency, it will still be possible to override the version for this specific module using <version>.

如果是,那么我们应该在某个模块中使用作用域吗?

If yes, should we use a scope in some module then?

可能不是,具有 compile 范围的依赖项是大多数打包方法的最佳选择.

Probably not, having the dependency with a compile scope is the best wat to go with most packaging methods.

但是您可能会遇到需要或更喜欢这样做的情况,例如,如果所述模块需要使用运行时环境特定版本,或者您的部署或打包模型的设计方式这需要它.鉴于您所暴露的情况,两者都是可能的,尽管大多数时候它不应该是必要的.

However you may have situations where you need or prefer to do this, for example if said modules requires to use a runtime environment specific version, or if your deployment or packaging model is designed in a way that demands it. Given the situation you expose, both are possible, though most of the time it should not be necessary.

这篇关于如果它们来自我父母的其他子模块,我应该依赖 Maven 中的传递依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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