java 从 Maven 中的依赖项中排除类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3167118/
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
Exclude classes from a dependency in Maven
提问by Robert Wilson
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? I'll try to give an example.
我有一个项目依赖于由供应商控制的工件。该工件包含我依赖的一些类,以及一些较旧且会导致问题的类。有没有办法让 maven 自动扩展 jar、删除类并将它们重新打包为依赖项?我会试着举一个例子。
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 ClassAin project-web, but I need to use the ClassBfrom the dependencyartifact. I can't modify supplier:artifact, or us:dependency.
所以 - 我需要ClassA在 project-web 中使用,但我需要使用ClassB来自dependency工件的。我无法修改供应商:工件或我们:依赖关系。
Any ideas?!
有任何想法吗?!
采纳答案by Peter Tillemans
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 pluginas it does exactly what you are asking according to me.
或者,您可以查看maven shade 插件,因为它完全符合我的要求。
回答by Pascal Thivent
If you are using Maven 2.0.9+, then just put your class in a jar and declare it as dependency beforethe artifact from your supplier. As mentioned by @Peter, Maven uses the pom order for the classpath since Maven 2.0.9 (see MNG-1412/ MNG-3111).
如果您使用的是 Maven 2.0.9+,那么只需将您的类放在一个 jar 中,并在您的供应商的工件之前将其声明为依赖项。正如@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 Pluginto unpack the dependency in target/classes(which should be on the compile classpath) using dependency:unpackwith some excludes(see Unpacking specific artifacts). 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).
如果出于某种原因您对此解决方案不满意,那么您可以使用Maven Dependency Plugin来解包依赖项target/classes(它应该在编译类路径上)使用dependency:unpacksome excludes(请参阅解包特定工件)。我什至不会尝试重新打包它,这将引入无用的复杂性(如果可行,在编译类路径上用新的 jar 替换旧的 jar 将不是微不足道的)。

