Java 如何从 spring-boot-starter-parent 中排除特定的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51239227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:02:17  来源:igfitidea点击:

How to exclude specific dependencies from spring-boot-starter-parent

javamavenspring-bootspring-data-jpadependency-management

提问by Saikat

I am using a legacy Spring application and want to migrate to Spring Boot. My intention is to use the spring-boot-starter-data-jpa. For this reason I have added following section in pom.xml(which manages all spring-boot-dependencies):

我正在使用旧版 Spring 应用程序并希望迁移到 Spring Boot。我的意图是使用spring-boot-starter-data-jpa. 出于这个原因,我在pom.xml(管理所有spring-boot-dependencies)中添加了以下部分:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>

But this is screwing up certain dependencies which I need to retain for time being. I am currently using Selenium dependencies (version 2.53.0; added transitively from another project) but spring-boot is fetching dependencies of 3.9.1.

但这破坏了我需要暂时保留的某些依赖项。我目前正在使用 Selenium 依赖项(版本2.53.0;从另一个项目中传递添加)但 spring-boot 正在获取3.9.1.

I want to exclude 3.9.1dependencies but the exclusionfilter is not working as expected.

我想排除3.9.1依赖项,但exclusion过滤器没有按预期工作。

Just to summarize, I want to use spring-boot-starter-parentand spring-boot-starter-data-jpabut not the managed selenium-javafrom spring-boot-dependencies.

只是为了总结,我想用spring-boot-starter-parentspring-boot-starter-data-jpa而不是管理selenium-javaspring-boot-dependencies

Appreciate any help in this regard.

感谢这方面的任何帮助。

采纳答案by M. Deinum

Instead of messing around with <excludes>and then try to figure out what you need to include again (after figuring out what you excluded). Just override the version as explained here in the Spring Boot Reference Guide.

而不是搞砸<excludes>然后试图弄清楚你需要再次包括什么(在弄清楚你排除了什么之后)。只需按照 Spring Boot 参考指南中的说明覆盖版本。

Assuming you are using the spring-boot-starter-parentas the parent you can just add a <selenium.version>to your <properties>section to specify which version you want.

假设您将spring-boot-starter-parent用作父级,您只需将 a 添加<selenium.version>到您的<properties>部分以指定您想要的版本。

<properties>
  <selenium.version>2.53.0</selenium.version>
</properties>

This will make Spring Boot use the version you want.

这将使 Spring Boot 使用您想要的版本。

回答by Dinesh

Remove the deppendency from the starter pom. Use the exclsuions tag in pom.xml. For example to remove the actuator dependency

从 starter pom 中删除依赖项。在 pom.xml 中使用 exclsuions 标记。例如删除执行器依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-actuator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

回答by sreenath reddy

Mention your dependency in pom.xml which you need to exclude in exclusion tag. Then excluded dependency will not be downloaded:

在 pom.xml 中提及您需要在排除标记中排除的依赖项。然后将不会下载排除的依赖项:

<dependency>
      <groupId>sample.ProjectA</groupId>
      <artifactId>Project-A</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion> 
 <!-- declare the exclusion here -->
          <groupId>sample.ProjectB</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>