简体   繁体   English

从 Maven 依赖项中排除类

[英]Exclude classes from a dependency in Maven

I have a project which depends on an artifact which is controlled by a supplier.我有一个项目依赖于由供应商控制的工件。 This artifact contains some classes that I depend on, and some which are older and causing problems.该工件包含我依赖的一些类,以及一些较旧且会导致问题的类。 Is there a way to get maven to automatically expand a jar, remove classes, and re-package them as a dependency?有没有办法让 maven 自动扩展 jar、删除类并将它们重新打包为依赖项? I'll try to give an example.我会试着举一个例子。

supplier:artifact:1.0.0
com.supplier.useful.ClassA
com.supplier.dto.ClassB (old)

us:dependency:1.1.20
com.supplier.dto.ClassB (new)

us:project-web:1.1.20
- supplier:artifact:1.0.0
- us:dependency:1.1.20

So - I need to use ClassA in project-web, but I need to use the ClassB from the dependency artifact.所以 - 我需要在 project-web 中使用ClassA ,但我需要使用dependency工件中的ClassB I can't modify supplier:artifact, or us:dependency.我无法修改供应商:工件或我们:依赖关系。

Any ideas?!有任何想法吗?!

Since version 2.0.9 maven preserves the classpath ordering so if you define your patched classes in a jar BEFORE the supplier jar it will hide the supplier implementation.由于 2.0.9 版 maven 保留了类路径顺序,因此如果您在供应商 jar 之前的 jar 中定义修补类,它将隐藏供应商实现。

Alternatively you can look at the maven shade plugin as it does exactly what you are asking according to me.或者,您可以查看maven shade 插件,因为它完全符合我的要求。

If you are using Maven 2.0.9+, then just put your class in a jar and declare it as dependency before the artifact from your supplier.如果您使用的是 Maven 2.0.9+,那么只需将您的类放在一个 jar 中,并您的供应商的工件之前将其声明为依赖项。 As mentioned by @Peter, Maven uses the pom order for the classpath since Maven 2.0.9 (see MNG-1412 / MNG-3111 ).正如@Peter 所提到的,自 Maven 2.0.9 起,Maven 使用 pom 顺序作为类路径(参见MNG-1412 / MNG-3111 )。

If for whatever reason you're not satisfied with this solution, then you could use the Maven Dependency Plugin to unpack the dependency in target/classes (which should be on the compile classpath) using dependency:unpack with some excludes (see Unpacking specific artifacts ).如果出于某种原因您对此解决方案不满意,那么您可以使用Maven Dependency Plugin使用dependency:unpack with some excludes来解包target/classes (应该在编译类路径上)中的dependency:unpack (请参阅解包特定工件)。 I wouldn't even try to repackage it, this will introduce useless complexity (replacing the old jar with the new one on the compile classpath won't be trivial, if feasible).我什至不会尝试重新打包它,这将引入无用的复杂性(如果可行,在编译类路径上用新的 jar 替换旧的 jar 将不是微不足道的)。

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

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