导入maven依赖

Importing maven dependencies

我有 2 个与此主题相关的问题。我想将以下依赖项导入到我的项目中:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>

这是我的 2 个问题

  1. 有没有办法通过 1 个导入语句导入所有 3 个?它们都具有相同的 groupId,而这 3 个是该 groupId 中唯一的。那么我可以省略 artifactId 并只导入 groupId 中的所有内容吗?
  2. Springboot 似乎导入了这些 jackson 库,但是是旧版本 (2.6.5)。当我放置这些依赖项 (2.7.3) 时,会覆盖 springboot 依赖项吗?

1。相同 groupId

的较小重要声明

据我所知,无法使用一个导入语句导入所有三个工件,即使它们使用相同的 groupId。但是,您通过使用变量来确保版本完全相同来做正确的事情。如果您看到不同依赖项的版本号冲突,这会有所帮助,这会导致下一个答案...

2。解决依赖冲突

不完全是。这两个依赖项都将包含在您的 class 路径中,这可能会在以后导致冲突(或者由于 Java 首先找到旧版本而无法解决来自较新版本的功能的问题)。有两种处理方法。一种是使用 Maven Dependency 插件来搜索如果您不确定可能会发生的冲突。然后一旦你理解了冲突(就像 Springboot 和你的软件的情况一样),你就可以排除特定的依赖项被 Springboot 引入,从而导致你的版本成为所有其他依赖项使用的版本。您可以通过在声明 Springboot 依赖项时添加排除项来做到这一点:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${springboot.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

编辑: 正如 khmarbaise 提到的,根据 Maven's documentation 关于此事,"nearest" 对您的项目的依赖(即在您的 pom 中声明而不是依赖项的 pom),将首先由您的程序使用,因此您不必排除工件。但是,我还没有发现这在实践中总是有效(或者可能发生了其他事情)。排除在另一种情况下很有用,在这种情况下,您正在创建一个包含所有依赖项的阴影 Jar,并且您不想包含同一版本的多个版本。