Alex Rivera | Logout

Exclude classes from a dependency in Maven

Asked 2010-07-02T15:36:32.557
13

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.

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. I can't modify supplier:artifact, or us:dependency.

Any ideas?!

Edit
Report

1 Answer

6

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).

answered 2010-07-02T17:45:31.057

Your Answer