【问题标题】:DependcyManagement or dependencies ?? in pom mavenDependcyManagement 或依赖项??在 pom maven
【发布时间】:2020-06-12 16:54:45
【问题描述】:

根据 maven 文档Dependency management

是一种集中依赖信息的机制。

从这里SO Question的这个问题来看,当我们有一个供所有孩子使用的公共jar文件时,很多人建议使用Dependency managmement而不是dependencies

dependency management 一样,依赖项仅在孩子请求时传播,但在 dependencies 的情况下,即使不需要,也会传播依赖项。

但是当 jar 文件对所有孩子都通用时,这不是更好的方法,即当所有孩子都继承同一个 jar 文件时

例如(取自 maven 文档的重写示例)

孩子

  <dependencies>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-a</artifactId>
    </dependency>

    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

孩子 b

  <dependencies>
    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>

    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

a 和 b 的父级


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

但这不会产生相同的结果吗??

a 和 b 的父级

    <dependencies>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>

孩子

  <dependencies>
    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

孩子 b

  <dependencies>
    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

还是我误会了??

我应该在什么条件下使用哪一个??

【问题讨论】:

    标签: java maven dependencies pom.xml


    【解决方案1】:

    当您在子项目中使用这些库时,父 pom 中的依赖管理部分正在管理这些库的版本和仅版本。

    相反,依赖部分强制所有子模块具有这些依赖项,无论子模块是否需要它们。

    如果您确定您的所有子模块都将使用group-a:module-a:version,请随时在父模块dependencies 部分中声明它。

    如果您在项目中至少有一个子模块,而这种强制依赖是不必要的,那么dependencyManagement 更适合您的需求。

    【讨论】:

    • 所以如果所有孩子都需要他们,那么使用dependencies会更好吗??
    • 所以dependencies 的唯一成本是不需要的继承是否正确?即它与dependencyManagement 在其他所有意义上都相同?
    • @ursa DependencyManagement 不仅处理版本,它还可以用于处理中心位置的排除,这也是使用dependencyManagement 的原因之一。此外,您在项目中有一个中心位置来定义您正在使用的所有版本。
    • @Hummingbird 完全正确。
    • @khmarbaise 虽然在技术上可以管理排除项,但如果您的项目是其他项目使用的库(具有不同的根 pom 和排除项),它可能会给您带来调试噩梦。
    猜你喜欢
    • 2022-12-06
    • 2011-08-30
    • 2018-06-07
    • 2010-11-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多