简体   繁体   English

在Maven多模块项目中,如何指定多个模块中所需的依赖关系?

[英]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). 我们称它们为moduleAmoduleB (共享依赖moduleC的模块)和moduleC (第三个模块)。

How do you deploy moduleA and moduleB ? 如何部署moduleAmoduleB Does it make sense that each have their own copy of the same library ? 每个人都拥有相同库的副本是否有意义?

  • For example if they're both WARs that run independently, you'll want to give each, its own copy of the library. 例如,如果它们都是独立运行的WAR,则需要为其提供各自的库副本。
  • If they're both jars that end up in the same war (possibly represented by 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: 解决此问题的一种可能性(例如,如果moduleAmoduleB共享很多依赖项,或者公共属性或设置)是添加一个公共父项:

  • An extra parent pom / reactor for moduleA and moduleB , but NOT for moduleC . 一个额外的父POM /反应器moduleAmoduleB ,但不适用于moduleC
  • This extra parent pom would be on the same level in the tree with moduleC (and so it would have, in turn, the same parent as moduleC ). 这个额外的父POM将是对与树同级别moduleC (所以它会反过来,同一父moduleC )。
  • It would contain whatever moduleA and moduleB have in common. 它将包含moduleAmoduleB共有的任何moduleA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM