【问题标题】:How to exclude specific class from a Maven dependency ( Jar dependency) at runtime如何在运行时从 Maven 依赖项(Jar 依赖项)中排除特定类
【发布时间】:2020-04-19 13:57:48
【问题描述】:

如何从 Maven 依赖项(Jar 依赖项)中排除特定类。

我有一个来自 avro-tools.jar 的 ObjectUtils.class 的问题,它是在运行时选择的,它没有从 commons-lang3 jar 中使用的方法。导致运行时 NoSuchMethod 错误。

我想知道有什么方法可以在我的 pom.xml 中指定从 avro-tools.jar (1.9.1) 版本中排除此类 org.apache.commons.lang3.ObjectUtils 并从 commons- lang3.jar(3.9版)

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后edit您的问题包括对您的项目结构和您遇到的问题的更详细描述。还包括项目的 POM 文件。
  • 您不能排除包含在依赖项中的单个类。在这种情况下,您唯一能做的就是直接在您自己的项目中为 commons-lang 定义一个版本,这会推翻依赖关系……但这可能会导致其他问题……可能你必须使用其他版本。但看起来 avro-tools.jar 已经遮蔽了 commons-lang3 部分,这非常糟糕但无法更改。您应该联系作者...

标签: java maven maven-plugin


【解决方案1】:

试试 maven-shade-plugin,它会从 pom.xml 生成的最终 jar 中删除类

注意:- 从 jar 中删除特定类是不可能的。

     <profiles>
    <profile>
        <id>TestShadeProfile</id>
        <dependencies>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-tools</artifactId>
            <version>1.9.1</version>
        </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                            <filters>
                             <filter>
                                <artifact>org.apache.avro:avro-tools</artifact>
                                <excludes>
                                    <exclude>org/apache/commons/lang3/ObjectUtils*</exclude>
                                </excludes>
                             </filter>
                            </filters>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

希望对你有帮助

mvn clean package -P TestShadeProfile

【讨论】:

  • 您好 Sarjit,感谢您的回复。让我试着回复一下
  • 没有 Sarjit,同样的问题,我没有看到该类在下载依赖项时没有从 jar 中排除
  • 排除仅适用于依赖项,这意味着在被定义为依赖项的 jar 上。您不能排除单个班级。
  • 使用 maven-shade-plugin 更新了解决方案
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 2015-03-07
  • 2015-09-23
  • 2012-02-25
  • 2021-02-14
  • 1970-01-01
相关资源
最近更新 更多