Maven multi-module: aggregate common dependencies in a single one?

旧时模样 提交于 2019-12-18 12:24:23

问题


I have searched for such a question without finding anything, so here I go.

I have a multi-module maven project. Multiple modules all inherit the same parent, where common dependencies are defined. Among them, there is one my own modules, a 'common' one, where some common functionality is implemented.

My question is: What would be a better practice for common dependencies: Define them all explicitly in the parent, like I currently do? Or define them in a 'common' module, which other modules reference, and then rely on transitivity (like a single-entry-point for common dependencies)?


回答1:


It's best to use the dependencyManagement tag in your parent pom to define your dependencies and their versions then reference these dependencies in your sub modules where needed. When you require other sub modules in your project (ie your common submodule from another submodule) then the dependencies will be found transitively. For example:

In your parent pom:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

In your common pom (notice there is no version or scope):

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
</dependencies>

And then you can just reference your common submodule from other submodules are you are already.




回答2:


I can see two reasonable options:

  • Declare common dependencies in a common module
  • Use import scope

Second option is useful when you have multiple modules depend on same large stack of components with number of their own dependencies.



来源:https://stackoverflow.com/questions/6086774/maven-multi-module-aggregate-common-dependencies-in-a-single-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!