paul
paul

Reputation: 13481

Maven dependencies exclusions not working

I'm using enforcer plugin of Maven and I see a behavior that I dont quite understand and it's dangerous.

Let's say that I have a conflict since dependency A has bla.jar:1.0 and is in conflict with my dependnecy B which has bla.jar:2.0

Then to fix the conflict, I make an exclude of bla.jar:1.0 from A

  <dependency>
      <groupId>com.foo</groupId>
      <artifactId>A</artifactId>
      <version>a.version.bla</version>
      <exclusions>
        <exclusion>
          <groupId>com.omg</groupId>
          <artifactId>bla</artifactId>
        </exclusion>
      </exclusions>
   </dependency>

expecting the application will get the bla.jar:2.0 fron classpath. But then I see when I run some unit test that the java proce3ss cannot find bla.jar ion the classpath at all and is giving me ClassNotFound in runtime.

Any idea what's wrong here?

I have in my pom defined from top to bottom B and then A

Upvotes: 3

Views: 6989

Answers (1)

J Fabian Meier
J Fabian Meier

Reputation: 35853

Please note that exclusions are not the best way to resolve dependency version conflicts.

The best approach is to use <dependencyManagement>. It allows you to set a version that replaces all transitive versions of that dependency.

In your case, I would first change the exclusion to <dependencyManagement>. Then I would proceed in the following way:

  1. Check mvn dependency:list which version of the dependency is on the classpath. It should be the one specified in <dependencyManagement> unless there is no version of that dependency in your dependency tree. If you find more than one, then probably the groupId changed at some point. Then you need exclusions.

  2. Check the scope of the dependency and verify that it is indeed compile.

  3. Then open the dependency jar and see whether this jar really contains the class for which you get ClassNotFound. Often classes change from version to version.

Upvotes: 4

Related Questions