Maven多模块:将常见依赖项聚合在一个模块中? [英] Maven multi-module: aggregate common dependencies in a single one?

查看:34
本文介绍了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 中:

In your parent 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天全站免登陆