[英]In a Maven multi-module project, how to specify dependency needed in more than one module?
I know that the intent of a multi-module Maven project is that each module can be compiled and built separately as well as collectively from the parent. 我知道,多模块Maven项目的目的是每个模块都可以单独编译,也可以与父模块一起构建。
Suppose two of my three modules need to use the Google Guava library. 假设我的三个模块中的两个需要使用Google Guava库。 Do I specify that dependency in the POM of each of the two sub-modules?
是否在两个子模块的每个POM中指定该依赖性? If so, what “scope”?
如果是这样,什么是“范围”? Or do I specify the dependency in the parent module?
还是在父模块中指定依赖项? I need Guava bundled in my final product, but I certainly don't need two copies.
我需要最终产品中捆绑有番石榴,但我当然不需要两份。
I usually declare all dependencies in dependencyManagement
of the parent project and then use them in depenencies
of modules, but without version
. 我通常申报所有依赖
dependencyManagement
父项目,然后在使用它们depenencies
模块,但没有version
。 Versions are only declared in the parent project. 版本仅在父项目中声明。
As for scope
, I normally don't declare scope in parent unless it is something very obvious like JUnit. 至于
scope
,除非像JUnit这样非常明显,否则我通常不在父级中声明作用域。
Do I specify that dependency in the POM of each of the two sub-modules?
是否在两个子模块的每个POM中指定该依赖性?
If you use this dependency directly, then yes. 如果直接使用此依赖关系,则可以。 Even if it would be included transitively.
即使将其包含在传递中。
If so, what “scope”?
如果是这样,什么是“范围”?
This depends on what the dependency is and how you use it. 这取决于依赖项是什么以及如何使用它。 Guava will normally be
compile
. 番石榴通常会被
compile
。
Or do I specify the dependency in the parent module?
还是在父模块中指定依赖项?
You can declare it in dependencyManagement
in parent so that you don't have to specify the version anywhere else. 您可以在parent的
dependencyManagement
中声明它,这样您就不必在其他任何地方指定版本。
But you normally will not declare the dependency in the parent module as it will be inherited by all the child modules. 但是您通常不会在父模块中声明依赖项,因为它将被所有子模块继承。 This is usually too much of an assumption.
这通常是一个过多的假设。
I need Guava bundled in my final product, but I certainly don't need two copies.
我需要最终产品中捆绑有番石榴,但我当然不需要两份。
Won't happen. 不会发生
Suppose two of my three modules need to use the Google Guava library.
假设我的三个模块中的两个需要使用Google Guava库。 Do I specify that dependency in the POM of each of the two sub-modules?
是否在两个子模块的每个POM中指定该依赖性?
It depends what is the relation between those modules and what you do with them; 这取决于这些模块之间的关系以及您对它们的处理方式。 let's call them
moduleA
, moduleB
(the modules that share a dependency), and moduleC
(the third module). 我们称它们为
moduleA
, moduleB
(共享依赖moduleC
的模块)和moduleC
(第三个模块)。
How do you deploy moduleA
and moduleB
? 如何部署
moduleA
和moduleB
? Does it make sense that each have their own copy of the same library ? 每个人都拥有相同库的副本是否有意义?
moduleC
), then you'll want to give each of them the dependency with scope=provided
, and add a third dependency in the moduleC
with the default scope. moduleC
),那么你会希望给他们每个人的依赖性scope=provided
,并添加第三依赖于moduleC
使用默认范围。 Or do I specify the dependency in the parent module?
还是在父模块中指定依赖项?
The problem with specifying the dependency in the parent module is that the dependency will also be picked up by moduleC
which might not be what you want. 在父模块中指定依赖项的问题在于,依赖项也将由
moduleC
,而这可能不是您想要的。
One possibility to solve this (for example if moduleA
and moduleB
share a lot of dependencies, or common properties or settings) is to add a common parent: 解决此问题的一种可能性(例如,如果
moduleA
和moduleB
共享很多依赖项,或者公共属性或设置)是添加一个公共父项:
moduleA
and moduleB
, but NOT for moduleC
. moduleA
和moduleB
,但不适用于moduleC
。 moduleC
(and so it would have, in turn, the same parent as moduleC
). moduleC
(所以它会反过来,同一父moduleC
)。 moduleA
and moduleB
have in common. moduleA
和moduleB
共有的任何moduleA
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.