简体   繁体   English

Maven:删除单个传递依赖项

[英]Maven: remove a single transitive dependency

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文件?

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>

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. 正确的方法是使用排除机制,但是有时您可能更喜欢使用以下hack来避免在许多工件具有您希望忽略的相同传递依赖性时添加大量排除。 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. 这告诉Maven您将手动处理在运行时提供此工件,因此不会打包它。 For instance: 例如:

    <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 a version of the artifact-to-be-ignored, and its POM will be retrieved at build-time; 副作用:您必须指定要忽略的工件版本,并在构建时检索其POM; 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. 如果您在防火墙后运行私有Maven存储库,这可能是一个问题。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM