如何从Maven导入范围中排除Spring Boot依赖项的传递性依赖

7

根据文档,我的Spring Boot应用程序pom中有以下内容:

  <dependencyManagement>
    <dependencies>
      <!-- Spring -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

我需要使用dependencyManagement<scope>import</scope>,因为我需要使用标准的公司基础pom。
然而,似乎无法排除spring-boot-dependencies的传递依赖项。在我的特定情况下,Spring Boot 1.2.1.RELEASE引入了一个版本过于新的Jetty,与我的其他<dependencies>不兼容。我尝试使用以下形式的<exclusion>
  <dependencyManagement>
    <dependencies>
      <!-- Spring -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>

        <!-- Doesn't work -->
        <exclusions>
          <exclusion>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>*</artifactId>
          </exclusion>
        </exclusions>

      </dependency>

    </dependencies>
  </dependencyManagement>

使用Maven 3.2.1的通配符支持,但似乎没有生效。

除了显式地覆盖所有Jetty依赖项之外,是否有其他解决此问题的方法? Jetty库很多,这种方法会非常脆弱。 此外,似乎我还需要对Jetty的传递依赖项执行相同的操作。

2个回答

5
根据Spring Boot Maven Plugin 2.3.1.RELEASE 文档,要覆盖单个依赖项,您需要在项目的dependencyManagement部分中添加条目,spring-boot-dependencies条目之前。
  <dependencyManagement>
    <dependencies>
      <!-- Your jetty version dependency -->
      <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>*</artifactId>
        <version>${jetty.version}</version>
      </dependency>

      <!-- Spring -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

很酷:用一个非常有用的答案回答一个5岁孩子的问题! - GeertPt

3

看起来使用Maven导入范围不可能

导入范围可用于将远程POM中的依赖关系管理信息包含到当前项目中。其中一个限制是它不允许为多模块项目定义其他排除

同样,Spring Boot文档也有确认:

如果您已经在自己的dependencyManagement部分中添加了spring-boot-dependencies,并使用了<scope>import</scope>您必须重新定义该artifact [...].


这也可能是一个选项:http://maven.40175.n5.nabble.com/exclude-on-scope-import-td5781856.html - btiernay
你最终是如何解决的?我也遇到了完全相同的问题。 - chaosguru
@chaosguru 我重新定义了工件。这很麻烦,但这更多是Maven的问题,而不是Spring Boot的问题。 - btiernay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接