19

For example I have dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.5.6</version>
</dependency>  

Can I exclude one class, for example org/slf4j/Marker.class?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • `exclude` during what phase - compile or package? – Raghuram Apr 10 '12 at 10:46
  • package in .ear, using maven-ear-plugin – Ilya Apr 10 '12 at 10:48
  • 1
    hmm! what you need in the .ear is the `slf4j-api.jar` without the specified class. There is no straightforward way to achieve this. – Raghuram Apr 10 '12 at 10:52
  • slf4j-api is the example – Ilya Apr 10 '12 at 10:54
  • But this does not make sense to exclude a single class in particular a class of an API. What you might do is to define the dependency scope to "provided" but this means it must be delivered by the environment where you deploy (JEE container based on EAr). – khmarbaise Apr 11 '12 at 07:32
  • possible duplicate of [Exclude classes from a dependency in Maven](http://stackoverflow.com/questions/3167118/exclude-classes-from-a-dependency-in-maven) – Barett Jun 13 '14 at 18:56

3 Answers3

11

Try it with the shade plugin

Details on why use shade and basic usage

sfk
  • 625
  • 1
  • 9
  • 17
9

Excluding a single class in not possible. Within <dependency> tags you can define <exclusions/>. However, these are for entire dependencies.

The shade plugin should be handled with care. Generally, it's not good practice to be creating a jar containing all your dependencies in one place as it tends to lead to problems if you are to be using the produced artifact in another project as a dependency. For example, shading slf4j in your jar and then depending on your artifact in another project where you have another slf4j will bring you grief.

carlspring
  • 31,231
  • 29
  • 115
  • 197
3

You could change those classes and define them in a different jar/module which should be included as a dependency before the jar that supplies the dependency where your class to be excluded resides (Marker.class).

Maven remembers the classpath ordering from version 2.0.9.

despot
  • 7,167
  • 9
  • 44
  • 63