依赖管理和范围 [英] dependencyManagement and scope

查看:63
本文介绍了依赖管理和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在parent-project/pom.xml中放置一个<dependencyManagement>部分.此<dependencyManagement>部分包含此类子模块的所有依赖项的声明和版本(即,不包含<scope>元素):

I usually put a <dependencyManagement> section in parent-project/pom.xml. This <dependencyManagement> section contains declaration and version for all dependencies of my children modules like this (i.e. without the <scope> element):

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
  </dependencies> 
</dependencyManagement>

在所有子模块(即moduleX/pom.xml)中,我拥有:

In all children modules (i.e. moduleX/pom.xml), I have:

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

很显然,在此示例中,我为相同的依赖项重复了<scope>test</scope>次(每个需要junit的子模块一次).

Obviously, in this example I'm repeating the <scope>test</scope> multiple times for the same dependency (once in every child module needing junit).

我的问题是:
<scope>声明的最佳实践是什么?
最好将其放在<dependencyManagement>中吗?
还是最好将其放在子模块的<dependencies>部分中(如本文中所示)?为什么?
这个问题有明确的答案吗?

My question is:
What are the best practices regarding <scope> declaration?
Is it better to put it in the <dependencyManagement>?
Or is it better to put it in the <dependencies> section of the child module (like in this post)? And why?
Is there any definitive answer to this question?

推荐答案

聚会晚了一点,但是我要加两分钱.我最近遇到了一个非常难以调试的问题.我有一个父pom,用于管理多个项目之间的依赖关系.我对它们进行了设置,其中包括所有常见的依赖项,包括groupId,artifactId,版本和最常见的作用域.我的想法是,如果项目符合最常见的范围,则不必在每个项目的实际依赖项部分中包含范围.当其中一些依赖项显示为传递性依赖项时,就会发生问题.例如,如果

A little late to the party, but I'll add my two cents. I recently ran into a very difficult to debug issue. I have a parent pom for managing dependencies across multiple projects. I had it set with all the dependencies common amongst them and included groupId, artifactId, version and the most common scope. My thinking would be that I would not have to include scope in the actual dependency section in each project if it fell in line with that most common scope. The problem occurred when some of those dependencies showed up as transitive dependencies. For example if

  • A在编译范围内取决于B
  • B在编译范围内取决于C
  • C设置为在parent的dependencyManagement中提供

然后确定A对C的传递依存关系.我不确定这是否有意义,但确实令人困惑.

Then A's transitive dependency on C is determined to be provided. I'm not really sure if that makes sense or not, but it certainly is confusing.

无论如何,请省去麻烦,并将范围排除在您的dependencyManagement之内.

Anyway, save yourself the hassle, and leave scope out of your dependencyManagement.

这篇关于依赖管理和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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