java Maven:删除单个传递依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/816858/
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-10-29 13:55:03  来源:igfitidea点击:

Maven: remove a single transitive dependency

javamaven-2dependencies

提问by flybywire

My project includes a jar file because it is listed as a transitive dependency.

我的项目包含一个 jar 文件,因为它被列为可传递依赖项。

However, I have verified not only that I don't need it but it causes problems because a class inside the jar files shadows a class I need in another jar file.

但是,我已经验证不仅我不需要它,而且会导致问题,因为 jar 文件中的一个类隐藏了另一个 jar 文件中我需要的类。

How do I leave out a single jar file from my transitive dependencies?

如何从传递依赖项中删除单个 jar 文件?

回答by David Rabinowitz

You can exclude a dependency in the following manner:

您可以通过以下方式排除依赖项:

        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6</version>
                <exclusions>
                        <exclusion>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                        </exclusion>
                </exclusions>
        </dependency>

回答by Pavel

The correct way is to use the exclusions mechanism, however sometimes you may prefer to use the following hack instead to avoid adding a large number of exclusions when lots of artifacts have the same transitive dependency which you wish to ignore. Rather than specifying an exclusion, you define an additional dependency with a scope of "provided". This tells Maven that you will manually take care of providing this artifact at runtime and so it will not be packaged. For instance:

正确的方法是使用排除机制,但是有时您可能更喜欢使用以下 hack 来避免在许多工件具有您希望忽略的相同传递依赖时添加大量排除。您不是指定排除项,而是定义范围为“提供”的附加依赖项。这告诉 Maven 您将在运行时手动提供这个工件,因此它不会被打包。例如:

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
    </dependency>
    <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
    </dependency>

Side effect: you must specify aversion of the artifact-to-be-ignored, and its POM will be retrieved at build-time; this is not the case with regular exclusions. This might be a problem for you if you run your private Maven repository behind a firewall.

副作用:必须指定一个版本的 artifact-to-be-ignored,它的 POM 将在构建时被检索;这不是常规排除的情况。如果您在防火墙后面运行私有 Maven 存储库,这对您来说可能是个问题。

回答by Mike Cornell

You can do this by explicitly excluding the problematic artifact. Take the dependency that includes the problem and mark it to be excluded:

您可以通过明确排除有问题的工件来做到这一点。取包含问题的依赖,并将其标记为排除:

From the maven website:

从 Maven网站

<dependency>
  <groupId>group-a</groupId>
  <artifactId>artifact-a</artifactId>
  <version>1.0</version>
  <exclusions>
    <exclusion>
      <groupId>group-c</groupId>
      <artifactId>excluded-artifact</artifactId>
    </exclusion>
  </exclusions>
</dependency>