Exclude classes from a dependency in Maven

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.

Alternatively you can look at the maven shade plugin as it does exactly what you are asking according to me.


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. As mentioned by @Peter, Maven uses the pom order for the classpath since Maven 2.0.9 (see 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). 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).

Tags:

Java

Maven 2