Maven多模块:将公共依赖项汇总到一个中? [英] Maven multi-module: aggregate common dependencies in a single one?

查看:620
本文介绍了Maven多模块:将公共依赖项汇总到一个中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何东西就搜索了这样的问题,所以我就走了.

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

我有一个多模块Maven项目.多个模块都继承同一个父对象,并在其中定义了公共依赖项.其中有一个我自己的模块,一个通用"模块,其中实现了一些通用功能.

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)?

推荐答案

最好在父pom中使用dependencyManagement标记来定义依赖项及其版本,然后在需要时在子模块中引用这些依赖项.当您在项目中需要其他子模块(即,另一个子模块中的公共子模块)时,将可传递地找到相关性.例如:

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:

在您的父pom中:

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

在您的常用pom中(请注意,没有版本或范围):

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.

这篇关于Maven多模块:将公共依赖项汇总到一个中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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